How many lines of the main method fail to compile?
11: static interface Printable {} 12: static class Rectangle implements Printable {} 13: // w ww . j a va2s . co m 14: public static void main(String[] args) { 15: Rectangle rect = new Rectangle(); 16: 17: System.out.println(null instanceof Rectangle); 18: System.out.println(rect instanceof Printable); 19: System.out.println(rect instanceof Rectangle); 20: System.out.println(rect instanceof ArrayList); 21: System.out.println(rect instanceof Collection); 22: }
A.
You are allowed to use null with instanceof; it just prints false.
The rect variable is both a Printable and a Rectangle, so lines 18 and 19 print true.
rect is not an ArrayList or Collection.
The compiler only knows that rect is not an ArrayList because ArrayList is a concrete class.
Line 20 does not compile.
The compiler can't definitively state that rect is not a Collection.
Only line 20 fails to compile, and Option A is correct.