About Question enthuware.ocajp.i.v7.2.972 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
The_Nick

About Question enthuware.ocajp.i.v7.2.972 :

Post by The_Nick »

Ok, I agree here.
Besides the scope of the question..
short s = 10;
it compiles ok..
10 is implicitly an int.
How does the compile do in order to accomplish the operation without causing an exception?
There is an implicit conversion like (short)10. or the compiler sees at compilation time that the number 10 can actually fit a short and therefore no problem?
What happens behind the scenes?
Thanks a lot in advance.

The_Nick

Re: About Question enthuware.ocajp.i.v7.2.972 :

Post by The_Nick »

Underlining also the fact that:

Code: Select all

int a = 4;
short b = 10+a;
It gives a compilation error.
Cannot convert int to short: type mismatch.
Thanks in advance.
This software plush forum is immense.

admin
Site Admin
Posts: 10404
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.972 :

Post by admin »

This is because of implicit narrowing of compile time constant int value into a shorter type such as a byte or short. Check this out for more info: http://stackoverflow.com/questions/2720 ... nd-casting

Kipchakbaev
Posts: 7
Joined: Sat Jan 18, 2014 8:04 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.972 :

Post by Kipchakbaev »

It is impossible to answer this question incorrect because the answers are mutually exclusive

wokingtown11
Posts: 4
Joined: Tue May 06, 2014 3:37 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.972 :

Post by wokingtown11 »

Kipchakbaev wrote:It is impossible to answer this question incorrect because the answers are mutually exclusive
I agree, you dont need to think about it.

2 answers are correct:

1 - false
2 - false
3 - true
4 - none

So of course only 1 and 2 can be the answers :-)

Mikhail Volkov
Posts: 8
Joined: Mon Jul 29, 2019 11:25 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.972 :

Post by Mikhail Volkov »

But remember, that
final short s = 10;
char c = s; - will be fine work :!:

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests