Java MCQs – Classes and Objects

11.) What is the main difference between a class and an object in Java?

A) A class is a blueprint, while an object is an instance of a class.
B) A class is an instance of an object, while an object is a blueprint.
C) A class and an object are the same thing.
D) A class defines attributes, while an object defines methods.

Answer: Option A

Explanation: A class defines the structure (attributes and methods) for creating objects, while an object is a specific instance created from that class.

12.) Which of the following best describes the concept of data abstraction in Java?

A) Hiding the internal implementation details of an object
B) Accessing object attributes using the “this” keyword
C) Creating a new class based on an existing class
D) Using “get” and “set” methods for data manipulation

Answer: Option A

Explanation: Data abstraction involves presenting only essential information and hiding the unnecessary details of an object’s implementation.

13.) Which section of a class contains the class’s attributes and instance variables?

A) Method section
B) Object section
C) Instance section
D) Field section

Answer: Option D

Explanation: The field section of a class contains the class’s attributes and instance variables.

14.) What is the purpose of the “toString” method in Java?

A) To convert a class to a string representation
B) To convert an object to an integer
C) To compare two objects for equality
D) To create a new object from an existing one

Answer: Option A

Explanation: The “toString” method is used to provide a string representation of an object for display or printing.

15.) What is a default constructor in Java?

A) A constructor with no parameters that are provided by the compiler
B) A constructor that is automatically generated for static methods
C) A constructor that creates a new instance of a class
D) A constructor with the “default” access modifier

Answer: Option A

Explanation: A default constructor is automatically generated by the compiler if no constructor is explicitly defined in a class.

16.) Which keyword is used to access the superclass’s members (fields and methods) from a subclass?

A) super
B) this
C) parent
D) base

Answer: Option A

Explanation: The “super” keyword is used to access the superclass’s members and methods from a subclass.

17.) In Java, can an object of a subclass be assigned to a variable of the superclass type?

A) No, it will result in a compilation error.
B) Yes, but only if the subclass overrides superclass methods.
C) Yes, this is allowed and is called polymorphism.
D) Yes, but only if the subclass has the same name as the superclass.

Answer: Option C

Explanation: Polymorphism allows an object of a subclass to be treated as an object of the superclass type, enabling flexibility and code reuse.

18.) Which access modifier makes a class member accessible only within the same class?

A) private
B) protected
C) public
D) default (package-private)

Answer: Option A

Explanation: The “private” access modifier restricts access to the member to within the same class only.

19.) What is the purpose of a “getter” method in Java?

A) To modify the value of an attribute
B) To provide access to the value of an attribute
C) To create a new instance of a class
D) To override superclass methods

Answer: Option B

Explanation: A “getter” method is used to retrieve the value of an attribute (instance variable) from an object.

20.) What is the role of the “equals” method in Java?

A) To compare the memory addresses of two objects
B) To compare the attributes of two objects for equality
C) To create a new instance of a class
D) To override superclass methods

Answer: Option B

Explanation: The “equals” method is used to compare the attributes (contents) of two objects to determine if they are equal.

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework