Multiple choice technology programming languages

What is the output of the following piece of code? if(" String ".trim() == "String") System.out.println("Welcome"); else System.out.println("Good Bye");

  1. The code will compile and print "Welcome"

  2. The code will compile and print "Good Bye"

  3. The code will cause a compiler error

  4. None of the Above

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The trim() method returns a new String object, not the same literal reference. Since ' String '.trim() creates a new String object and 'String' is a literal, they are different objects with the same content. Using == compares references, not content, so the comparison is false, printing 'Good Bye'.