File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Use of instanceof operator K&B Practise Exam 3 Q-35 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 "Use of instanceof operator K&B Practise Exam 3 Q-35" Watch "Use of instanceof operator K&B Practise Exam 3 Q-35" New topic
Author

Use of instanceof operator K&B Practise Exam 3 Q-35

Mansi Agarwal
Ranch Hand

Joined: Apr 11, 2012
Posts: 53


What is the result?
A. 1
B. 1 3
C. 1 2 3
D. 1 3 4
E. 1 2 3 4
F. Compilation fails.
G. An exception is thrown at runtime.

Why it says answer 3? It should be F. We cant compare 'r' with the animal in line15.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Why would that generate a compilation error ? You can compare any objects using "instanceof".


[My Blog]
All roads lead to JavaRanch
Mansi Agarwal
Ranch Hand

Joined: Apr 11, 2012
Posts: 53
Hi Christophe,

I didn't get you.

As far as I know we cant compare incompatible class objects. We can compare object of the same hierarchy.

Example:
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
You are right that some types are considered incompatible and cannot be legally compared with instanceof. At least, not with references that identify the incompatible types - you could always use two Object references to compare anything you want. But - are a Robot and Animal really incompatible? Is there no possible way to create an object that is both a Robot and an Animal?
Karthikeyan Kandasamy
Greenhorn

Joined: Apr 17, 2011
Posts: 14
this post has the answer - http://www.coderanch.com/t/513123/java-programmer-SCJP/certification/instanceof-comparing-two-different-branches

suhas dasnagar
Greenhorn

Joined: Dec 24, 2005
Posts: 3
Hi,
The answer given in the K & B book is correct.
As per your reference in the line 15 it will check the reference variable is not an instance of Animal it return false so the sysout will not print.
It does not give any cmpiler error.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

Surely the answer is B and not C? 2 would not be printed as would be false?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1



the above link explains the doubt. good one Karthikeyan
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

Sorry, could someone confirm what the correct answer is for the original question asked in this thread? Thanks
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

James Boswell wrote:Sorry, could someone confirm what the correct answer is for the original question asked in this thread? Thanks


the main question was regarding whether or NOT compiler error comes when using instanceof operator. consider this example

s instance of java.utilDate; // here is is of type java.util.String

the answer would be compiler error. why ? since String and Date are in different hierarchies in java class hierarchy. had 's' be of the type Object ,the compiler would have passed it, though it would fail at runtime.

now consider this code snippet



And

rob instanceof Animal; // why compiler does not flag error here like it did in the above case, this was the original question. mansi's(question poster) doubt was that it should give compiler error since robot and animal are not in same hierarchy(like it was in string and date case above).

i think the link given by karthikeyan explains the reason very well with extra information.

if you still have doubts James, feel free to post.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

Mansi Agarwal wrote:

What is the result?
A. 1
B. 1 3
C. 1 2 3
D. 1 3 4
E. 1 2 3 4
F. Compilation fails.
G. An exception is thrown at runtime.

Why it says answer 3? It should be F. We cant compare 'r' with the animal in line15.


This was the original question. What is the answer? I believe it is B, not C as the OP has implied.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

if you look into the previous posts, you will come to know that original question was not really regarding choice. it was beyond that , in that whether the code will even compile. if you want to know the correct answer you can simply run it and find it out for yourself. though the link given by karthikayen has good food for thought and knowledge.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of instanceof operator K&B Practise Exam 3 Q-35