Java OCA OCP Practice Question 1368

Question

Fill in the blanks:

Using the ___ and ___ modifiers together allows a variable to be accessed from any class,

without requiring an instance variable.

  • A. final, package-private
  • B. class, static
  • C. protected, instance
  • D. public, static
  • E. default, public


D.

Note

The public modifier allows access members in the same class, package, subclass, or even classes in other packages.

While the static modifier allows access without an instance of the class.

Option D is the correct answer.

Option A is incorrect because final is not related to access, and package-private prevents access from classes outside the package.

Option B is incorrect because class is not a modifier; it is a keyword.

Option C is incorrect because instance is not a Java keyword or modifier.

protected prevents classes that are not subclasses and are outside the package from accessing the variable.

Option E is incorrect.

The default keyword is for interface methods and switch statements, not class variables.




PreviousNext

Related