Java OCA OCP Practice Question 1450

Question

What does the following print?

public class Main { 
   static interface Printable {} 
   static class Rectangle implements Printable {} 

   public static void main(String[] args) { 
      Rectangle rect = new Rectangle(); 
      boolean n = null instanceof Rectangle; 
      boolean v = rect instanceof Printable; 
      boolean b = rect instanceof Rectangle; 
      System.out.println(n + " " + v + " " + b); 
  } //w w  w.  j  a va  2 s.  com
} 
  • A. true true true
  • B. false true true
  • C. false false false
  • D. None of the above


B.

Note

While using null with instanceof compiles, it always returns false.

The other two instanceof calls show that instanceof can be used with both classes and interfaces.

They both return true, making Option B correct.




PreviousNext

Related