TopicTestAccessModifiers
Java Test Paper: Access Modifiers
Section A: Multiple Choice (1 mark each)
Which access modifier provides the widest accessibility in Java? a.
publicb.protectedc.privated.package-private(default)What is the default access modifier for class members (variables and methods) when no access modifier is specified in Java? a.
publicb.protectedc.privated.package-private(default)Which access modifier allows a member to be accessible within the same class, derived classes, and classes in the same package? a.
publicb.protectedc.privated.package-privateWhich access modifier restricts the visibility of a member to only the class in which it is declared? a.
publicb.protectedc.privated.package-privateWhat is the purpose of the
publicaccess modifier in Java? a. To limit access to the member to the same class. b. To restrict access to derived classes. c. To make the member accessible from anywhere. d. To make the member accessible only within the same package.In Java, can a
privatemember of a class be accessed from a different class in the same package? a. Yes b. No
Section B: True/False (1 mark each)
True or False: A
protectedmember can be accessed from any class, regardless of its package, as long as it is a subclass of the class where the member is defined.True or False: The
default(package-private) access modifier allows access to a class member only within the same class and package.True or False: The
publicaccess modifier is used to create truly encapsulated classes and protect data from unauthorized access.True or False: An interface in Java can have members with
protectedaccess.
Section C: Code-related Questions (2 marks each)
Write a Java class named "Car" with a
privatemember variable "model" and apublicmethod named "getModel" to retrieve the value of "model."Create a class named "Employee" with a
protectedmember variable "salary." Write a method named "getSalary" in a subclass of "Employee" named "Manager" to access and return the "salary" value.Define an interface named "Drawable" with a
publicmethod "draw." Create a class named "Circle" that implements the "Drawable" interface and provides an implementation of the "draw" method.

Comments
Post a Comment