Multiple choice technology programming languages

Click the Exhibit button. 1. public class Test { 2. int x= 12; 3. public void method(int x) { 4. x+=x; 5. System.out.println(x); 6. } 7. } Given: 34. Test t = new Test(); 35. t.method(5); What is the output from line 5 of the Test class?

  1. 5

  2. 10

  3. 12

  4. 17

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

The parameter x in the method shadows the instance variable. Inside method(), x refers to the parameter (value 5), not the instance field (value 12). So x+=x means 5+=5, which equals 10.