Packages And Access Modifiers

1.Which of the following statements is true about Java packages?
  • A. Packages are used only for importing external libraries.
  • B. A class can belong to multiple packages.
  • C. Packages provide access protection and namespace management.
  • D. A package can contain only classes and interfaces.

Packages provide access protection and namespace management.

Packages provide access protection and namespace management.

2.Which access modifier allows visibility only within the same package?
  • A. private
  • B. protected
  • C. default
  • D. public

default

default

3.What is the result of compiling a class without any package declaration?
  • A. It belongs to the java.lang package by default.
  • B. It causes a compilation error.
  • C. It belongs to the default (unnamed) package.
  • D. It becomes a part of all user-defined packages.

It belongs to the default (unnamed) package.

It belongs to the default (unnamed) package.

4.Which modifier allows a method to be accessed in different packages only through inheritance?
  • A. public
  • B. private
  • C. default
  • D. protected

protected

protected

5.What happens if you declare a class as private?
  • A. t's accessible within the same package.
  • B. It's only accessible within the same class.
  • C. It compiles successfully and is accessible globally.
  • D. It causes a compile-time error.

It causes a compile-time error.

It causes a compile-time error.