About Question com.enthuware.ets.scjp.v6.2.185 :

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

Moderator: admin

Post Reply
Aditya553
Posts: 15
Joined: Sun Aug 06, 2017 2:37 am
Contact:

About Question com.enthuware.ets.scjp.v6.2.185 :

Post by Aditya553 »

I'm unable to understand.Please explain in a simpler manner.

Question -
The following class will print index = 2 when compiled and run.


class Test
{
public static int[ ] getArray() { return null; }
public static void main(String[] args)
{
int index = 1;
try
{
getArray()[index=2]++;
}
catch (Exception e){ } //empty catch
System.out.println("index = " + index);
}
}

Options -
1)True
2) False

Explanation was given -
If the array reference expression produces null instead of a reference to an array, then a NullPointerException is thrown at runtime, but only after all parts of the array reference expression have been evaluated and only if these evaluations completed normally. The embedded assignment of 2 to index occurs before the check for a null pointer.
In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated.
Note that if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated.

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

Re: About Question com.enthuware.ets.scjp.v6.2.185 :

Post by admin »

See my post in this thread https://coderanch.com/t/683214/certific ... ng#3205868
Then apply the same concept here.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Aditya553
Posts: 15
Joined: Sun Aug 06, 2017 2:37 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.185 :

Post by Aditya553 »

So
getArray()[index=2]++;
here first index becomes 2 and than it increases to 3 and than it becomes
null[3]

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

Re: About Question com.enthuware.ets.scjp.v6.2.185 :

Post by admin »

Why do you think index increments to 3?? ++ is not there on index.

The element at index 2 in the array returned by getArray() is incremented.
If you like our products and services, please help us by posting your review here.

Aditya553
Posts: 15
Joined: Sun Aug 06, 2017 2:37 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.185 :

Post by Aditya553 »

public static int[ ] getArray() { return null; }
so this line means int[] have null values .

getArray()[index=2]++;
this line means index becomes 2 and than in getArray int[] at index 2 (which is null)is incremented.
null cannot be incremented

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

Re: About Question com.enthuware.ets.scjp.v6.2.185 :

Post by admin »

Aditya553 wrote:public static int[ ] getArray() { return null; }
so this line means int[] have null values .
No, it means there is no array at all because the return value is null. There is a difference between a null reference and an array containing nulls. See this: https://stackoverflow.com/questions/274 ... mpty-array
Aditya553 wrote: getArray()[index=2]++;
this line means index becomes 2 and than in getArray int[] at index 2 (which is null)is incremented.
null cannot be incremented
Correct. Since getArray() returns null, you can't even access null[2] let alone increment it. So there will be a NullPointerException, which will be caught by the catch block.
If you like our products and services, please help us by posting your review here.

Aditya553
Posts: 15
Joined: Sun Aug 06, 2017 2:37 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.185 :

Post by Aditya553 »

ok got it.
Thanks

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests