This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof operator" Watch "instanceof operator" New topic
Author

instanceof operator

Pankaja Shinde
Ranch Hand

Joined: Sep 15, 2006
Posts: 87
I found this question on Mock exam.

Which of the following statements are true?

1) The instanceof operator can be used to determine if a reference is an instance of a class, but not an interface.
2) The instanceof operator can be used to determine if a reference is an instance of a particular primitive wrapper class
3) The instanceof operator will only determine if a reference is an instance of a class immediately above in the hierarchy but no further up the inheritance chain
4) The instanceof operator can be used to determine if one reference is of the same class as another reference thus

Answer is 2.

Can anyone explain me.

I think question is wrong.

Regards
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

I guess the same. Here is excerpt which I took from Java Specification,

At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException. Otherwise the result is false.


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Pankaja Shinde:
I found this question on Mock exam.

Which of the following statements are true?

1) The instanceof operator can be used to determine if a reference is an instance of a class, but not an interface.
2) The instanceof operator can be used to determine if a reference is an instance of a particular primitive wrapper class
3) The instanceof operator will only determine if a reference is an instance of a class immediately above in the hierarchy but no further up the inheritance chain
4) The instanceof operator can be used to determine if one reference is of the same class as another reference thus

Answer is 2.

Can anyone explain me.

I think question is wrong.

Regards


1 is certainly not true. 3 is not true. 4 is not true because instanceof cannot be used between references.

So by the process of elimination, if one of the answers has to be true, then 2 is correct.

Also simply by looking at option 2, there is nothing unreasonable there.
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

I don't think Option 2 is correct, because instanceof can be used with any class not only just primitive wrappers.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
But option2 is not saying that the instanceof operator can be used only with wrapper classes.

It is simply saying that the instanceof operator can be applied between a reference and a wrapper class (assuming that the reference is compatible).
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Which Mock exam?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

MOCK Exam LINK
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof operator
 
Similar Threads
A Question from Marcus exam 3 about instanceof
instanceof Marcus Mock 3 No. 6
Test Question - Explanation?
Using instanceof operator
Q about instanceof