Java OCA OCP Practice Question 1244

Question

How many lines of the following program contain compilation errors?

package mypkg; //from  w w w.j  a  va 2s . com
class Shape { 
        private String name; 
        public Shape(String name) {this.name = name;} 
} 
public class Rectangle extends Shape { 
        public Rectangle(String movie) {} 
        public static void main(String[] showing) { 
           System.out.print(new Rectangle("T").name); 
        } 
} 
  • A. None
  • B. One
  • C. Two
  • D. Three


C.

Note

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

This code does not compile for two reasons.

First, the name variable is marked private in the Shape class, which means it cannot be accessed directly in the Rectangle class.

Next, the Rectangle class defines a constructor that is missing an explicit super() statement.

Since Shape does not include a no-argument constructor, the no-argument super() cannot be inserted automatically by the compiler without a compilation error.

For these two reasons, the code does not compile, and Option C is the correct answer.




PreviousNext

Related