Boolean Expressions

AP Computer Science A· difficulty 3/5

String s1 = new String("hi");
String s2 = new String("hi");
if (s1 == s2) {
  System.out.println("A");
} else if (s1.equals(s2)) {
  System.out.println("B");
} else {
  System.out.println("C");
}

What is printed?

  • A

    B

    check_circle
  • B

    A

  • C

    C

  • D

    A B

Explanation

For Strings created with new, == compares references (different objects, false). .equals compares contents ("hi" equals "hi", true). So "B" is printed.

Want 10 more like this — adaptive to your weak spots?

Related questions