TopicTestAccessModifiers
Java Test Paper: Access Modifiers
Section A: Multiple Choice (1 mark each)
Which access modifier provides the widest accessibility in Java? a.
public
b.protected
c.private
d.package-private
(default)What is the default access modifier for class members (variables and methods) when no access modifier is specified in Java? a.
public
b.protected
c.private
d.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.
public
b.protected
c.private
d.package-private
Which access modifier restricts the visibility of a member to only the class in which it is declared? a.
public
b.protected
c.private
d.package-private
What is the purpose of the
public
access 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
private
member 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
protected
member 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
public
access 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
protected
access.
Section C: Code-related Questions (2 marks each)
Write a Java class named "Car" with a
private
member variable "model" and apublic
method named "getModel" to retrieve the value of "model."Create a class named "Employee" with a
protected
member 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
public
method "draw." Create a class named "Circle" that implements the "Drawable" interface and provides an implementation of the "draw" method.
Comments
Post a Comment