Java MCQs – packages and import statements

11.) When might you encounter a naming collision while importing classes?

A) When using wildcard imports
B) When importing classes from the same package
C) When importing classes from different packages
D) When using fully qualified class names

Answer: Option A

Explanation: Wildcard imports (.*) can lead to naming collisions if classes with the same name exist in different packages.

12.) What is the advantage of using named packages over the default package?

A) Default packages have better access to built-in classes.
B) Named packages prevent naming conflicts.
C) Default packages offer faster runtime performance.
D) Named packages automatically import all classes.

Answer: Option B

Explanation: Named packages help avoid naming conflicts and provide better organization for classes and interfaces.

13.) Can a class belong to multiple packages simultaneously?

A) Yes, but it requires advanced import statements.
B) No, a class can belong to only one package.
C) Yes, but it causes naming conflicts.
D) Yes, if it’s placed in the default package.

Answer: Option B

Explanation: In Java, a class can belong to only one package at a time.

14.) Which statement is true about packages and directories in Java?

A) Each package corresponds to a separate directory.
B) Packages are not related to directory structure.
C) Directories are created automatically for each class.
D) Packages and directories have opposite structures.

Answer: Option A

Explanation: Java follows a one-to-one relationship between packages and directories in the file system.

15.) What’s the purpose of the CLASSPATH environment variable?

A) It sets the default package for a project.
B) It defines the location of Java compiler.
C) It specifies where to find compiled classes and packages.
D) It is used for creating class documentation.

Answer: Option C

Explanation: The CLASSPATH environment variable tells the Java compiler and runtime where to find classes and packages.

16.) Which of the following is NOT a benefit of using packages in Java?

A) Organizing and managing code
B) Reducing naming conflicts
C) Improving runtime performance
D) Enhancing code readability

Answer: Option C

Explanation: Packages help organize code, reduce naming conflicts, and enhance code readability, but they do not directly impact runtime performance.

17.) If you have classes A and B in package myPackage, which import statement is correct to use them in another class?

A) import myPackage.*;
B) import myPackage.A, B;
C) import A, B from myPackage;
D) import class myPackage.A, myPackage.B;

Answer: Option B

Explanation: When importing multiple classes from the same package, you can list their names separated by commas.

18.) What happens if you attempt to use a class without an import statement or fully qualified name?

A) The program compiles but throws a runtime error.
B) The program compiles and uses the class from the default package.
C) The program doesn’t compile due to an import error.
D) The program doesn’t compile due to a missing package declaration.

Answer: Option B

Explanation: If no import or fully qualified name is provided, the program assumes the class is from the default package.

19.) Can two classes with the same name exist in different packages?

A) No, class names must be unique across all packages.
B) Yes, but they must have distinct method names.
C) Yes, but it leads to naming conflicts.
D) No, Java enforces unique class names globally.

Answer: Option B

Explanation: Two classes with the same name can coexist in different packages, as long as they don’t lead to ambiguity when used.

20.) What is the role of a manifest file in Java packages?

A) It defines the directory structure of packages.
B) It contains metadata about the package and its classes.
C) It specifies import statements for package classes.
D) It is used to set the CLASSPATH environment variable.

Answer: Option B

Explanation: The manifest file in a JAR (Java Archive) package contains metadata about the package, including version information and entry points.