Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 212

Given:

1. abstract class Shape {

2. Shape ( ) { System.out.println ("Shape"); }

3. protected void area ( ) { System.out.println ("Shape"); }

4. }

5.

6. class Square extends Shape {

7. int side;

8. Square int side {

9. /* insert code here */

10. this.side = side;

11. }

12. public void area ( ) { System.out.println ("Square"); }

13. }

14. class Rectangle extends Square {

15. int len, br;

16. Rectangle (int x, int y) {

17. /* insert code here */

18. len = x, br = y;

19. }

20. void area ( ) { System.out.println ("Rectangle"); }

21. }

Which two modifications enable the code to compile?

abstract

    Correct Answer: B, D

    For the code to compile correctly, two modifications are necessary. First, at line 9, 'public' should be inserted to declare the constructor of the 'Square' class correctly. Without the correct visibility, the constructor will not be recognized properly. Second, at line 17, 'super(); super.side = x;' should be inserted to ensure that the 'Rectangle' class correctly calls the parent constructor and properly initializes 'side' from its parent class 'Square', providing the necessary setup for the inherited attributes.

Discussion
PasciOptions: DE

strange code at line 8, maybe shoud be: Square(int side) {...} then the correct change would be: - line 17: insert super(x); - line 20: insert public

steefaandOptions: CF

C and F are correct.

iSnoverOptions: CF

The answer is CF, in line 17 "insert super(x);" and in line 20 "insert public".

WilsonKKerllOptions: DE

- line 2, void Shape(){...} - line 17, Rectangle(int x, int y){super(x);....} - line 20, public void area() {....}

Abdullah_Rahahleah

super keywork is mandatory if the default constructor for the parent overloaded