Java OCA OCP Practice Question 2014

Question

What will the following program print when run?.

public class Main {
  public static void main(String[] args) {
    String str1 = "lower", str2 = "LOWER", str3 = "UPPER";
    str1.toUpperCase();
    str1.replace("LOWER","UPPER");
    System.out.println((str1.equals(str2)) + " " + (str1.equals(str3)));
  }
}

Select the one correct answer.

  • (a) The program will print false true.
  • (b) The program will print false false.
  • (c) The program will print true false.
  • (d) The program will print true true.
  • (e) The program will fail to compile.
  • (f) The program will compile, but throw an exception at runtime.


(b)

Note

The reference value in the reference str1 never changes and it refers to the string literal "lower" all the time.

The calls to toUpperCase() and replace() return a new String object whose reference value is ignored.




PreviousNext

Related