Java MCQs – Access Modifiers

11.) Can an interface have a private method?

A) Yes, but it can only be accessed within the interface.
B) No, interfaces cannot have private methods.
C) Yes, and it can be accessed by classes that implement the interface.
D) Yes, but it can only be accessed within the same package.

Answer: Option B

Explanation: No, interfaces cannot have private methods. All methods in an interface are implicitly public.

12.) Which access modifier is appropriate for a method that should be accessible only within the same package and not be overridden by subclasses?

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

Answer: Option C

Explanation: The default (package-private) access modifier combined with the final keyword would make a method accessible only within the same package and not be overridden by subclasses.

13) If a subclass is in a different package, which access modifiers of the superclass are accessible?

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

Answer: Option B

Explanation: In a subclass in a different package, accessible access modifiers of the superclass are protected and default (package-private).

14.) Which access modifier allows a subclass to increase the visibility of a method while overriding it?

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

Answer: Option. C

Explanation: The protected access modifier allows a subclass to increase the visibility of a method while overriding it, but it can’t decrease visibility.

15.) Can a class be marked as both abstract and final simultaneously?

A) Yes, as long as it has abstract methods.
B) No, abstract and final are conflicting modifiers and cannot be used together.
C) Yes, but it can’t have any methods.
D) Yes, but only if it’s in a different package.

Answer: Option B

Explanation: No, abstract and final are conflicting modifiers and cannot be used together. An abstract class is meant to be subclassed, while a final class cannot be subclassed.

16.) Which access modifier is used for interface methods by default?

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

Answer: Option A

Explanation: Interface methods are public by default. All methods in an interface are implicitly public and abstract.

17.) In which case would you use the “protected” access modifier for a class member?

A) When you want it to be accessible globally.
B) When you want it to be accessible only within the same package.2
C) When you want it to be accessible within the same package and its subclasses.
D) When you want it to be accessible only within the same class.

Answer: Option C

Explanation: The “protected” access modifier is used when you want a class member to be accessible within the same package and its subclasses.

18.) Which access modifier would you use for a class member that needs to be accessed by any class, even those in different packages?

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

Answer: Option A

Explanation: The “public” access modifier allows a class member to be accessed by any class, regardless of the package.