Top 10 Interview Questions in class, object,constructor, method
In this blog lets discuss about the interview questions relevant to class, object,constructor, method
- What is an Object?
Any real world entity is an object. eq. car, laptop, student, employee, pen.
- What is a Class?
A class is a blueprint of an object. A class is a collection of objects with common properties , behavior , relationship and semantics. A collection of student object becomes a Student class and collection of car objects becomes Car class. An object is an instance of a class.
- What are instance variables?
The properties of an object are called instance variables. eg. A Student class can have instance variables as name, studentId. In case of the instance variable, the variable name is same but the values are different for every object.
- What is a static variable?
A static variable is one, which is common to all the objects.Only one copy of the variable is available and shared by all the objects eg. A Student class can the teacher name as a static variable.
In case of static variable, the value is same for all the objects. It can be called using classname.variablename
- What is a Constructor?
A constructor is used to initialize the instance variables. It has the same name as that of the class and does not have a return type.
The syntax for the constructor is access-specifier class-name
- What is a default constructor?
A default constructor is one without parameters
- Can a class have more than one constructor?
Yes. A class can have more than one constructor taking different parameters. This concept is called constructor overloading
- What will happen if I have not added any constructor to my class?
If you have not added any constructor, the compiler will add the default constructor automatically
- Can a constructor be private?
Yes. But you can create the object only in the same class.If needed the object can be returned using a static method.
- Show an example of constructor with and without parameters
- What will be in the first line of any constructor?
The first line of any constructor will have either this() or super() keyword.
- How will you create an object?
Use the new keyword to create an object. The new keyword() is used to allocate memory.
Student student = new Student();
where new Student() is the object,
student is just a reference variable pointing to a student object.
- What is a method?
The methods are operations done on that object. It can be with or without return type and with or without parameter. The syntax for a method is
- What will happen if the instance variable name and the local variable name are same?>
- What is the meaning of this keyword?
The ‘this’ refers to the current object.
An object has state, which is also called as properties. A student object has properties as name, studentId, city, phone,department.A car object has properties like model, brand, price.
An object can have behavior, which is called as methods. A student object can have methods as printDetails(), getGrade(). A car object can have methods like getMileage(), printAccessories()
To learn about the top questions and answers in overloading click here