No comments yet

Top 15 interview questions in class, object, constructor, method

[Social9_Share type="horizontal"]

Top 15 Interview Questions in class, object,constructor, method

In this blog lets discuss about the interview questions relevant to class, object,constructor, method

  1. What is an Object?
  2. Any real world entity is an object. eq. car, laptop, student, employee, pen.
    java 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()

  3. What is a Class?
  4. 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.

    java class

  5. What are instance variables?
  6. 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.

  7. What is a static variable?
  8. 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

  9. What is a Constructor?
  10. 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

    Example

  11. What is a default constructor?
  12. A default constructor is one without parameters

  13. Can a class have more than one constructor?
  14. Yes. A class can have more than one constructor taking different parameters. This concept is called constructor overloading

  15. What will happen if I have not added any constructor to my class?
  16. If you have not added any constructor, the compiler will add the default constructor automatically

    Become an expert Java Developer

  17. Can a constructor be private?
  18. Yes. But you can create the object only in the same class.If needed the object can be returned using a static method.

  19. Show an example of constructor with and without parameters
  20. Example

  21. What will be in the first line of any constructor?
  22. The first line of any constructor will have either this() or super() keyword.

  23. How will you create an object?
  24. Use the new keyword to create an object. The new keyword() is used to allocate memory.
    Example
    Student student = new Student();
       where new Student() is the object,
         student is just a reference variable pointing to a student object.

  25. What is a method?
  26. 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
    access-specifier return-type method-name(parameter-list)
    Example

  27. What will happen if the instance variable name and the local variable name are same?>
  28. In this case the local variable hides the instance variable. the instance variable will take the default values of the data type. Use ‘this’ keyword to prevent it

  29. What is the meaning of this keyword?
  30. ‘this’ refers to the current object.

    To learn about the top questions and answers in overloading click here

    Happy Learning

Post a comment