Java Packages & Import Statements Quiz
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.
Question 1
What is the primary purpose of packages in Java beyond mere code organization?
Question 2
Why does this perfectly valid class fail to compile when placed in the file src/com/example/MyClass.java?
class MyClass {
public void hello() {}
}Question 3
Which of these package names is explicitly forbidden by the Java Language Specification?
Question 4
What happens when two jars on the classpath both contain the same fully qualified class?
Question 5
Why is the default (package-private) access modifier extremely useful when combined with packages?
Question 6
Which import statement correctly allows use of Arrays.asList without qualifying the class?
Question 7
Why does this static import allow calling asList() directly?
import static java.util.Arrays.asList;
// List<String> list = asList("a", "b");Question 8
What is the recommended package naming convention for open-source or commercial Java projects?
Question 9
Why does this import cause a compilation error?
import java.util.*;
import java.awt.*;
// List colors;Question 10
What is the fully qualified name of this class?
package com.mycompany.app.service;
public class UserService {
}Question 11
Which of these is NOT a valid package name according to the language specification?
Question 12
Can a public class be placed in the default (unnamed) package?
Question 13
Why does this static import on-demand compile successfully?
import static java.lang.Math.*;
// double r = sqrt(16);Question 14
What happens if you declare two classes with the same simple name in different packages and try to use both without qualification?
Question 15
Which package name would be most appropriate for internal utility classes of a library called AcmeTools?
Question 16
Why is it considered a bad practice to use wildcard imports (import package.*) in production code?
Question 17
What is the only package that is automatically imported into every Java source file?
Question 18
Can a file contain more than one package declaration?
Question 19
Why does this compilation unit compile successfully even though no import is present?
class Test {
String name;
System system;
}Question 20
Which of these package names is technically legal but universally considered terrible practice?
Question 21
What is the correct directory structure for the class com.example.service.UserService?
Question 22
Why does this static import fail with a compiler error?
import static java.util.Collections.EMPTY_LIST;
// List list = EMPTY_LIST;Question 23
Which classes from the same package can access a package-private member?
Question 24
What is the most important reason to avoid placing any code in the default package?
Question 25
Which of these import statements is illegal?
Question 26
Why do many large codebases forbid wildcard imports entirely?
Question 27
What is the correct way to refer to a class that has the same simple name as another imported class?
Question 28
Why is the package name org.example recommended for example code instead of com.example?
Question 29
Can a package declaration contain comments or annotations?
Question 30
What is the most common real-world consequence of choosing a bad package name?
Question 31
Why are subpackages not automatically visible to their parent packages?
Question 32
Which of these is considered the cleanest way to import multiple constants from an enum?
Question 33
What is the purpose of the package-info.java file?
Question 34
Why does the Java platform itself use packages like jdk.internal.*?
Question 35
After completing this quiz, what is the single most important lesson about packages?
Question 36
Which classes automatically belong to the same package as the current class without any import?
Question 37
Why should you never name a package 'test' at the top level?
Question 38
What is the only valid position for the package declaration in a Java source file?
Question 39
Which of these is considered the gold standard for package naming in enterprise applications?
Question 40
You now fully understand that:
