File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mansi Agarwal
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would that generate a compilation error ? You can compare any objects using "instanceof".
 
Mansi Agarwal
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
 
suhas dasnagar
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely the answer is B and not C? 2 would not be printed as would be false?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the above link explains the doubt. good one Karthikeyan
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, could someone confirm what the correct answer is for the original question asked in this thread? Thanks
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic