Consider the following program:
class Base {// w ww .ja va2 s . co m public Base() { System.out.print("Base "); } public Base(String s) { System.out.print("Base: " + s); } } class Derived extends Base { public Derived(String s) { super(); // Stmt-1 super(s); // Stmt-2 System.out.print("Derived "); } } public class Main { public static void main(String []args) { Base a = new Derived("Hello "); } }
Select three correct options from the following list:
a)removing only Stmt-1 will make the program compilable and it will print the following: Base Derived b)removing only Stmt-1 will make the program compilable and it will print the following: Base: Hello Derived c)removing only Stmt-2 will make the program compilable and it will print the following: Base Derived d)removing both Stmt-1 and Stmt-2 will make the program compilable and it will print the following: Base Derived e)removing both Stmt-1 and Stmt-2 will make the program compilable and it will print the following: Base: Hello Derived
b) c) d)
option a) If you remove Stmt-1
, a call to super(s) will result in printing Base: Hello, and then constructor of the Derived class invocation will print Derived.
hence it does not print: Base Derived.
option e) If you remove Stmt-1
and Stmt-2
, you will get a compilable program but it will result in printing: Base Derived and not Base: Hello Derived.