Skip to content
Sharp

Object-oriented programming · interview question

What does this program print?

What does this code output?Medium
java
public class Main {
    public static void main(String[] args) {
        String a = "hello";
        String b = "hel" + "lo";
        String c = new String("hello");
        System.out.println((a == b) + " " + (a == c) + " " + a.equals(c));
    }
}
Choose one answer
Answer and explanation

Correct answer

A. true false true

Why

String literals and compile-time constant expressions are interned, so a and b reference the same object. new String forces a new object. equals compares characters.

Test yourself on Object-oriented programming

A free test with instant, explained results. No account needed.

Start free test
All Object-oriented programming questions →