• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mock Question Doubt...EQ+..Operators and Assignment

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,

Question ID : com.enthuware.ets.scjp.v5.2.185



Please select 1 option

A. True
B. False

The answer is A..

I have executed and seen that it prints 2...but I am not getting it..

I dont know why NullPointerException is not thrown at this loc..

getArray()[index=2]++;

here getArray() returns null after that index is set to 2..then when postfix increment operator is applied, why doesn't it throw a nullPointerException??



Can anyone please give their suggestions on the above problem.Thank you in advance.

Regards,
Hardik.S.Raja
 
Enthuware Software Support
Posts: 4818
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hardik,
There is an explanation given with it -


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.



So NPE is thrown (put e.printStackTrace() or something in the empty catch block) but by then index has already been assigned the value of 2.
 
Hardik Raja
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Paul,

Got it..

Thanks
Hardik
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
NullPointerException is thrown here,but you are printing that exception.
make some change in your code:

public class PracticeTest {
public static int[ ] getArray() { return null; }
public static void main(String[] args)
{
int index = 1;
try
{
getArray() [index=2]++;
} catch (Exception e)
{
System.out.println(e);//change
}
System.out.println("index = " + index);
}
}
Now you will see that your program is throwing NullPointerException.
 
reply
    Bookmark Topic Watch Topic
  • New Topic