Multiple choice technology

Given the following code how could you invoke the Base constructor that will print out the string "base constructor"; class Base{ Base(int i){ System.out.println("base constructor"); } Base(){ } } public class Sup extends Base{ public static void main(String argv[]){ Sup s= new Sup(); //One } Sup() { //Two } public void derived() { //Three } }

  1. On the line After //One put Base(10);

  2. On the line After //One put super(10);

  3. On the line After //Two put super(10);

  4. On the line After //Three put super(10);

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

To call a specific superclass constructor from a subclass constructor, use super(args) as the FIRST statement in the subclass constructor body. Option C places super(10) after //Two which is inside the Sup() constructor, correctly invoking Base(int i).

AI explanation

To invoke the Base constructor that will print out the string "base constructor", you need to call the appropriate constructor from the Sup class.

In the given code, the Sup class does not have any explicitly defined constructors. Therefore, it will use the default constructor provided by the compiler.

To invoke the Base constructor with the string "base constructor", you need to add a call to the super class constructor in the Sup class constructor.

Looking at the options:

A) On the line after //One put Base(10);

  • This option is incorrect because it calls the Base constructor with an integer argument, but the Base class does not have a constructor that accepts an integer argument.

B) On the line after //One put super(10);

  • This option is incorrect because it calls the super class constructor with an integer argument, but the Base class does not have a constructor that accepts an integer argument.

C) On the line after //Two put super(10);

  • This option is correct. It calls the super class constructor without any arguments, which matches the default constructor defined in the Base class. This will invoke the Base constructor that prints out "base constructor".

D) On the line after //Three put super(10);

  • This option is incorrect because the Sup class does not have a method named derived().

Therefore, the correct answer is option C.