Core Java Interview questions


Total available count: 646
Subject - Java Technologies
Subsubject - Core Java

What is returned by the method toString() in java?

The method toString() is used to return a string representation of an object. The toString() method is internally invoked by the java compiler when an object is printed.

Let's see an example of the

class SlightBookCourse{  
 String Faculty;  
 String Course;
 String Duration;  
   
  
 SlightBookCourse(String Faculty, String Course, String Duration){  
 this.Faculty=Faculty;  
 this.Course=Course;  
 this.Duration=Duration;  
 }  
   
 public String toString(){//overriding the toString() method  
  return Faculty+" "+Course+" "+Duration;  
 }  
 public static void main(String args[]){  
   SlightBookCourse C1=new SlightBookCourse("Bhaskar","ReactJS","3 Months");  
   SlightBookCourse C2=new SlightBookCourse("Vijay","SQL Server","6 Months");  
     
   System.out.println(s1);//compiler writes here s1.toString()  
   System.out.println(s2);//compiler writes here s2.toString()  
 }  
}  

toString() method.




Next 5 interview question(s)

1
The method finalize() throws which exception?
2
The method clone() throws which exception?
3
How object can be created using newinstance() in java?
4
How object can be created using Class.forName() in java?
5
What is the difference between class and object in java?