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?
2.) Which 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?
4.) What is the default access modifier for class members (variables and methods) in Java?
5.) Which access modifier 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?
7.) Which access modifier is used when you want to hide a variable or method from other classes, even subclasses?
8.) If a class member has no explicit access modifier, what access level does it have?
9.) Which of the following is an incorrect representation of access modifiers in order of increasing visibility?
10.) Which access modifier can be applied to top-level classes?