OCA Java SE 8 Mock Exam - OCA Mock Question 10








Question

What is the output of the following code? (Choose all that apply)

1: interface MyInterface { int getValue(); } 
2: abstract class MyClass implements MyInterface { 
3:   protected int getValue() {return 4;} 
4: } 
5: public class MyClass2 extends MyClass { 
6:    public static void main(String[] args) { 
7:      MyClass puma = new MyClass(); 
8:      System.out.println(puma.getValue()); 
9:    } 
10: 
11:  public int getValue(int length) {return 2;} 
12: } 
  1. 2
  2. 4
  3. The code will not compile because of line 3.
  4. The code will not compile because of line 5.
  5. The code will not compile because of line 7.
  6. The code will not compile because of line 11.
  7. The output cannot be determined from the code provided.




Answer



C, D, E.

Note

The method getValue() in the interface MyInterface is assumed to be public, since it is part of an interface.

C is correct. The implementation of the method on line 3 is therefore an invalid override, as protected is a more restrictive access modifier than public.

D is correct. class MyClass2 implements an overloaded version of getValue(), since the declaration in class MyClass is invalid, it needs to implement a public version of the method. Since it does not, the declaration of MyClass is invalid.

Option E is correct, since MyClass is marked abstract and cannot be instantiated.

F is not correct. The overloaded method on line 11 is declared correctly.

As the code has compiler errors, A, B, and G are not correct.