About Question com.enthuware.ets.scjp.v6.2.185 :
Moderator: admin
-
- Posts: 15
- Joined: Sun Aug 06, 2017 2:37 am
- Contact:
About Question com.enthuware.ets.scjp.v6.2.185 :
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.
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.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.185 :
See my post in this thread https://coderanch.com/t/683214/certific ... ng#3205868
Then apply the same concept here.
HTH,
Paul.
Then apply the same concept here.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 15
- Joined: Sun Aug 06, 2017 2:37 am
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.185 :
So
getArray()[index=2]++;
here first index becomes 2 and than it increases to 3 and than it becomes
null[3]
getArray()[index=2]++;
here first index becomes 2 and than it increases to 3 and than it becomes
null[3]
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.185 :
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.
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.
-
- Posts: 15
- Joined: Sun Aug 06, 2017 2:37 am
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.185 :
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
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
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.185 :
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-arrayAditya553 wrote:public static int[ ] getArray() { return null; }
so this line means int[] have null values .
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.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
If you like our products and services, please help us by posting your review here.
-
- Posts: 15
- Joined: Sun Aug 06, 2017 2:37 am
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.185 :
ok got it.
Thanks
Thanks
Who is online
Users browsing this forum: No registered users and 31 guests