Bird
Raised Fist0
Javaprogramming~20 mins

Import statement usage in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Import Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Java code using import?

Consider the following Java code snippet. What will it print when run?

Java
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("apple");
        list.add("banana");
        System.out.println(list.get(1));
    }
}
Abanana
Bapple
CCompilation error due to missing import
DRuntime error: IndexOutOfBoundsException
Attempts:
2 left
💡 Hint

Remember that ArrayList is part of java.util package.

❓ Predict Output
intermediate
2:00remaining
What error occurs if import is missing?

What error will this Java code produce if the import statement is removed?

Java
import java.util.HashMap;

public class Test {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("one", 1);
        System.out.println(map.get("one"));
    }
}
ACompilation error: missing semicolon
BRuntime error: NullPointerException
CCompilation error: cannot find symbol HashMap
DPrints 1
Attempts:
2 left
💡 Hint

Think about what happens if you use a class without importing it or fully qualifying it.

🔧 Debug
advanced
2:30remaining
Why does this import cause a conflict?

Given the following code, why does it cause a compilation error?

Java
import java.util.Date;
import java.sql.Date;

public class Test {
    public static void main(String[] args) {
        Date d = new Date(0);
        System.out.println(d);
    }
}
ACompilation error due to ambiguous reference to Date
BPrints the current date and time
CPrints 0
DRuntime error: ClassCastException
Attempts:
2 left
💡 Hint

Think about what happens when two classes with the same name are imported.

📝 Syntax
advanced
1:30remaining
Which import statement is syntactically correct?

Which of the following import statements is correct in Java?

Aimport java.util.*
Bimport java.util.*;
Cimport java.util.*;;
Dimport java.util
Attempts:
2 left
💡 Hint

Remember the syntax for import statements requires a semicolon at the end.

🚀 Application
expert
3:00remaining
How to use fully qualified class name to avoid import conflict?

You have two classes named List in different packages: java.awt.List and java.util.List. You want to use both in the same file without import statements. How do you declare variables of each type?

Aimport java.awt.List; import java.util.List; List awtList; List utilList;
BList awtList = new List(); List utilList = new List();
Cjava.awt.List awtList = new List(); java.util.List utilList = new ArrayList();
Djava.awt.List awtList = new java.awt.List(); java.util.List<String> utilList = new java.util.ArrayList<>();
Attempts:
2 left
💡 Hint

Use the full package name before the class name to avoid ambiguity.

Practice

(1/5)
1. What is the main purpose of an import statement in Java?
easy
A. To allow the use of classes from other packages without full package names
B. To declare a new package for the current class
C. To create a new class in the program
D. To start the main method of the program

Solution

  1. Step 1: Understand the role of import statements

    Import statements let you use classes from other packages without writing their full package names every time.
  2. Step 2: Differentiate from other Java keywords

    Import does not declare packages, create classes, or start methods; it only helps with class usage.
  3. Final Answer:

    To allow the use of classes from other packages without full package names -> Option A
  4. Quick Check:

    Import = Use classes easily [OK]
Hint: Import lets you use classes without full package names [OK]
Common Mistakes:
  • Confusing import with package declaration
  • Thinking import creates classes
  • Assuming import runs code
2. Which of the following is the correct syntax to import all classes from the package java.util?
easy
A. import java.util.all;
B. import java.util;
C. import * from java.util;
D. import java.util.*;

Solution

  1. Step 1: Recall the syntax for importing all classes

    To import all classes from a package, use the asterisk (*) after the package name with a semicolon.
  2. Step 2: Check each option's syntax

    Only import java.util.*; is correct. Others miss the asterisk or use wrong keywords.
  3. Final Answer:

    import java.util.*; -> Option D
  4. Quick Check:

    Use * to import all classes [OK]
Hint: Use * after package name to import all classes [OK]
Common Mistakes:
  • Omitting the asterisk (*) when importing all classes
  • Missing semicolon at the end
  • Using incorrect keywords like 'all' or 'from'
3. What will be the output of the following code?
import java.util.ArrayList;
public class Test {
  public static void main(String[] args) {
    ArrayList<String> list = new ArrayList<>();
    list.add("Hello");
    list.add("World");
    System.out.println(list.get(1));
  }
}
medium
A. ArrayList
B. Hello
C. World
D. Compilation error

Solution

  1. Step 1: Understand the import and list usage

    The code imports ArrayList correctly and creates a list with "Hello" at index 0 and "World" at index 1.
  2. Step 2: Check the printed element

    list.get(1) accesses the second element, which is "World".
  3. Final Answer:

    World -> Option C
  4. Quick Check:

    list.get(1) = World [OK]
Hint: Index 1 means second element in list [OK]
Common Mistakes:
  • Confusing index 0 and 1
  • Assuming import causes error
  • Thinking output is class name
4. Identify the error in the following code snippet:
import java.util.List;
public class Demo {
  public static void main(String[] args) {
    List<String> items = new ArrayList<>();
  }
}
medium
A. Missing semicolon after import statement
B. ArrayList is not imported
C. List cannot be used without initialization
D. No error, code is correct

Solution

  1. Step 1: Check import statements

    The code imports List but does not import ArrayList, which is used in the code.
  2. Step 2: Identify missing import causing error

    ArrayList must be imported from java.util to be used; missing import causes compilation error.
  3. Final Answer:

    ArrayList is not imported -> Option B
  4. Quick Check:

    Missing import for ArrayList causes error [OK]
Hint: Import all classes you use explicitly [OK]
Common Mistakes:
  • Forgetting semicolon after import
  • Assuming List needs initialization differently
  • Ignoring missing imports for used classes
5. You want to use both java.util.Date and java.sql.Date in the same Java file. How should you import them to avoid confusion?
hard
A. import java.util.Date; // Use fully qualified name for java.sql.Date in code
B. import java.util.*; import java.sql.*;
C. import java.util.Date; import java.sql.Date;
D. import java.util.*; import java.sql.Date;

Solution

  1. Step 1: Understand class name conflict

    Both packages have a class named Date, so importing both directly causes ambiguity.
  2. Step 2: Use import for one and full name for the other

    Import one Date class and use the full package name for the other to avoid confusion.
  3. Final Answer:

    import java.util.Date; // Use fully qualified name for java.sql.Date in code -> Option A
  4. Quick Check:

    Import one Date, fully qualify the other [OK]
Hint: Import one class, fully qualify the other to avoid name clash [OK]
Common Mistakes:
  • Importing both classes directly causing ambiguity
  • Using wildcard imports causing confusion
  • Not using fully qualified names when needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard