File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes from hardest questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "from hardest questions" Watch "from hardest questions" New topic

from hardest questions

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi, this one is from the bill hardest questions.
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;
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

Joined: Aug 25, 2000
Posts: 44
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.
Get Certified, Guaranteed!

<A HREF="" TARGET=_blank rel="nofollow"></A> <BR>Get Certified, Guaranteed!
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi, thanks very much for explaining this to me. That helps.
I agree. Here's the link:
subject: from hardest questions
It's not a secret anymore!