#4 is wrong. Expression (A instanceof B) will evaluate as true not only if A has the same type as B, but also if A itself or one of it's superclasses implements interface B or; A extends (directly or not) class B For instance:
The instanceof operator returns true if the type of the object referenced by the left operand is assignment compatible with the type of the right operand. The test for assignment compatibility is less restrictive than a test for an exact match between two types. Although the instanceof operator will return true if the types of the left and right operands match exactly, instanceof will also return true if the type of the left operand is a subclass of the right operand. If you want to test for an exact match between the types of two objects, then use the equality operator, ==, to verify that both classes have the same Class object: obj1.getClass() == obj2.getClass().
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
I find it helpful to note that the wording of #4 is referring to the right hand side (rhs) as a reference. The rhs must be an explicitly named Class that the left operator may be cast to, or an Interface. Given:
Line 1 will compile, run and return true, an Integer may be cast to a Number Line 2 will not compile, the rhs is a reference, not a Class or Interface name as required. (this is the situation answer #4 refers to) Line 3 will not compile, you may not use instanceof with a dynamically generated rhs Line 4 will compile and return false when run, Collection is an interface, whether or not i1 implements it will not be determined until runtime. Line 5 will not compile, i1 is known at compile time to not be castable to type Thread. hth