TopicTestAccessModifiers



 Java Test Paper: Access Modifiers

Section A: Multiple Choice (1 mark each)

  1. Which access modifier provides the widest accessibility in Java? a. public b. protected c. private d. package-private (default)

  2. 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)

  3. 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

  4. 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

  5. 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.

  6. 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)

  1. 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.

  2. True or False: The default (package-private) access modifier allows access to a class member only within the same class and package.

  3. True or False: The public access modifier is used to create truly encapsulated classes and protect data from unauthorized access.

  4. True or False: An interface in Java can have members with protected access.

Section C: Code-related Questions (2 marks each)

  1. Write a Java class named "Car" with a private member variable "model" and a public method named "getModel" to retrieve the value of "model."

  2. 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.

  3. 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

Popular posts from this blog

FrontEnd - FAQs - Part 1

Java Script - FAQs

CoreJava - ClassesObjectsMethods - Assignment