Java Access Modifiers MCQ Questions and Answers

1. Which access modifier in Java specifies the highest level of access?

a) private
b) protected
c) public
d) default

Answer:

c) public

Explanation:

The public access modifier allows the widest level of access, making the class or class member accessible from anywhere.

2. What is the default access modifier in Java?

a) public
b) private
c) protected
d) None of the above

Answer:

d) None of the above

Explanation:

If no access modifier is specified, it defaults to package-private, meaning the class or member is accessible within its own package.

3. Which access modifier allows visibility only within the same class?

a) public
b) private
c) protected
d) default

Answer:

b) private

Explanation:

The private access modifier restricts visibility to within the same class only.

4. Can a top-level class in Java be declared as private?

a) Yes
b) No
c) Only in inner classes
d) Only if it's a static class

Answer:

b) No

Explanation:

A top-level class cannot be declared as private in Java. Private is only applicable to inner classes and members.

5. Which access modifier in Java allows access within the same package and subclasses?

a) public
b) private
c) protected
d) default

Answer:

c) protected

Explanation:

The protected access modifier allows access within the same package and also in subclasses, even if they are in different packages.

6. What does the protected access modifier not allow?

a) Access from within the same class
b) Access from within the same package
c) Access from subclasses in different packages
d) Access from non-subclass classes in different packages

Answer:

d) Access from non-subclass classes in different packages

Explanation:

The protected access modifier does not allow access from non-subclass classes outside its own package.

7. Which access modifier is most restrictive in Java?

a) public
b) private
c) protected
d) default

Answer:

b) private

Explanation:

The private access modifier is the most restrictive, limiting access to only within the class it is declared in.

8. How does the default (package-private) access modifier restrict access?

a) Allows access only within the same class
b) Allows access only within the same package
c) Allows access from anywhere
d) Allows access only from subclasses

Answer:

b) Allows access only within the same package

Explanation:

The default (package-private) access modifier restricts access to within the same package.

9. Can a method with a private access modifier be inherited by a subclass in Java?

a) Yes
b) No
c) Only in the same package
d) Only if overridden

Answer:

b) No

Explanation:

Private methods are not visible to subclasses and therefore cannot be inherited.

10. Which statement is true about the public access modifier?

a) It restricts access to within the same package
b) It allows access from anywhere in the program
c) It is the default access level
d) It is only applicable to methods

Answer:

b) It allows access from anywhere in the program

Explanation:

The public access modifier allows unrestricted access from anywhere in the program.

11. In which scenario would you use the protected access modifier?

a) To hide a class member from other classes in the same package
b) To allow access only within the same class
c) To allow access to subclasses, including those in different packages
d) To allow access to any class within the same package

Answer:

c) To allow access to subclasses, including those in different packages

Explanation:

The protected access modifier is used to allow access to a class member in subclasses, including those that are in different packages.

12. Is it possible to restrict access to a constructor in Java?

a) Yes, using any access modifier
b) No, constructors must always be public
c) Yes, but only with the private modifier
d) No, constructors cannot have access modifiers

Answer:

a) Yes, using any access modifier

Explanation:

Constructors in Java can have access modifiers, including public, protected, private, or package-private (default), to control instantiation.

13. What is the default access level for a class in Java if no access modifier is specified?

a) public
b) private
c) protected
d) package-private

Answer:

d) package-private

Explanation:

If no access modifier is specified for a class, it defaults to package-private, meaning it's accessible only within its own package.

14. What does the `final` modifier indicate when applied to a class?

a) The class is abstract
b) The class can be inherited
c) The class cannot be inherited
d) The class is public

Answer:

c) The class cannot be inherited

Explanation:

When the `final` modifier is applied to a class, it indicates that the class cannot be inherited by other classes​``【oaicite:6】``​.

15. What is the role of the `abstract` modifier in Java?

a) To make a class or method final
b) To indicate that a class can create objects
c) To declare a class that cannot be instantiated
d) To make a method private

Answer:

c) To declare a class that cannot be instantiated

Explanation:

The `abstract` modifier is used to declare a class that cannot be instantiated on its own and must be inherited​``【oaicite:5】``​.

16. Which modifier indicates that a method can only be accessed by one thread at a time?

a) transient
b) volatile
c) synchronized
d) static

Answer:

c) synchronized

Explanation:

The `synchronized` modifier indicates that a method can be accessed by only one thread at a time, making it useful for concurrency control​``【oaicite:4】``​.

17. What is the function of the `static` modifier in Java?

a) Makes a method synchronized
b) Indicates that an attribute or method belongs to the class, rather than an object
c) Makes an attribute final and unchangeable
d) Indicates that a method is abstract

Answer:

b) Indicates that an attribute or method belongs to the class, rather than an object

Explanation:

The `static` modifier indicates that the member (method or field) belongs to the class itself rather than to a specific instance of the class​``【oaicite:3】``​.

18. How does the `transient` modifier affect serialization?

a) It makes an attribute serializable
b) It prevents an attribute from being serialized
c) It makes a method transient
d) It has no effect on serialization

Answer:

b) It prevents an attribute from being serialized

Explanation:

The `transient` modifier is used to indicate that an attribute should not be serialized when the object containing it is serialized​``【oaicite:2】``​.

19. What is the effect of the `volatile` modifier on a variable?

a) It makes the variable immutable
b) It ensures the variable is read from and written to main memory
c) It synchronizes access to the variable
d) It makes the variable static

Answer:

b) It ensures the variable is read from and written to main memory

Explanation:

The `volatile` modifier ensures that a variable's value is always read from and written to the main memory, not cached by threads​``【oaicite:1】``​.

20. When is it appropriate to use the `abstract` modifier for methods?

a) In any class
b) Only in final classes
c) Only in abstract classes
d) In static methods

Answer:

c) Only in abstract classes

Explanation:

The `abstract` modifier for methods can only be used in abstract classes, and such methods must be overridden in subclasses​``【oaicite:0】``​.


Comments