Java OCA OCP Practice Question 2748

Question

What is the output of the following code?

public static void main(String args[]) {
    String s = "string 1";
    int i = 5;//from  w  w  w. j a  v a 2 s .com
    someMethod1(i);
    System.out.println(i);
    someMethod2(s);
    System.out.println(s);
}

public static void someMethod1(int i) {
    System.out.println(++i);
}
public static void someMethod2(String s) {
    s = "string 2";
    System.out.println(s);
}
  • a. 5 5 string 2 string 1
  • b. 6 6 string 2 string 2
  • c. 5 5 string 2 string 2
  • d. 6 5 string 2 string 1


d

Note

The i variable in the main is not modified, as it is passed by value.

While the string is passed by reference, the local variable s was modified in the third method, not the one in the main method.




PreviousNext

Related