Java OCA OCP Practice Question 2024

Question

What will the following program print when run?.

public class Main {
 public static void main (String[] args) {
   StringBuilder sb1 = new StringBuilder("WOW");
   StringBuilder sb2 = new StringBuilder(sb1);
   System.out.println((sb1==sb2) + " " + sb1.equals(sb2));
 }
}

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 throws an exception at runtime.


(b)

Note

The references sb1 and sb2 are not aliases.

The StringBuilder class does not override the equals() method, hence the answer is (b).




PreviousNext

Related