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








Question

Select the correct method declaration to be inserted at //INSERT CODE HERE:

         interface Printable { 
             void print(); 
         } 
         class Square implements Printable { 
             public void print() { 
                System.out.println("Square print"); 
             } 
         } 
         class Rectangle implements Printable { 
             public void print() { 
                System.out.println("Rectangle print"); 
             } 
         } 
         public class Test { 
             // INSERT CODE HERE 
                 movable.print(); 
             } 
         } 
  1. void walk(Printable movable) {
  2. void walk(Square movable) {
  3. void walk(Rectangle movable) {
  4. void walk() {




Answer



a, b, c

Note

D is incorrect. Because class Test doesn't define any instance methods, the only way that the question's line of code can execute is when a method parameter movable is passed to the method walk.

A is correct. Because the interface Printable defines the method print, you can pass a variable of its type to the method print.

B is correct. Because the class Square implements the interface Printable and defines the method print, you can pass a variable of its type to the method walk. With this version of the method walk, you can pass it an object of the class Square or any of its subclasses.

C is correct. Because the class Rectangle implements the interface Printable and defines the method print, you can pass a variable of its type to the method walk. With this version of method walk, you can pass it an object of the class Rectangle or any of its subclasses.