Multiple choice technology programming languages

What will this program print out ? class Base{ int value = 0; Base(){ addValue(); } void addValue(){ value += 10; } int getValue(){ return value; } } class Derived extends Base{ Derived(){ addValue(); } void addValue(){ value += 20; } } public class Test { public static void main(String[] args){ Base b = new Derived(); System.out.println(b.getValue()); } }

  1. 10

  2. 20

  3. 30

  4. 40

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

During instantiation of Derived, the Base constructor is called first, which invokes addValue(). Because of dynamic binding, the overridden addValue() in Derived runs, adding 20 to value. Then, the Derived constructor runs and calls addValue() again, adding another 20, resulting in 40.