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));
}
}
- true true
- false true
- true false
- 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
- Which Java keyword, when applied to a class, prevents any other class from…
- What will be the output of the following Java program?
- What is the default value of an int instance variable (field) that is declared…
- What will be the output of the following Java program?
- What will be the output of the following Java program?
- What will be the output of the following Java program?
- What will be the output of the following Java program?
- What will be the output of the following Java program?
- What will be the output of the following Java program?
- What will be the output of the following Java program (default JVM settings)?
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