Java · Question 6 · Medium

What will be the output of the following Java program?

public class Main {
    public static void main(String[] args) {
        String a = "java";
        String b = new String("java");
        System.out.println((a == b) + " " + a.equals(b));
    }
}
  1. true true
  2. false true
  3. true false
  4. false false
Answer and explanation

Answer: B. false true

Explanation: new String("java") creates a separate object, so == (reference comparison) is false. equals() compares the characters, so it is true.

Previous: What is the default value of an int instance variable (field) that is declared… · Next: What will be the output of the following Java program?

Practise java

Try a fresh set of java questions with an explanation after every answer, free and without signing up: Java practice. Or sit a full placement mock test.

More java questions

More technical & programming practice

Python · Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL · Operating Systems · Computer Networks

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge