Java OCA OCP Practice Question 335

Question

What is the result of the following program?

1:public class Main { 
2:   public static void addToInt(int x, int v) { 
3:     x = x + v; //from  ww w  .  j  a v  a 2 s  . co m
4:   } 
5:   public static void main(String[] args) { 
6:     int a = 15; 
7:     int b = 10; 
8:     Main.addToInt(a, b); 
9:     System.out.println(a);   
10:  } 
11:} 
  • A. 10
  • B. 15
  • C. 25
  • D. Compiler error on line 3.
  • E. Compiler error on line 8.
  • F. None of the above.


B.

Note

The code compiles successfully, so options D and E are incorrect.

The value of a cannot be changed by the addToInt() method, no matter what the method does, because only a copy of the variable is passed into the parameter x.

Therefore, a does not change and the output on line 9 is 15.




PreviousNext

Related