MCQ Practice Single Best Answer Topic: Elements of Accounting

Q Which keyword can protect a class in a package from accessibility by the classes outside the package ?

Question ID
#24249
Subchapter
Elements of Accounting
Action
Choose one option below

Choose Your Answer

Click an option to check whether your answer is correct.

  • A private
  • B protected
  • C final
  • D don't use any keyword at all (make it default)
Correct Answer: D

Explanation

Accessibility of classes in Java

  • In Java, top-level classes (i.e., not inner classes) can only have:

    • public → accessible from anywhere.

    • default (no modifier) → accessible only within the same package.

  • private and protected cannot be used with a top-level class.

  • final means the class cannot be inherited, but it doesn’t restrict access across packages.


Now checking the options

(a) private → ❌ Not allowed for a top-level class.
(b) protected → ❌ Not allowed for a top-level class (only applies to members).
(c) final → ❌ Only prevents subclassing, not accessibility.
(d) don’t use any keyword at all (make it default) → ✅ Correct!

  • A default (package-private) class is accessible only within the same package.

  • Classes outside the package cannot access it.


Answer: (d) don’t use any keyword at all (make it default)

Share This Question

Share this MCQ with your friends or study group.