Java OCA OCP Practice Question 1476

Question

What does the following print?

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

   public static void main(String[] args) { 
      Rectangle rect = new Square(); 
      Square van = new Square(); 
      Square[] vans = new Square[0]; 

      boolean b = rect instanceof Printable; 
      boolean v = van instanceof Printable; 
      boolean a = vans instanceof Printable[]; 

      System.out.println(b + " " + v + " " + a); 
   } //from w w w. j  a  va 2 s. c  om
} 
  • A. true true true
  • B. false true true
  • C. true false false
  • D. None of the above. The code does not compile


A.

Note

Clearly a Rectangle is a Printable since the Rectangle class implements Printable.

The Square class is also a Printable since it extends Rectangle.

This question also confirms you know that arrays can be tested with instanceof, which they can.

Option A is correct.




PreviousNext

Related