Here, we have listed topic-wise the most 50+ basic core java interview questions with the best possible answers for 2021.
These basic java interview questions are often asked from freshers and beginners in any java technical interview round.
We have also covered the coding programming questions with answers that will boost up your knowledge to crack the interview round.
Basic Interview Questions based on Class and Object in Java
1. What is an Object in Java?
Ans: An object in Java is any real-world thing that has properties and actions. In other words, an entity that has state, behavior, and identity is known as object.
2. What are the characteristics of an object in Java?
Ans: An object has three characteristics that are as follows:
a) State: It represents the properties of an object. It is represented by instance variable/attribute of an object.
b) Behavior: It represents functionality or actions. It is represented by methods/functions in Java.
c) Identity: It represents the unique name of an object. It differentiates one object from the other. The unique name of an object is used to identify the object.
3. What is a class in Java?
Ans: A class is a user-defined data type that acts as a template for creating objects of the identical type. It represents the common properties and actions (functions) of an object.
4. Why do we need a class in Java?
Ans: We need a class for packing together a group of logical related data items (fields) and methods (functions) that work on them.
5. What is an attribute?
Ans: A variable that is defined in a class is called attribute.
6. What is instantiating of an object?
Ans: The process of creating an object of a particular class is called instantiating of an object.
7. Why is an object called an instance of a class?
Ans: An object is called an instance of a class because every object created from a class gets its own instances of variables defined inside a class. Multiple objects can be constructed from the same class.
8. Is a class a data type in Java?
Ans: Yes, a class is also considered a user-defined data type because a user defines a class.
9. Can a class represent an object?
Ans: No, a class never represents an object. It represents data and actions that an object will have.
10. Which of the following are valid class names?
Student, _Student, class, true, _, CollegeStudent, schoolName, null, _class, Class99
Ans: Student, _Student, _, CollegeStudent, _class, Class99 are the valid class names. class, true, null, schoolName are not valid class names.
11. What is the difference between class and object in Java?
Ans: The differences between class and object are as follows:
- A class is a user-defined data type whereas an object is an instance of class data type.
- A class represents the type of objects whereas an object represents particular instances of things.
- Classes do not occupy memory location but objects occupy memory location.
- Classes cannot be manipulated due to not available in the memory location but objects can be manipulated.
12. What is the meaning of creating an object in Java?
Ans: Creating an object means allocating memory to store the data of variables temporarily. i.e. we create an object of a class to store data temporarily.
13. How to create an object in Java?
Ans: In Java, an object of a class is created using the new keyword in three steps. They are as follows.
- Declaration of a reference variable.
- Creation of an object.
- Linking the object and reference variable.
14. What is the general syntax to create an object of class?
Ans: The general syntax to create an object of a class is as follows:
Classname object_reference_variable = new Classname();
15. Consider a class whose name is College and object reference variable myCollege. It is declared like this:
Answers the following questions:
a) Is an object of class College created?
b) What is the meaning of this statement: College myCollege;?
Ans a. No, an object of class College is not created still. It only refers to an object.
Ans b. The statement College myCollege; tells the JVM to allocate memory space for a reference variable and names that reference variable myCollege. The reference variable is of type College.
16. What is object reference in Java?
Ans: An object reference is a unique hexadecimal number that represents a memory address of the object. When an object is created, a new reference number is allocated to it.
17. Where the address of an object is stored in the memory?
Ans: The address of an object is stored in the object reference variable in the stack memory.
18. What is an object reference variable in Java?
Ans: Object reference variable is a variable that stores the address of an object in the stack memory.
19. Suppose we have created an object of a class Student like this: new Student();
Answers the following questions:
a) Is an object of class Student created?
b) What is the meaning of this statement: new Student();
Ans a: Yes, an object of class Student is created.
Ans b: The statement new Student(); tells the JVM to allocate memory space for a new Student object on the heap.
20. For which purpose we create an object of class in Java?
Ans: We create an object of a class in java to store data temporarily in Java application and to access members of any particular class.
21. How many ways to create object in Java?
Ans: There are several ways to create an object of class in Java. They are as follows.
- Using the new keyword
- Using Class.forName
- Using Clone.
- Using Object Deserialization.
- Using ClassLoader.
22. What is new in Java?
Ans: In Java, a new is a special keyword that is used to create an object of the class. It allocates the memory to store an object during runtime and returns a reference to it.
23. What is void in Java?
Ans: Void is a keyword that indicates that this method does not return any data back to the class of an object.
24. What is the difference between reference variable and variable?
Ans: Reference variable holds the address of an object whereas a variable holds data.
25. What is object declaration in Java?
Ans: The process of defining a variable along with its data type and name is called the declaration of state of an object or simply object declaration.
26. What is object initialization in Java?
Ans: The process of assigning a value of the variable is called initialization of state of an object or simply object initialization.
27. How to initialize the state of object in Java?
Ans: There are three ways by which we can initialize the state of an object. In other words, we can initialize value of variables in Java by using three ways. They are as follows:
- By using constructor
- By using a reference variable
- By using a method.
28. What is an anonymous object in Java?
Ans: An object which has no reference variable is called anonymous object in Java
29. Write the syntax to create an anonymous object in Java?
Ans: The general syntax to create an anonymous object is as follows:
30. What is an anonymous class in Java?
Ans: A class that does not have a name is called anonymous class in Java.
31. When to use an anonymous object?
Ans: Anonymous object creation is useful when it is not used more than once.
32. Define concrete class in Java.
Ans: A class whose object can be created and whose all methods have body is called concrete class.