Java Collections Practice
ArrayList, HashMap, HashSet and basic operations on collections.
1
ArrayList Basics
Easy
Read N and then N integers. Store them in an ArrayList, then print the list with each element on its own line.
Input Format
First line: N. Second line: N ints.
Output Format
N lines: each element.
Sample Test Cases
|
Sample Input 1
3 10 20 30 |
Sample Output 1
10 20 30 |
Constraints
1
Explanation
Use list.add() and iterate.
2
ArrayList Size
Easy
Read N and then N strings (one per line). Add them to an ArrayList and print the size and the first element (or "Empty" if none).
Input Format
First line: N. Next N lines: strings.
Output Format
If N > 0: two lines — size, then first element. Else "Empty".
Sample Test Cases
|
Sample Input 1
3 apple banana cherry |
Sample Output 1
3 apple |
|
Sample Input 2
0 |
Sample Output 2
Empty |
Constraints
0
Explanation
size() and get(0).
1
ArrayList Sum and Max
Medium
Read N and N integers into an ArrayList. Print the sum and the maximum, space-separated.
Input Format
First line: N. Second line: N ints.
Output Format
Two ints: sum and max.
Sample Test Cases
|
Sample Input 1
5 3 7 2 9 4 |
Sample Output 1
25 9 |
Constraints
1
Explanation
Iterate or use Collections.max().
2
Remove Duplicates (ArrayList)
Medium
Read N and N integers. Use an ArrayList to print only the first occurrence of each value, in order, space-separated.
Input Format
First line: N. Second line: N ints.
Output Format
Unique values in first-seen order.
Sample Test Cases
|
Sample Input 1
6 1 2 2 3 1 4 |
Sample Output 1
1 2 3 4 |
Constraints
1
Explanation
Skip if list.contains(value).
3
HashMap Count Words
Medium
Read a sentence. Split by spaces and use a HashMap to count each word. Print each word and its count as "word: count", one per line, in order of first appearance.
Input Format
A single line containing a sentence.
Output Format
word: count lines in first-appearance order.
Sample Test Cases
|
Sample Input 1
the cat and the dog |
Sample Output 1
the: 2 cat: 1 and: 1 dog: 1 |
Constraints
1
Explanation
merge or get/put to count.
4
HashSet Unique Count
Medium
Read N and N integers. Add them to a HashSet and print the number of distinct values.
Input Format
First line: N. Second line: N ints.
Output Format
A single integer.
Sample Test Cases
|
Sample Input 1
5 1 2 1 3 2 |
Sample Output 1
3 |
Constraints
1
Explanation
set.size() gives unique count.
5
HashMap Get or Default
Medium
Read N and then N pairs (key value, strings). Store in HashMap. Then read a query key and print its value, or "Not found".
Input Format
First line: N. Next N lines: key value. Last line: query.
Output Format
The value or "Not found".
Sample Test Cases
|
Sample Input 1
3 apple red banana yellow grape purple banana |
Sample Output 1
yellow |
|
Sample Input 2
2 a 1 b 2 c |
Sample Output 2
Not found |
Constraints
1
Explanation
Use getOrDefault or containsKey.
1
Sort ArrayList
Hard
Read N and N integers into an ArrayList. Sort it ascending and print on one line.
Input Format
First line: N. Second line: N ints.
Output Format
Sorted integers on one line.
Sample Test Cases
|
Sample Input 1
5 5 2 8 1 9 |
Sample Output 1
1 2 5 8 9 |
Constraints
1
Explanation
Collections.sort(list).
2
Most Frequent Element
Hard
Read N and N integers. Use a HashMap to find the element that appears most often. If tied, print the one that appears first.
Input Format
First line: N. Second line: N ints.
Output Format
The most frequent element.
Sample Test Cases
|
Sample Input 1
7 1 3 2 3 4 3 2 |
Sample Output 1
3 |
Constraints
1
Explanation
Track max count while building the map.
3
Intersection of Two Lists
Hard
Read N, N integers for list A, M, and M integers for list B. Print the values common to both (no duplicates), space-separated, in the order they appear in A. If none, print "None".
Input Format
First line: N. Second: A. Third: M. Fourth: B.
Output Format
Common values, or "None".
Sample Test Cases
|
Sample Input 1
4 1 2 3 4 3 3 4 5 |
Sample Output 1
3 4 |
|
Sample Input 2
3 1 2 3 3 4 5 6 |
Sample Output 2
None |
Constraints
1
Explanation
Use a HashSet for B and check each A element.
4
Reverse ArrayList
Hard
Read N and N integers into an ArrayList and print them in reverse order on one line.
Input Format
First line: N. Second line: N ints.
Output Format
Reversed list on one line.
Sample Test Cases
|
Sample Input 1
4 1 2 3 4 |
Sample Output 1
4 3 2 1 |
Constraints
1
Explanation
Collections.reverse(list) or iterate backwards.
5
Second Most Frequent
Hard
Read N and N integers. Using a HashMap, print the second most frequent value. If all values are equally frequent, print "No second".
Input Format
First line: N. Second line: N ints.
Output Format
The second most frequent, or "No second".
Sample Test Cases
|
Sample Input 1
6 1 1 1 2 2 3 |
Sample Output 1
2 |
|
Sample Input 2
4 1 2 3 4 |
Sample Output 2
No second |
Constraints
2
Explanation
Rank frequencies by count.
Competitive MCQs — Java Collections
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score
0/ 33
Q1
Which collection stores elements in insertion order and allows duplicates?
Correct!
Wrong — correct answer is .
ArrayList is an ordered, duplicate-allowing list.
Q2
Which collection stores unique elements with no guaranteed order?
Correct!
Wrong — correct answer is .
HashSet stores unique elements in hash order.
Q3
Which interface represents a key-value map?
Correct!
Wrong — correct answer is .
Map (e.g. HashMap) maps keys to values.
Q4
What is the output of this code?
java
1
2
3
4
2
3
4
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
System.out.println(list.get(1));Correct!
Wrong — correct answer is .
get(1) returns the second element, "B".
Q5
What is the output of this code?
ArrayList list = new ArrayList<>();
list.add("A");
list.add("B");
System.out.println(list.get(1));
ArrayList
list.add("A");
list.add("B");
System.out.println(list.get(1));
Correct!
Wrong — correct answer is .
get(1) returns the second element, "B".
Q6
Which method returns the number of elements in a collection?
Correct!
Wrong — correct answer is .
Collections use size(); arrays use length.
Q7
What is the output of this code?
java
1
2
3
4
2
3
4
HashSet<Integer> set = new HashSet<>();
set.add(1);
set.add(1);
System.out.println(set.size());Correct!
Wrong — correct answer is .
Duplicates are ignored, so the set has one element.
Q8
What is the output of this code?
HashSet set = new HashSet<>();
set.add(1);
set.add(1);
System.out.println(set.size());
HashSet
set.add(1);
set.add(1);
System.out.println(set.size());
Correct!
Wrong — correct answer is .
Duplicates are ignored, so the set has one element.
Q9
Which Map implementation maintains keys in sorted order?
Correct!
Wrong — correct answer is .
TreeMap orders keys by natural ordering.
Q10
Which List implementation is best for frequent insertions in the middle?
Correct!
Wrong — correct answer is .
LinkedList node links make middle insertion cheap.
Q11
What is the output of this code?
java
1
2
3
2
3
HashMap<String, Integer> m = new HashMap<>();
m.put("x", 10);
System.out.println(m.get("x"));Correct!
Wrong — correct answer is .
The value for key "x" is 10.
Q12
What is the output of this code?
HashMap m = new HashMap<>();
m.put("x", 10);
System.out.println(m.get("x"));
HashMap
m.put("x", 10);
System.out.println(m.get("x"));
Correct!
Wrong — correct answer is .
The value for key "x" is 10.
Q13
Which is a legacy synchronized List?
Correct!
Wrong — correct answer is .
Vector is the synchronized legacy counterpart of ArrayList.
Q14
Which collection keeps elements in sorted order?
Correct!
Wrong — correct answer is .
TreeSet stores elements in their natural sorted order.
Q15
What is the output of this code?
ArrayList list = new ArrayList<>();
list.add("A");
System.out.println(list.size());
ArrayList
list.add("A");
System.out.println(list.size());
Correct!
Wrong — correct answer is .
One added element makes the size 1.
Q16
What is the output of this code?
ArrayList n = new ArrayList<>();
n.add(1);
n.add(2);
n.remove(0);
System.out.println(n.get(0));
ArrayList
n.add(1);
n.add(2);
n.remove(0);
System.out.println(n.get(0));
Correct!
Wrong — correct answer is .
remove(0) deletes the first element, leaving 2 at index 0.
Q17
Which method removes all elements?
Correct!
Wrong — correct answer is .
clear() empties the collection.
Q18
What is the output of this code?
ArrayList list = new ArrayList<>();
list.add("x");
System.out.println(list.contains("x"));
ArrayList
list.add("x");
System.out.println(list.contains("x"));
Correct!
Wrong — correct answer is .
contains() returns true because "x" is present.
Q19
Which interface represents an ordered collection allowing duplicates?
Correct!
Wrong — correct answer is .
List is the ordered, duplicate-allowing interface.
Q20
Which interface stores unique elements?
Correct!
Wrong — correct answer is .
Set implementations reject duplicates.
Q21
What is the output of this code?
HashMap m = new HashMap<>();
m.put("a", 1);
m.put("a", 2);
System.out.println(m.get("a"));
HashMap
m.put("a", 1);
m.put("a", 2);
System.out.println(m.get("a"));
Correct!
Wrong — correct answer is .
Re-putting the same key overwrites the old value with 2.
Q22
What is the output of this code?
HashSet s = new HashSet<>();
s.add(1);
s.add(2);
s.add(1);
System.out.println(s.size());
HashSet
s.add(1);
s.add(2);
s.add(1);
System.out.println(s.size());
Correct!
Wrong — correct answer is .
Duplicates are ignored, so the set holds 1 and 2.
Q23
Which method gets a value by key from a HashMap?
Correct!
Wrong — correct answer is .
map.get(key) returns the mapped value or null.
Q24
What is the output of this code?
ArrayList list = new ArrayList<>();
for (int i = 1; i <= 3; i++) list.add(i * 10);
System.out.println(list);
ArrayList
for (int i = 1; i <= 3; i++) list.add(i * 10);
System.out.println(list);
Correct!
Wrong — correct answer is .
Each element is stored as 10, 20, 30.
Q25
Which collection is best for frequent insertion/deletion in the middle?
Correct!
Wrong — correct answer is .
Linked nodes make middle edits cheap compared to shifting arrays.
Q26
What is autoboxing?
Correct!
Wrong — correct answer is .
int becomes Integer automatically, e.g., list.add(5).
Q27
What is the output of this code?
ArrayList list = new ArrayList<>();
list.add(5);
int x = list.get(0);
System.out.println(x);
ArrayList
list.add(5);
int x = list.get(0);
System.out.println(x);
Correct!
Wrong — correct answer is .
Unboxing turns the Integer back into int 5.
Q28
Which method returns true when a list has no elements?
Correct!
Wrong — correct answer is .
isEmpty() checks whether the collection has zero elements.
Q29
What is the output of this code?
TreeSet t = new TreeSet<>();
t.add(3);
t.add(1);
t.add(2);
System.out.println(t.first());
TreeSet
t.add(3);
t.add(1);
t.add(2);
System.out.println(t.first());
Correct!
Wrong — correct answer is .
TreeSet orders elements, so first() is the smallest: 1.
Q30
Which Map keeps keys sorted?
Correct!
Wrong — correct answer is .
TreeMap orders entries by key.
Q31
What is the output of this code?
HashMap m = new HashMap<>();
System.out.println(m.get("missing"));
HashMap
System.out.println(m.get("missing"));
Correct!
Wrong — correct answer is .
A missing key returns null rather than throwing.
Q32
Which is an ordered (by insertion) Map?
Correct!
Wrong — correct answer is .
LinkedHashMap preserves insertion order.
Q33
Which collection is ideal for a LIFO stack?
Correct!
Wrong — correct answer is .
ArrayDeque supports push/pop for stack behavior.