Java MCQ – Home
Java Basic MCQ
➤ Java Hello World MCQ
➤ Find Java Keywords
➤ Java Identifier Quiz
➤ Java Data Types Quiz
➤ If-Else MCQ in Java-1
➤ If-Else MCQ in Java-2
Object class MCQ
➤ Object Class Quiz
➤ equals() Method Quiz
➤ Hashcode Value Quiz
➤ toString() Method Quiz
➤ Clone() Method Quiz
Multithreading MCQ
➤ Define a Thread-1
➤ Define a Thread-2
➤ Get/set ThreadName
➤ Thread State MCQ
➤ Thread Priority MCQ
➤ Yield(), join() & sleep()
➤ Synchronization MCQ
➤ Interthread Comms
➤ Deadlock, Daemon
Exception Handling
➤ Exception Handling-1
➤ Exception Handling-2
➤ Exception Handling-3
➤ Java try-catch MCQ-1
➤ Java try-catch MCQ-2
➤ Java try-catch MCQ-3
➤ Nested try-catch MCQ
➤ throw Keyword MCQ
➤ finally Block MCQ-1
➤ finally Block MCQ-2
➤ throws Keyword MCQ
Generics MCQ
➤ Java Generics Quiz-1
➤ Java Generics Quiz-2
➤ Java Generics Quiz-3
Collection Framework
➤ Collections Quiz-1
➤ Collections Quiz-2
➤ ArrayList MCQ-1
➤ ArrayList MCQ-2
➤ LinkedList MCQ
➤ Vector Stack MCQ
➤ Java Cursors MCQ
➤ Java TreeSet MCQ
➤ TreeSet & Comparator
Java.lang.Object.equals() Method Quiz | Prerequisite:- Equals Method in Java | Another Quiz on Object class:- Object Class Quiz Set-1, Object Class Quiz Set-2, Java Hashcode Value Quiz, Java Equals() Method Quiz
Q1) Find the output of the below Java program?
public class Test {
public static void main(String[] args) {
Test t1 = new Test();
Test t2 = new Test();
System.out.println(t1.equals(t2));
}
}
a) true
b) false
c) null
d) error
View Answer
Answer:- b) falseQ2) Find the output of the below Java program?
public class Test {
public static void main(String[] args) {
Test t1 = new Test();
Test t2 = t1;
System.out.println(t1.equals(t2));
}
}
a) true
b) false
c) null
d) error
View Answer
Answer:- a) trueQ3) Find the output of the below Java program?
public class Test {
public static void main(String[] args) {
int x = 10;
int y = 10;
System.out.println(x.equals(y));
}
}
a) true
b) false
c) error
d) Exception
View Answer
Answer:- c) errorThe equals() method of java.lang.Object class is a non-static method and to call equals() method object must be there.
Q4) Find the output of the below Java program?
class Student {
int id;
Student(int id) {
this.id = id;
}
}
public class Test {
public static void main(String[] args) {
Student s1 = new Student(1001);
Student s2 = new Student(1001);
System.out.println(s1.equals(s2));
}
}
a) true
b) false
c) error
d) Exception
View Answer
Answer:- b) falseEvery class is a direct or indirect subclass of java.lang.Object class. Since the equals() method is not defined in the Student class therefore it is executed from java.lang.Object class. Object class equals() method is implemented to compare objects based on their references. The s1 and s2 objects have different reference hence it returns false.
Q5) Find the output of the below Java program?
class Student {
int id;
Student(int id) {
this.id = id;
}
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(obj instanceof Student) {
Student s = (Student)obj;
return this.id == s.id;
}
return false;
}
}
public class Test {
public static void main(String[] args) {
Student s1 = new Student(1001);
Student s2 = new Student(1001);
System.out.println(s1.equals(s2));
}
}
a) true
b) false
c) error
d) Exception
View Answer
Answer:- a) trueThe equals() method is overridden in the Student class. Hence whenever the equals() method is called on the Student object then it is executed from the Student class rather than java.lang.Object class, which is meant for content comparison.
Q6) Whenever we are overriding the equals() method then it is also recommended to override?
a) toString()
b) hashCode()
c) clone()
d) getClass()
View Answer
Answer:- a) hashCode()Two equivalent objects should have the same hashcode. We should follows this relation between hashCode() and equals() method. Hence it is recommended to override the hashCode() method. Based on which parameter we override equals() method, it is highly recommended to use the same parameter while overriding hashCode() also.
Q7) Find the output of the below Java program?
public class Test {
public static void main(String[] args) {
System.out.println(null.equals(null));
}
}
a) true
b) false
c) error
d) Exception
View Answer
Answer:- c) errorWe can’t dereference null, else we will get compile-time error: <null> cannot be dereferenced.
Q8) Find the output of the below Java program?
class Student {
int id;
Student(int id) {
this.id = id;
}
}
public class Test {
public static void main(String[] args) {
Student s1 = new Student(1001);
System.out.println(null.equals(s1));
}
}
a) true
b) false
c) error
d) Exception
View Answer
Answer:- c) errorIt is similar to the previous question. We can’t dereference null, else we will get compile-time error: <null> cannot be dereferenced.
Q9) Find the output of the below Java program?
class Student {
int id;
Student(int id) {
this.id = id;
}
}
public class Test {
public static void main(String[] args) {
Student s1 = new Student(1001);
System.out.println(s1.equals(null));
}
}
a) true
b) false
c) error
d) Exception
View Answer
Answer:- b) falseThe reference of s1 object is not null and hence we get results as false.
Q10) Find the output of the below Java program?
class Student {}
class Collage {}
public class Test {
public static void main(String[] args) {
Student s1 = new Student();
Collage c1 = new Collage();
System.out.println(s1.equals(c1));
}
}
a) true
b) false
c) error
d) Exception
View Answer
Answer:- b) falseFor two incompatiable objects java.lang.equals() method always returns false.
Q11) Find the output of the below Java program?
class Student {}
public class Test {
public static void main(String[] args) {
Student s1 = null;
Student s2 = new Student();
System.out.println(s1.equals(s2));
}
}
a) true
b) false
c) error
d) Exception
View Answer
Answer:- d) ExceptionThe s1 referenced to null literal and equals() method can’t invoke it. We will get NullPointerException.
Q12) Find the output of the below Java program?
class Student {}
public class Test {
public static void main(String[] args) {
Student s1 = null;
Student s2 = null;
System.out.println(s1.equals(s2));
}
}
a) true
b) false
c) error
d) Exception
View Answer
Answer:- d) ExceptionSimilar to the previous one.
Q13) Find the output of the below Java program?
class Student {}
public class Test {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = null;
System.out.println(s1.equals(s2));
}
}
a) true
b) false
c) error
d) Exception
View Answer
Answer:- b) falseIf you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!