• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would that generate a compilation error ? You can compare any objects using "instanceof".
 
Mansi Agarwal
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Rancher
Posts: 4334
59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this post has the answer - https://coderanch.com/t/513123/java-programmer-SCJP/certification/instanceof-comparing-two-different-branches

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely the answer is B and not C? 2 would not be printed as would be false?
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthikeyan Kandasamy wrote:this post has the answer - https://coderanch.com/t/513123/java-programmer-SCJP/certification/instanceof-comparing-two-different-branches



the above link explains the doubt. good one Karthikeyan
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
This one time, at bandcamp, I had relations with a tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic