Java OCA OCP Practice Question 2018

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 = "hello";
    StringBuilder sb = new StringBuilder(s);
    sb.reverse();//w  w  w. j  a  v a2 s.  com
    if (s == sb) System.out.println("a");
    if (s.equals(sb)) System.out.println("b");
    if (sb.equals(s)) System.out.println("c");
  }
}

Select the one correct answer.

  • (a) The code will fail to compile because the constructor of the String class is not called properly.
  • (b) The code will fail to compile because the expression (s == sb) is illegal.
  • (c) The code will fail to compile because the expression (s.equals(sb)) is illegal.
  • (d) The program will print c, when run.
  • (e) The program will throw a ClassCastException, when run.


(b)

Note

The code will fail to compile since the expression (s == sb) is illegal.

It compares references of two classes that are not related.




PreviousNext

Related