Given class B, C, D and E below, what gets printed after executing the following
ID: 3858444 • Letter: G
Question
Given class B, C, D and E below, what gets printed after executing the following line of code: B b = new B (): a. C: 40 D: 29 b. C: 38 D: 40 c. C: 12 D: 35 d. None of the above Answer: class E { private int age -12: protected float amount = 35: public String toString() { return "E";} public int getAge{ } { return age: } public void updateAmount (float f) { amount = f: } public void updateAge(int i) (age = i: } } class D extends E (public D() super (): updateAmount(28): } public String toString() (return "D: " + (amount + getAge()): } } Class C extends D { public C() {Explanation / Answer
Answer: b) C:38 D:40
Explanation:
1)Lets see how object C is constructed
a) first E is initiated.
Result: age=12 and amount=35
b) Then D is initiated. updateAmount(28) in D constructor is called.
Result: age=12 and amount=28
c) finally updateAge(10) in C constructor is called
Result:age=10 and amount=28
Therefore: C:38 (10+28)
2) Now lets see how object D is constructed
a) first E is initiated
Result:age=12and amount=35
b)then updateAmount(28) in D constructor is called.
Result: age=12 and amount=28
Therefore D:40 (12+28)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.