Java MCQs – Access Modifiers

Access modifiers in Java are important tools for controlling who can see and use variables, methods, and classes. They decide how these parts of the program can be reached by other parts. Ranging from the wide “public” modifier to the strict “private” one, each has a special job in controlling access. These multiple-choice questions (MCQs) explore the details of access modifiers, aiding developers in understanding their importance for creating safe and well-structured Java programs.

1.) Which access modifier allows a variable or method to be accessible within the same package and any subclass, regardless of the package?

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

Answer: Option B

Explanation: protected access modifier allows a variable or method to be accessible within the same package and any subclass, regardless of the package.

2.) Which access modifier restricts a variable or method to be accessible only within the same class?

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

Answer: Option D

Explanation: private access modifier restricts a variable or method to be accessible only within the same class.

3.) Which access modifier provides the widest level of visibility for a variable or method?

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

Answer: Option A

Explanation: public access modifier provides the widest level of visibility for a variable or method, allowing it to be accessed from anywhere.

4.) What is the default access modifier for class members (variables and methods) in Java?

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

Answer: Option C

Explanation: The default access modifier (also known as package-private) allows class members to be accessed within the same package, but not outside of it.

5.) Which access modifier allows a class to be accessible only within the same package?

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

Answer: Option C

Explanation: The default access modifier (package-private) allows a class to be accessible only within the same package.

6.) In Java, can a subclass in a different package access a superclass’s protected method?

A) Yes, without any restrictions.
B) Yes, but only if they are in the same package.
C) No, protected methods are not accessible by subclasses.
D) No, protected methods are only accessible within the same class.

Answer: Option B

Explanation: Yes, a subclass in a different package can access a superclass’s protected method, as long as they are in a subclass relationship.

7.) Which access modifier is used when you want to hide a variable or method from other classes, even subclasses?

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

Answer: Option D

Explanation: private access modifier is used when you want to hide a variable or method from other classes, including subclasses.

8.) If a class member has no explicit access modifier, what access level does it have?

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

Answer: Option C

Explanation: If no access modifier is specified, the default access level (package-private) is assumed. This means the member is accessible within the same package.

9.) Which of the following is an incorrect representation of access modifiers in order of increasing visibility?

A) private < default < protected < public
B) private < protected < default < public
C) public < protected < default < private
D) protected < default < private < public

Answer: Option B

Explanation: The correct order is: private < default < protected < public.

10.) Which access modifier can be applied to top-level classes?

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

Answer: Option C

Explanation: Top-level classes can have access modifiers: public, default (package-private), and protected.