Java MCQs – Encapsulation and abstraction

11.) Which modifier restricts access to class members within the same class?

A) public
B) protected
C) private
D) static

Answer: Option C

Explanation: The “private” modifier restricts access to class members within the same class, promoting encapsulation.

12.) What does an abstract method lack?

A) Implementation
B) Access modifier
C) Return type
D) Parameter

Answer: Option A

Explanation: An abstract method does not have a concrete implementation in the superclass and must be overridden by subclasses.

13.) What is the main difference between encapsulation and abstraction in Java?

A) Encapsulation focuses on hiding data, while abstraction focuses on hiding implementation details.
B) Encapsulation focuses on hiding implementation details, while abstraction focuses on hiding data.
C) Encapsulation and abstraction are the same concepts.
D) Encapsulation and abstraction are unrelated concepts.

Answer: Option A

Explanation: Encapsulation is about bundling data and methods, while abstraction is about presenting a simplified view of an object’s essential features.

14.) Can an abstract class have non-abstract (concrete) methods in Java?

A) No, an abstract class can only have abstract methods.
B) Yes, but only if the non-abstract methods are marked as “final.”
C) Yes, an abstract class can have both abstract and non-abstract methods.
D) Yes, but only if the non-abstract methods have private access.

Answer: Option C

Explanation: An abstract class can have both abstract and non-abstract methods, providing default implementations for some methods.

15.) What is the purpose of an abstract method in an abstract class?

A) To prevent subclassing.
B) To provide a concrete implementation in the abstract class.
C) To serve as a placeholder that must be overridden by subclasses.
D) To make the class inaccessible.

Answer: Option C

Explanation: An abstract method in an abstract class does not have an implementation and must be overridden by concrete subclasses.

16.) How does encapsulation contribute to data protection and security in Java?

A) By making all class members public.
B) By allowing unrestricted access to all data.
C) By restricting direct access to internal data and providing controlled access through methods.
D) By using the “protected” access modifier for all attributes.

Answer: Option C

Explanation: Encapsulation prevents direct access to internal data, reducing the risk of unauthorized modifications and enhancing data security.

17.) What does encapsulation ensure in Java?

A) Access to all class members from any package
B) Hiding implementation details and restricting access to class members
C) Accessibility of only private methods within the class
D) Automatic instantiation of objects

Answer: Option B

Explanation: Encapsulation ensures that implementation details are hidden and access to class members is controlled, promoting data protection.

18.) Which access modifier provides the most open visibility for class members in Java?

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

Answer: Option D

Explanation: The “public” access modifier allows class members to be accessed from any class or package.

19.) Which Java keyword is used to declare a method as abstract within an abstract class?

A) abstract
B) method
C) void
D) override

Answer: Option A

Explanation: The “abstract” keyword is used to declare a method as abstract within an abstract class, indicating that it lacks implementation.

20.) How does abstraction contribute to reducing software complexity and improving maintenance?

A) By exposing all implementation details
B) By making all methods public
C) By hiding data attributes
D) By presenting a high-level view and hiding low-level details

Answer: Option D

Explanation: Abstraction simplifies understanding by presenting essential features while hiding complex implementation details.