OCA Java SE 8 Mock Exam - OCA Mock Question 34








Question

Which code can be used to create and initialize an object of class ColorPencil?

class Shape {} 
class ColorShape extends Shape { 
    String color; 
    ColorShape(String color) {this.color = color;} 
} 
  1. ColorShape var1 = new ColorShape();
  2. ColorShape var2 = new ColorShape(RED);
  3. ColorShape var3 = new ColorShape("RED");
  4. Shape var4 = new ColorShape("BLUE");




Answer



C, D

Note

A is incorrect because new ColorShape() tries to invoke the noargument constructor of class ColorShape, which isn't defined in class ColorShape.

B is incorrect because new ColorShape(RED) tries to pass a variable RED, which isn't defined in the code.