Java OCA OCP Practice Question 1666

Question

What will be printed when the following program is run?.

public class Main {
  public static void main(String[] args) {
    int i = 0;/*from   w  w  w .jav a 2s  . c  o  m*/
    addTwo(i++);
    System.out.println(i);
  }

  static void addTwo(int i) {
    i += 2;
  }
}

Select the one correct answer.

  • (a) 0
  • (b) 1
  • (c) 2
  • (d) 3


(b)

Note

Evaluation of the actual parameter i++ yields 0, and increments i to 1 in the process.

The value 0 is copied into the formal parameter i of the method addTwo() during method invocation.

The formal parameter is local to the method, and changing its value does not affect the value in the actual parameter.

The value of the variable i in the main() method remains 1.




PreviousNext

Related