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
The toString() method of the Object class in Java is used to retrieve object information in string form. Also see:- Object Class Quiz
Q1) What is the return type of the toString() method in java.lang.Object class?
a) Object
b) String
c) void
d) Integer
View Answer
Answer:- b) StringThe prototype of java.lang.Object.toString() method is:- public String toString()
Q2) Which could be the possible output of the below Java program?
class Student {
int id;
Student(int id) {
this.id = id;
}
@Override
public String toString(){
return ""+ id;
}
}
public class Test {
public static void main(String[] args) {
Test t1 = new Test();
Student s1 = new Student(101);
System.out.println(t1+" "+s1);
}
}
a) Test@4617c264 101
b) Test Student@4617c264
c) Test@4617c264 Student@6427c652
d) t1 s1
View Answer
Answer:- a) Test@4617c264 101In Student class toString() method is overridden hence it returns a value based on the object state. But in the Test class toString() method is not overridden hence it is executed from java.lang.Object class.
Q3) Which could be the possible output of the below Java program?
class Student {
int id;
Student(int id) {
this.id = id;
}
@Override
public String toString(){
return ""+ id;
}
}
public class Test {
@Override
public int hashCode() {
return 101;
}
public static void main(String[] args) {
Test t1 = new Test();
Student s1 = new Student(101);
System.out.println(t1+" "+s1);
}
}
a) Test@4617c264 101
b) Test@101 101
c) Test@65 10
d) Exception
View Answer
Answer:- c) Test@65 10In Student class toString() method is overridden hence it return a value based on the object state. But in the Test class toString() method is not overridden hence it is executed from java.lang.Object class.
The toString() method of Object class internally calling hashCode() method. Since the hashCode() method is overridden in the Test class therefore it is executed from the Test class. The hashCode() method of Test class return 101, which is converted to hexadecimal format by the toString() method of Object class. Finally, we get the result as Test@65
Q4)
public class Test {
public static void main(String[] args) {
Test t1 = new Test();
System.out.println(t1.toString());
}
}
Output:- Test@d716361
Now, what will be the output of the below statement?
System.out.println(t1);
a) Test
b) Test@d716361
c) t1
d) None of these
View Answer
Answer:- b) Test@d716361Whenever we call print()/println() method to display object then internally it called toString() method.
Q5) Which of the following class doesn’t override the toString() method?
a) String, StringBuffer, & StringBuilder
b) Integer, Long, Float, & Double
c) ArrayList, LinkedList, HashSet & HashMap
d) Math
View Answer
Answer:- d) MathThe Math class contains only static members, but the toString() method is a non-static method.
Q6) What is the output of the below Java program?
class Student {
int id;
String name;
String course;
public Student(int id, String name, String course) {
this.id = id;
this.name = name;
this.course = course;
}
@Override
public String toString() {
return "Student [id=" + id +
", name=" + name +
", course=" + course + "]";
}
}
public class Test {
public static void main(String[] args) {
Student s1 = new Student(1234, "Sophia", "Java");
System.out.println(s1);
}
}
a) Student [id=1234, name=Sophia, course=Java]
b) [id=1234, name=Sophia, course=Java]
c) [1234, Sophia, Java]
d) Student@ee7d9f1
View Answer
Answer:- a) Student [id=1234, name=Sophia, course=Java]The Math class contains only static members, but the toString() method is a non-static method.
Q7) Find the output of the below Java program?
class A {
int x;
A(int x) {
this.x = x;
}
@Override
public int hashCode() {
return x * 100;
}
@Override
public String toString() {
return "" + x;
}
}
public class Test {
public static void main(String[] args) {
A a = new A(50);
System.out.println(a);
}
}
a) A@100
b) 50
c) A@50
d) A@ee7d9f1
View Answer
Answer:- b) 50The toString() method is overriden in the A class hence hashCode() method won’t get any chance to execute.
If 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!