OCA Java SE 8 Mock Exam 2 - OCA Mock Question 26








Question

Given the following code, select the correct statements:

class Shape {
  public void fragrance() {
    System.out.println("Shape");
  }
}

class Rectangle {
  public void fragrance() {
    System.out.println("Rectangle");
  }
}

class Square {
  public void fragrance() {
    System.out.println("Square");
  }
}

public class Main {
  public void arrangeShapes() {
    Shape f1 = new Rectangle();
    Shape f2 = new Square();
    f1.fragrance();
  }
}

             a  The output of the code is: 

                Shape 

             b  The output of the code is: 

                Rectangle 

             c  The output of the code is: 

                Square 

             d  The code fails to compile. 





Answer



D

Note

Neither of the classes Rectangle or Square extends the class Shape.

Hence, a variable of type Shape can't be used to store objects of the classes Rectangle or Square.

The following lines of code will fail to compile:

Shape f1 = new Rectangle(); 
Shape f2 = new Square();