Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is the output of following program: public class Test { public static void

ID: 3849440 • Letter: W

Question

What is the output of following program: public class Test { public static void main(String[] args){ B b = new B(2): b. method B(): } } class A{ public A(){ public A (){ System out println("A's constructor is executed"): } public void method(){ System out println("methodA is executed"): } public void methodAB(){ System out.println("A's methodAB is executed"): } } class B extends A { private int num = 0: public B(){ super(): System out.println("B's constructor is executed"): public B(int n) { num = n: System out printin("B's constructor is executed"): } public void method(){ System.out println("num is"+ num): System out println("methodB is executed"): } public void methodAB() { System.out printin(B's methodAB is executed"): } Compiling time error because both "methodB" is not defined in Class A B s constructor is executed num is 0 methodB is executed num is 2 methodB is executed

Explanation / Answer

Statement: B b = new B(2);

1.    Initially B class Parameterized Constructor gets invoked which in-turn calls Base class default Constructor automatically.

   Hence output for these constructor calls are:
      
       A's Constructor is executed
       B's Constructor is executed

      
   Value 2 is assigned to variable num in B class Parameterized constructor.
      
  
2.   Statement:   b.methodB();
  
   This statement invoked methodB of class B and prints the following output:
  
       num is 2
       methodB is executed

      
Hence the final output is:
  
       A's Constructor is executed
       B's Constructor is executed
       num is 2
       methodB is executed

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote