Java OCA OCP Practice Question 1416

Question

How many lines of the following application contain compilation errors?

1:  package mypkg; 
2:   /* w w  w .j a va 2 s  .c om*/
3:  interface Playable {} 
4:  abstract class Car implements Playable { 
5:     public Car(int p) {} 
6:     public void play() {} 
7:  } 
8:  public class Main extends Car { 
9:     public void play(int count) {} 
10:    public void concert() { 
11:       super.play(5); 
12:    } 
13:    public static void main(String[] p) { 
14:       Playable mn = new Main(); 
15:       mn.concert(); 
16:    } 
17: } 
  • A. None. The code compiles and runs without issue.
  • B. One
  • C. Two
  • D. Three
  • E. Four


D.

Note

The code definitely does not compile, so Option A is incorrect.

The first problem with this code is that the Main class is missing a constructor causing the class declaration on line 8 to fail to compile.

The default no-argument constructor cannot be inserted if the superclass, Car, does not define a no-argument constructor.

The second problem with the code is that line 11 does not compile, since it calls super.play(5), but the version of play() in the parent class does not take any arguments.

Line 15 does not compile.

While mn may be a reference variable that points to a Main() object, the concert() method cannot be called unless it is explicitly cast back to a Main reference.

For these three reasons, the code does not compile, and Option D is the correct answer.




PreviousNext

Related