Java MCQs – packages and import statements

Packages and Import Statements provide a comprehensive set of multiple-choice questions to test your understanding of these essential concepts in Java programming. Packages and import statements play a crucial role in organizing and managing Java code by allowing you to group related classes together and access classes from different packages. These MCQs cover various aspects of creating, using, and importing packages, helping you strengthen your knowledge of Java’s modular structure and how to effectively utilize import statements.

1.) What’s the main purpose of Java packages?

A) Group-related classes
B) Create variables
C) Handle exceptionsn3
D) Implement interfaces

Answer: Option A

Explanation: Java packages are used to group classes and interfaces that are related or serve a common purpose, helping to organize and manage code.

2.)Purpose of import java.util.*;?

A) Import all classes1
B) Import wildcard classes
C) Import with sub-packages
D) Import the current package

Answer: Option C

Explanation: The import java.util.*; statement imports all classes from the java.util package and its subpackages.

3.) What does the import statement do?

A) Includes package contents
B) Creates objects
C) Imports all classes
D) Defines new packages

Answer: Option A

Explanation: The import statement allows you to access classes and other members of a specific package without using their fully qualified names.

4.) Correct syntax for importing MyClass from com.example?

A) import MyClass from com.example;le;on1
B) import com.example.MyClass;
C) import package com.example.MyClass;
D) import com.example package MyClass;

Answer: Option B

Explanation: The correct syntax for importing a class is import package_name.ClassName;.

5.) To use MyClass from myPackage, which import is needed?

A) import myPackage.MyClass;
B) import myPackage;
C) import MyClass from myPackage;
D) import class myPackage.MyClass;

Answer: Option A

Explanation: To use a class from another package, you need to import it using the full package name and class name.

6.) Use classes from default package in Java?

A) Yes, without restrictions
B) Yes, within the same package
C) No, inaccessible
D) No, only in default classes

Answer: Option A

Explanation: Classes from the default package can be used without import statements, but it’s recommended to use named packages for better organization.

7.) Do classes in same package need imports?

A) No, imports not needed
B) Yes, mandatory for all
C) Not allowed
D) Only for abstract classes

Answer: Option A

Explanation: Classes in the same package are accessible without import statements.

8.) What is the default package in Java?

A) It is a package that contains all built-in classes.
B) It is a package created automatically for each new project.
C) It is a package that does not have a name.
D) It is a package used for importing external libraries.

Answer: Option C

Explanation: The default package is a package without a specified name. It’s not recommended to use the default package for your classes.

9.) Valid access to MyClass in myPackage without import?

A) myPackage.MyClass
B) MyClass.from(myPackage);
C) import myPackage.MyClass;
D) MyClass myPackage;

Answer: Option A

Explanation: To access a class without an import, use its fully qualified name, like myPackage.MyClass. Other options are incorrect or invalid.

10.) What is the purpose of using “import static” statements in Java?

A) To import all classes from a package.
B) To import classes with static methods only.
C) To import static members of a class.
D) To import classes for reflection purposes.

Answer: Option C

Explanation: import static is used to import specific static members (fields or methods) of a class.