aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from Ch 3 Self Test, Sierra/Bates Book 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 "Question from Ch 3 Self Test, Sierra/Bates Book" Watch "Question from Ch 3 Self Test, Sierra/Bates Book" New topic
Author

Question from Ch 3 Self Test, Sierra/Bates Book

David Willis
Greenhorn

Joined: Apr 16, 2003
Posts: 18
I just took the Self Test from Chapter 3 of Kathy Sierra & Bert Bates "Sun Certified Programmer & Developer for Java 2 Exam Study Guide" and I am slightly bewildered by question #2. Here's the question:
Given the following:
1. import java.awt.*;
2. class Ticker extends Component {
3. public static void main (String [] args) {
4. Ticker t = new Ticker();
5.
6. }
7. }
which two of the following statements, inserted independently, could legally be inserted into line 5 of this code? (Choose two.)
A. boolean test = (Component instanceof t);
B. boolean test = (t instanceof Ticker);
C. boolean test = t.instanceof(Ticker);
D. boolean test = (t instanceof Component);
E. boolean test = t.instanceof(Object);
F. boolean test = (t instanceof String);
I know that A,C, and E are illegal statements (will not compile). But it looks to me like B,D, and F are all legal statements that can bee added to line 5 and not prevent sucessful compilation. But the question states that there are only two correct answers, and the answer key says they are B and E. Is this an inconsistency in the phrasing of the question and the expected answers? Or am I just missing something so obvious that I just can't see it? Any help is appreciated.
David.
[ April 18, 2003: Message edited by: David Willis ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Since it is impossible for t to ever be a String, the comparison is meaningless since the compiler knows that it must always be false. So it gives you a compile error.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
David Willis
Greenhorn

Joined: Apr 16, 2003
Posts: 18
Thanks. I didn't realize that the Java compiler checked to see if the variable operand of instanceof was in the hierarchy tree of the type operand. I thought it just confirmed syntax, and performed the analysis at runtime. Does the Java Language Specification demand that this compilation time analysis of the hierarchy tree take place, or is it a unique characteristic of Sun's javac?
[ April 16, 2003: Message edited by: David Willis ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
See JLS 15.20.2
Type Comparison Operator instanceof
If a cast of the RelationalExpression to the ReferenceType would be rejected as a compile-time error, then the instanceof relational expression likewise produces a compile-time error. In such a situation, the result of the instanceof expression could never be true.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
instanceof is not a method into which you can pass an argument, so E is wrong.
B and D are therefore right.
Incidentally, they would both evaluate to true because this is what you get when the right operand is the same class or superclass of the left operand.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
David Willis
Greenhorn

Joined: Apr 16, 2003
Posts: 18
Thanks Thomas. I found a link the the JLS in a earlier thread you posted in, and have bookmarked it for future reference.
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Yikes! Howdy David, I just wanted to confirm something -- you said that the book says "B and E" are correct?
Because in our copies of the book, it says "B and D" (which are indeed the correct answers), for the reasons indicated by others. (In a nutshell, if the compiler can tell, for certain, that this instanceof could NEVER work at runtime, you'll get a compiler error).
So now I'm all worried about where the "B and E" answers came from. I'm hoping that you simply copied it down wrong
Please let me know, or I won't get any sleep tonight
-Kathy


Co-Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0596007124/ref=jranch-20" target="_blank" rel="nofollow">"Head First Design Patterns"</a><br /> <br />Just a Jini girl living in a J2EE world.
preeti khane
Ranch Hand

Joined: Mar 12, 2003
Posts: 93
what I would like to add here is that the instanceof comparison works similar to the == for objects, that is the object instance being compared have to be of similar types , classes, subtypes in the hierachy to match...

However when checking if a class object is an instanceof a unrelated interface, the class need not implement the interface... there will be no compile time error but a runtime error... Am I right in this assumption?
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
You won't get a runtime error, just false being returned.
Remember that x instanceof y always yields true if the class (or interface) y can be found in the class hierarchy of x, that is:
y is a superclass of x or x itself
y is an interface implemented by any superclass of x or by x itself

Here is some code which indicates what happens with interfaces.
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi David,
I fully agree with 'Kathy'. B and D are correct answers, can you explain how E will the answer.


Narasimha
David Willis
Greenhorn

Joined: Apr 16, 2003
Posts: 18
No worries. I screwed up when composing my post. The book does say B and D are the correct answers (and that's what I have written in my self test notebook, too :-) .) Sorry about causing any unwarrented panic.
 
jQuery in Action, 2nd edition
 
subject: Question from Ch 3 Self Test, Sierra/Bates Book