0
0
Javaprogramming~15 mins

Import statement usage in Java - Cheat Sheet & Quick Revision

Choose your learning style8 modes available
overviewRecall & Review
beginner
What is the purpose of an import statement in Java?
An import statement allows you to use classes and interfaces from other packages without writing their full package names every time.
touch_appClick to reveal answer
beginner
How do you import all classes from a package named <code>java.util</code>?
You use <code>import java.util.*;</code> to import all classes from the <code>java.util</code> package.
touch_appClick to reveal answer
beginner
True or False: You can import a single class using its full package name.
True. For example, <code>import java.util.ArrayList;</code> imports only the <code>ArrayList</code> class.
touch_appClick to reveal answer
beginner
What happens if you don't use an import statement but want to use a class from another package?
You must use the full package name every time you refer to the class, like java.util.ArrayList instead of just ArrayList.
touch_appClick to reveal answer
beginner
Can you import classes from the same package using an import statement?
No, classes in the same package are accessible without import statements.
touch_appClick to reveal answer
Which import statement imports all classes from the java.io package?
Aimport java.io;
Bimport java.io.*;
Cimport * from java.io;
Dimport java.io.all;
What is the correct way to import only the Scanner class from java.util?
Aimport java.util.Scanner;
Bimport java.util.*;
Cimport Scanner from java.util;
Dimport java.util.Scanner.*;
If you do not import a class, how can you still use it?
ABy using a wildcard import
BBy guessing the class name
CBy writing its full package name every time
DBy declaring it again
Can you import a class from the same package?
AOnly if the class is private
BYes, always required
COnly if the class is public
DNo, it is not necessary
What does the import statement import java.util.*; do?
AImports all classes in java.util package
BImports only the java.util package itself
CImports all sub-packages of java.util
DImports only the first class in java.util
Explain why import statements are useful in Java and how they simplify code.
Describe the difference between importing a single class and using a wildcard import.