Java OCA OCP Practice Question 2010

Question

What will be the result of attempting to compile and run the following program?.

public class Main {
  public static void main(String[] args) {
    String s = "ab" + "12";
    String t = "ab" + 12;
    String u = new String("ab12");
    System.out.println((s==t) + " " + (s==u));
  }
}

Select the one correct answer.

  • (a) The program will fail to compile.
  • (b) The program will print false false, when run.
  • (c) The program will print false true, when run.
  • (d) The program will print true false, when run.
  • (e) The program will print true true, when run.


(d)

Note

The constant expressions "ab" + "12" and "ab" + 12 will, at compile time, be evaluated to the string-valued constant "ab12".

Both variables s and t are assigned a reference to the same interned String object containing "ab12".

The variable u is assigned a new String object, created by using the new operator.




PreviousNext

Related