List the output of this program: class A { public int n1 = 1; public int n2 = 2;
ID: 3831690 • Letter: L
Question
List the output of this program: class A { public int n1 = 1; public int n2 = 2; A() { n1= 4; n2 = 5;} A(int b, int a) { n1 = b; n2= a;} public void print() { System.out.println (n1 + + n2);} } class B extends A { public int n1 ; public int n2; private static int k = 0; B() { super(); k = k+1; n1= 4; n2 = 5;} B(int a, int b) { super(a,b); k = k+2; n1 = a; n2 = b;} public void print(); super.print(); System.out.println(n1 + "," + n2 + "," + k); } public int n(int b, int c) { int a; a = 3; b = k; n2 = c + a + b; return (b); } public static void main(Strin1) args) { B b1 = new B(); b1.n2 = b1.f1(b1.n2, b1.n1); b1. print(); B b2 = new B(2,3); b2.n1 = b2.f1 (b1.n1, b2.n2); b2. print(); b1 =b2; b1. print(); } }Explanation / Answer
B b1 = new B(); k=1, n1= 4, n2 = 5 , A(n1 = 4, n2 = 5)
b1.n2 = b1.f1(b1.n2, n1.n1)
=> f1(5, 4)
b = k=1
n2 = 4 + 3 + 1 = 8
return 1
b1.n2 = 1
So, b1 = k=1, n1= 4, n2 = 1 , A(n1 = 4, n2 = 5)
b1.print() : super.print() => 4, 5
4, 1, 1
Output:
4, 5
4, 1, 1
Now,
B b2 = new B(2, 3); k = 2, n1 = 2, n2 = 3, A(2, 3)
b2.n1 = b2.f1(b1.n1, b2.n2)
f1(2, 3)
b = k = 2
a = 3
n2 = 3 + 3 + 2=8
return 2
b2.n1 = 2
So, b2: k = 2, n1 = 2, n2 = 8, A(2, 3)
b2.print:
2, 3
2, 8, 2
b1 = b2
b1.print()
2, 3
2, 8, 2
So, final output:
4, 5
4, 1, 1
2, 3
2, 8, 2
2, 3
2, 8, 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.