Java toString() Quiz

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) String

The 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) [email protected] 101
b) Test [email protected]
c) [email protected] [email protected]
d) t1 s1

View Answer Answer:- a) [email protected] 101

In 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) [email protected] 101
b) [email protected] 101
c) [email protected] 10
d) Exception

View Answer Answer:- c) [email protected] 10

In 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 [email protected]

Q4)

public class Test {
  public static void main(String[] args) {
     Test t1 = new Test();
     System.out.println(t1.toString());
  }
}

Output:- [email protected]

Now, what will be the output of the below statement?

System.out.println(t1);

a) Test
b) [email protected]
c) t1
d) None of these

View Answer Answer:- b) [email protected]

Whenever 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) Math

The 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) [email protected]

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) [email protected]
b) 50
c) [email protected]
d) [email protected]

View Answer Answer:- b) 50

The 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 you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *