OCA Java SE 8 Class Design - OCA Mock Question Class Design 20








Question

Which of the following statements can be inserted in the ___ so that the code will compile successfully?

     public class Shape {} 
     public class Circle extends Shape {} 
     public class Rectangle {} 
     public class ShapeHandler { 
        private Shape shape; 
        public void setShape(Shape shape) { this.shape = shape; } 
        public static void main(String[] args) { 
            new ShapeHandler().setShape(___); 
        } 
     } 
  1. new Circle()
  2. new Rectangle()
  3. new Shape()
  4. new Object()
  5. new String("Shape")
  6. null




Answer



A, C, F.

Note

Circle is a subclass of Shape, so A can be used.

Rectangle is not defined as a subclass of Shape, so it cannot be used and B is incorrect.

The class Shape is not marked as abstract, so it can be instantiated and passed, so C is correct.

Object is a superclass of Shape, not a subclass, so it also cannot be used and option D is incorrect.

The class String is unrelated in this example, so E is incorrect.

A null value can always be passed as an object value, regardless of type, so F is correct.