Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

from hardest questions

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, this one is from the bill hardest questions.
java.lang.Object
java.util.EventObject
java.awt.AWTEvent
java.awt.event.ActionEvent
suppose we have the following code to count events and save the most recent event
int evtCt = 0;
AWTEvent lastE;
public void saveEvent(AWTEvent evt){
lastE = evt;
evtCt++;
}
which of the following calls of saveEvent would run without causing an exception. Select all which are correct;
a call with an AWTEvent object reference;
b call with an ActionEvent object reference;
c call with an EventObject object reference;
d call with null value;
correct answer is a, b, d.

I can understand why a, b right. How about d, could explain me how the "null" worked here.
 
Paul A
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What, in the code, suggests you that it should not work?
In the method, the passed parameter is not accessed anywhere. So there is no chance of a NPE. All it is doing is incrementing the counter and assigning the passed value to lastE. So when you pass null, lastE is assigned null. But it is ok as it is not accessed anywhere.
HTH,
Paul
------------------
http://pages.about.com/jqplus
Get Certified, Guaranteed!
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, thanks very much for explaining this to me. That helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic