Java OCA OCP Practice Question 3231

Question

What will be written to the standard output when the following program is executed?

public class Main {
  int a;/*from  ww w.ja  v a  2s.co  m*/
  int b;
  public void f() {
    a = 0;
    b = 0;
    int[] c = { 0 };
    g(b, c);
    System.out.println(a + " " + b + " " + c[0] + " ");
  }

  public void g(int b, int[] c) {
    a = 1;
    b = 1;
    c[0] = 1;
  }

  public static void main(String[] args) {
    Main obj = new Main();

    obj.f();
  }
}

Select the one correct answer.

  • (a) 0 0 0
  • (b) 0 0 1
  • (c) 0 1 0
  • (d) 1 0 0
  • (e) 1 0 1


(e)

Note

Method g() modifies the field a.

Method g() modifies the parameter b, not the field b, since the parameter declaration shadows the field.

Variables are passed by value, so the change of value in parameter b is confined to the method g().

Method g() modifies the array whose reference value is passed as a parameter.

Change to the first element is visible after return from the method g().




PreviousNext

Related