Java OCA OCP Practice Question 1012

Question

Choose the correct statement about the following code:

1: interface Shape { 
2:   abstract int getBorders(); 
3: } 
4: abstract class Square implements Shape { 
5:   abstract int getArea(); 
6: } 
7: public class Rectangle extends Square { 
8:   int getArea() { return 6; } 
9: } 
  • A. It compiles and runs without issue.
  • B. The code will not compile because of line 2.
  • C. The code will not compile because of line 4.
  • D. The code will not compile because of line 7.
  • E. It compiles but throws an exception at runtime.


D.

Note

The code fails to compile because Rectangle, the first concrete subclass, doesn't implement getBorders(), which is inherited as an abstract method.

Option D is correct.

Option B is incorrect because there is nothing wrong with this interface method definition.

Option C is incorrect because an abstract class is not required to implement any abstract methods, including those inherited from an interface.

Option E is incorrect because the code fails at compilation-time.




PreviousNext

Related