0
0
Javaprogramming~15 mins

Import statement usage in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Import Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code 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
💻 code 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
🔧 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
📝 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
🚀 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