Packages & Imports — Concept Check
Questions on Packages & Imports. Organise classes into packages, control visibility with package-private access, and import types cleanly including static imports.
Questions in this quiz
8 questions on Packages & Imports — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have a Java class in the default package. What is a limitation you might face?
- AIt cannot have public methods.
- BIt cannot access the Java standard library.
- CIt cannot be compiled with javac.
- DIt cannot be imported by classes in named packages.
2.A developer writes a package statement after an import statement. What will happen?
- AThe compiler will throw an error.
- BThe package statement will be ignored.
- CThe program will run but with warnings.
- DThe import statement will be ignored.
3.You have two classes with the same name in different packages. How can you use both in the same file?
- ARename one of the classes to avoid conflict.
- BUse fully-qualified names for both classes.
- CImport both and rely on the compiler to resolve conflicts.
- DImport one and use the fully-qualified name for the other.
4.A developer uses 'import java.util.*;' in their code. What is a potential downside?
- AIt causes compilation errors if the package is empty.
- BIt prevents the use of static imports.
- CIt makes the code less readable.
- DIt increases the runtime memory usage.
5.You want to access a package-private member of a class from another package. What should you do?
- AUse a static import to access it.
- BImport the package to access it.
- CMove your class to the same package.
- DChange the member to public or protected.
6.A developer believes that static imports can only be used for methods. What is the truth?
- AStatic imports can only be used for fields.
- BStatic imports can only be used for methods.
- CStatic imports can be used for fields and methods.
- DStatic imports are not part of Java.
7.In a Java project, you notice 'import static java.lang.Math.PI;'. What advantage does this provide?
- AAutomatically imports all Math class methods.
- BAllows using 'PI' directly without 'Math.' prefix.
- CReduces memory usage of the application.
- DIncreases the execution speed of mathematical operations.
8.What is the default access level for class members if no access modifier is specified?
- AProtected
- BPackage-private
- CPublic
- DPrivate