Java Packages & Import Statements Quiz

Java
0 Passed
0% acceptance

40 deep, practical, and often overlooked questions about Java packages — from directory structure enforcement and classpath mechanics to import rules, static imports, naming conventions, and the subtle ways packages affect accessibility, compilation, and runtime behavior.

40 Questions
~80 minutes
1

Question 1

What is the primary purpose of packages in Java beyond mere code organization?

A
To provide a namespace and enforce access control at the package level
B
To improve runtime performance
C
To enable multiple inheritance
D
To allow dynamic class loading
2

Question 2

Why does this perfectly valid class fail to compile when placed in the file src/com/example/MyClass.java?

java
class MyClass {
    public void hello() {}
}
A
Missing package declaration — file is in com/example but class has no package
B
Class name must match file name exactly
C
Missing public modifier
D
No main method
3

Question 3

Which of these package names is explicitly forbidden by the Java Language Specification?

A
java.utils
B
com.mycompany.app
C
myapp.internal
D
_private.api
4

Question 4

What happens when two jars on the classpath both contain the same fully qualified class?

A
The class loaded first (classpath order) wins — others are ignored
B
Compilation error
C
LinkageError at runtime
D
The newest version is chosen
5

Question 5

Why is the default (package-private) access modifier extremely useful when combined with packages?

A
Members are visible only within the same package — perfect for internal APIs
B
It makes members public
C
It hides members from subclasses
D
It prevents serialization
6

Question 6

Which import statement correctly allows use of Arrays.asList without qualifying the class?

A
import java.util.Arrays;
B
import java.util.*;
C
import java.util.Arrays.asList;
D
Both A and B
7

Question 7

Why does this static import allow calling asList() directly?

javascript
import static java.util.Arrays.asList;
// List<String> list = asList("a", "b");
A
Static import brings the method into the current namespace
B
It imports the entire Arrays class
C
It only works for fields
D
Compilation error
8

Question 8

What is the recommended package naming convention for open-source or commercial Java projects?

A
Reverse domain name (e.g., com.github.username.project)
B
All lowercase single word
C
Same as class name
D
Uppercase to match class
9

Question 9

Why does this import cause a compilation error?

java
import java.util.*;
import java.awt.*;
// List colors;
A
Both packages contain a List type — ambiguous reference
B
import statements must be first
C
Too many imports
D
No error
10

Question 10

What is the fully qualified name of this class?

java
package com.mycompany.app.service;

public class UserService {
}
A
com.mycompany.app.service.UserService
B
UserService
C
com/mycompany/app/service/UserService
D
service.UserService
11

Question 11

Which of these is NOT a valid package name according to the language specification?

A
com.example._internal
B
com.example.my-project
C
com.example.2utils
D
com.example.utils
12

Question 12

Can a public class be placed in the default (unnamed) package?

A
Yes, but strongly discouraged and forbidden in modular JARs
B
No — public classes must have a package
C
Only in Java 8 and earlier
D
Only for testing
13

Question 13

Why does this static import on-demand compile successfully?

javascript
import static java.lang.Math.*;
// double r = sqrt(16);
A
It brings all static members of Math into scope
B
Only works for constants
C
Causes conflict with local methods
D
Compilation error
14

Question 14

What happens if you declare two classes with the same simple name in different packages and try to use both without qualification?

A
Compilation error — ambiguous reference
B
Runtime exception
C
The one imported first wins
D
Always picks java.lang
15

Question 15

Which package name would be most appropriate for internal utility classes of a library called AcmeTools?

A
com.acme.tools.internal
B
com.acme.tools
C
internal.com.acme.tools
D
acme.tools.hidden
16

Question 16

Why is it considered a bad practice to use wildcard imports (import package.*) in production code?

A
It obscures which classes are actually used and can cause conflicts
B
It slows down compilation
C
It increases bytecode size
D
It prevents modularization
17

Question 17

What is the only package that is automatically imported into every Java source file?

A
java.lang
B
java.util
C
java.io
D
None — all must be imported
18

Question 18

Can a file contain more than one package declaration?

A
No — exactly one package statement (or none) per compilation unit
B
Yes — multiple are allowed
C
Only in module-info.java
D
Only for inner classes
19

Question 19

Why does this compilation unit compile successfully even though no import is present?

javascript
class Test {
    String name;
    System system;
}
A
java.lang types (String, System) are automatically available
B
Implicit import of current package
C
Default import of java.util
D
IDE adds imports silently
20

Question 20

Which of these package names is technically legal but universally considered terrible practice?

A
com.sun.internal.util
B
org.mycompany.app
C
myapp
D
utils
21

Question 21

What is the correct directory structure for the class com.example.service.UserService?

A
src/com/example/service/UserService.java
B
src/com.example/service/UserService.java
C
src/com/example/UserService.java
D
src/UserService.java
22

Question 22

Why does this static import fail with a compiler error?

javascript
import static java.util.Collections.EMPTY_LIST;
// List list = EMPTY_LIST;
A
EMPTY_LIST was renamed to List.of() — field no longer exists
B
Static imports don’t work on fields
C
Missing public modifier
D
Conflicts with java.awt.List
23

Question 23

Which classes from the same package can access a package-private member?

A
Any class in the exact same package, regardless of inheritance
B
Only subclasses
C
Only classes in subpackages
D
Only the declaring class
24

Question 24

What is the most important reason to avoid placing any code in the default package?

A
It cannot be used from named packages and is forbidden in modules
B
It runs slower
C
It prevents garbage collection
D
It disables generics
25

Question 25

Which of these import statements is illegal?

A
import static java.lang.System.out;
B
import java.util.*;
C
import static java.lang.Math.*;
D
import java.lang.*;
26

Question 26

Why do many large codebases forbid wildcard imports entirely?

A
They make it impossible to see at a glance which classes are actually used
B
They cause runtime overhead
C
They prevent tree-shaking
D
They increase JAR size
27

Question 27

What is the correct way to refer to a class that has the same simple name as another imported class?

A
Use its fully qualified name
B
Remove one of the imports
C
Use this.ClassName
D
Cast it
28

Question 28

Why is the package name org.example recommended for example code instead of com.example?

A
org is reserved for non-commercial and open-source organizations
B
com is only for companies
C
No difference
D
org prevents deployment
29

Question 29

Can a package declaration contain comments or annotations?

A
No — it must be the very first non-whitespace statement in the file
B
Yes — comments are allowed before
C
Only Javadoc comments
D
Only @Deprecated
30

Question 30

What is the most common real-world consequence of choosing a bad package name?

A
Naming conflicts when publishing to public repositories or merging code
B
Slower execution
C
Larger bytecode
D
GC pauses
31

Question 31

Why are subpackages not automatically visible to their parent packages?

A
There is no hierarchical relationship — com.example and com.example.utils are unrelated
B
Security restriction
C
Performance optimization
D
Only in modules
32

Question 32

Which of these is considered the cleanest way to import multiple constants from an enum?

A
import static com.example.Color.*;
B
import com.example.Color;
C
No import — use Color.RED directly
D
import com.example.*;
33

Question 33

What is the purpose of the package-info.java file?

A
To hold package-level Javadoc and annotations (e.g., @Deprecated)
B
To define package-private classes
C
To declare module exports
D
To store configuration
34

Question 34

Why does the Java platform itself use packages like jdk.internal.*?

A
To clearly signal that these APIs are not supported for external use
B
For performance
C
To reduce JAR size
D
To enable encapsulation
35

Question 35

After completing this quiz, what is the single most important lesson about packages?

A
Packages are Java’s primary mechanism for namespace management and encapsulation
B
Packages are optional
C
Wildcard imports are best practice
D
Package structure doesn’t affect compilation
36

Question 36

Which classes automatically belong to the same package as the current class without any import?

A
Other classes in the exact same package
B
Classes in subpackages
C
Classes in java.util
D
Inner classes only
37

Question 37

Why should you never name a package 'test' at the top level?

A
It conflicts with common test source roots and build tools
B
It’s reserved by the JVM
C
It prevents testing
D
It disables assertions
38

Question 38

What is the only valid position for the package declaration in a Java source file?

A
The very first line (before any comments or blank lines)
B
After imports
C
After the class declaration
D
Anywhere before the class
39

Question 39

Which of these is considered the gold standard for package naming in enterprise applications?

A
Reverse internet domain + project + logical layers (e.g., com.acme.banking.service)
B
Single word lowercase
C
All uppercase
D
Same as module name
40

Question 40

You now fully understand that:

A
Packages are not just folders — they are Java’s core namespace and access control mechanism
B
Package structure is optional
C
java.* packages are safe to use
D
Imports affect runtime performance

QUIZZES IN Java