ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
public class Main {
    public static void main(String[] args) {
        String a = "Hello";
        String b = "hello";
        System.out.print(a.equals(b) + " " + a.equalsIgnoreCase(b) + " " + (a == b));
    }
}
  1. A. false true true
  2. B. true false false
  3. C. true true false
  4. D. false true false

Correct answer: D. false true false

equals() is case-sensitive, so "Hello".equals("hello") is false. equalsIgnoreCase() ignores case, so it's true. The literals differ in case, so even the string pool stores them as distinct entries, making a == b false too.

More OOP with C++ and Java questions

Exams that ask this topic