1. Which access modifier in Java specifies the highest level of access?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
Private methods are not visible to subclasses and therefore cannot be inherited.
10. Which statement is true about the public access modifier?
Answer:
Explanation:
The public access modifier allows unrestricted access from anywhere in the program.
11. In which scenario would you use the protected access modifier?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
The `abstract` modifier for methods can only be used in abstract classes, and such methods must be overridden in subclasses​“【oaicite:0】“​.