0% found this document useful (0 votes)
12 views9 pages

Java Assessment Worksheet - Map, Set, Iterator - Set 2

The document is a Java assessment worksheet covering topics such as Maps, Sets, and Iterators. It includes brief answer questions, output prediction tasks, error identification, debugging exercises, and programming tasks related to word frequency counting and set operations. Additionally, there is a bonus section on advanced concepts like LinkedHashMap and ConcurrentModificationException.

Uploaded by

krishtheeba
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

Java Assessment Worksheet - Map, Set, Iterator - Set 2

The document is a Java assessment worksheet covering topics such as Maps, Sets, and Iterators. It includes brief answer questions, output prediction tasks, error identification, debugging exercises, and programming tasks related to word frequency counting and set operations. Additionally, there is a bonus section on advanced concepts like LinkedHashMap and ConcurrentModificationException.

Uploaded by

krishtheeba
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Assessment Worksheet

Map, Set & Iterator


Name: _________________​
Date: _________________​
Time Allowed: 90 minutes​
Total Marks: 60

Section A: Brief Answer Questions (20 marks, 2 marks


each)
1.​ What is a Map in Java? How is it different from a List?​

2.​ What is the difference between HashMap and TreeMap?​

3.​ What is a Set in Java? What is its main characteristic?​

4.​ What is the difference between HashSet and TreeSet?​

5.​ What is an Iterator in Java? Why do we need it?​

6.​ Can a Map have duplicate keys? Can it have duplicate values?​

7.​ What is the difference between HashSet and LinkedHashSet?​

8.​ How do you check if a key exists in a Map?​

9.​ What happens if you try to add a duplicate element to a Set?​

10.​What is the difference between Iterator and for-each loop?​

Section B: Output Prediction (20 marks, 4 marks each)


Question 1:
import [Link].*;

public class Test {


public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
[Link]("Apple", 10);
[Link]("Banana", 20);
[Link]("Apple", 30);
[Link]("Cherry", 40);

[Link]([Link]());
[Link]([Link]("Apple"));
[Link]([Link]("Orange"));
}
}

Output:

Question 2:
import [Link].*;

public class Test {


public static void main(String[] args) {
Set<String> set = new HashSet<>();
[Link]("Java");
[Link]("Python");
[Link]("Java");
[Link]("C++");

[Link]([Link]());
[Link]([Link]("Java"));
[Link]([Link]("Ruby"));
}
}

Output:
Question 3:
import [Link].*;

public class Test {


public static void main(String[] args) {
Set<Integer> set = new TreeSet<>();
[Link](30);
[Link](10);
[Link](50);
[Link](20);
[Link](10);

for (Integer num : set) {


[Link](num + " ");
}
}
}

Output:

Question 4:
import [Link].*;

public class Test {


public static void main(String[] args) {
Map<Integer, String> map = new TreeMap<>();
[Link](3, "Three");
[Link](1, "One");
[Link](2, "Two");
[Link](1, "NewOne");

for (Integer key : [Link]()) {


[Link](key + " : " + [Link](key));
}
}
}

Output:
Question 5:
import [Link].*;

public class Test {


public static void main(String[] args) {
List<String> list = new ArrayList<>();
[Link]("A");
[Link]("B");
[Link]("C");

Iterator<String> itr = [Link]();


while ([Link]()) {
String value = [Link]();
if ([Link]("B")) {
[Link]();
}
}

[Link](list);
}
}

Output:

Section C: Error Identification & Debugging (10 marks, 5


marks each)
Question 1: Find and fix the errors
import [Link].*;

public class Test {


public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
[Link]("One", 1);
[Link]("Two", 2);
[Link]("Three", 3);

for (String key : map) {


[Link](key + " = " + [Link](key));
}
}
}

What are the errors?

Corrected Code:

Question 2: Find and fix the errors


import [Link].*;

public class Test {


public static void main(String[] args) {
Set<String> set = new HashSet<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Cherry");

for (String fruit : set) {


if ([Link]("Banana")) {
[Link](fruit);
}
}

[Link](set);
}
}

What is the error?

Corrected Code:

Section D: Write Programs (10 marks)


Task 1: Word Frequency Counter (5 marks)

Write a Java program that:


●​ Creates a HashMap to count the frequency of words in a sentence
●​ Takes a sentence: "java is fun and java is powerful and fun"
●​ Splits the sentence into words
●​ Stores each word as key and its frequency as value
●​ Displays all words with their frequencies
●​ Finds and displays the most frequent word

Your Solution:

Task 2: Set Operations (5 marks)

Write a Java program that:

●​ Creates two HashSets:


○​ Set1: {1, 2, 3, 4, 5}
○​ Set2: {4, 5, 6, 7, 8}
●​ Performs and displays the following operations:
○​ Union: All unique elements from both sets
○​ Intersection: Common elements in both sets
○​ Difference: Elements in Set1 but not in Set2
●​ Uses Iterator to display the results

Your Solution:

Bonus Section: Advanced Concepts (10 marks, 5 marks


each)
Question 1: LinkedHashMap vs HashMap

Explain the difference between HashMap and LinkedHashMap with an example program that
demonstrates the insertion order behavior.

Your Answer & Code:

Question 2: Iterator with ConcurrentModificationException

Write a program that:


●​ Creates an ArrayList with 5 elements
●​ Demonstrates the ConcurrentModificationException when modifying collection during
for-each loop
●​ Shows the correct way to remove elements using Iterator
●​ Explain why Iterator is needed for safe removal

Your Solution & Explanation:

Quick Reference Guide


Map Interface:

●​ Purpose: Store key-value pairs


●​ Key feature: Unique keys, values can be duplicate
●​ Common implementations:
○​ HashMap: Unordered, allows one null key
○​ TreeMap: Sorted by keys, no null keys
○​ LinkedHashMap: Maintains insertion order

Map Methods:

●​ put(key, value) - Add/update key-value pair


●​ get(key) - Retrieve value for a key
●​ remove(key) - Remove entry
●​ containsKey(key) - Check if key exists
●​ containsValue(value) - Check if value exists
●​ size() - Number of entries
●​ keySet() - Get all keys
●​ values() - Get all values
●​ entrySet() - Get all key-value pairs

Set Interface:

●​ Purpose: Store unique elements


●​ Key feature: No duplicates allowed
●​ Common implementations:
○​ HashSet: Unordered, fastest
○​ TreeSet: Sorted order
○​ LinkedHashSet: Insertion order
Set Methods:

●​ add(element) - Add element (returns false if duplicate)


●​ remove(element) - Remove element
●​ contains(element) - Check if element exists
●​ size() - Number of elements
●​ clear() - Remove all elements
●​ isEmpty() - Check if empty

Iterator:

●​ Purpose: Traverse collections


●​ Key feature: Safe removal during iteration
●​ Methods:
○​ hasNext() - Check if more elements exist
○​ next() - Get next element
○​ remove() - Remove current element

Comparison:
Feature HashMap TreeMap HashSet TreeSet

Order Unordered Sorted Unordered Sorted

Null One null key No null key One null No null

Performanc O(1) O(log n) O(1) O(log n)


e

Use case Fast access Sorted Unique Sorted


keys elements unique

Common Mistakes to Avoid:


❌ Trying to modify collection during for-each loop ❌ Expecting Set to maintain insertion order
(use LinkedHashSet) ❌ Assuming Map maintains order (use LinkedHashMap or TreeMap) ❌
Not checking for null values from [Link]() ❌ Using Iterator without hasNext() check ❌
Calling [Link]() before hasNext() ❌ Trying to add duplicate keys in Map (overwrites
value)

End of Assessment
Tips:
✓ Remember: Map stores key-value pairs, Set stores unique elements ✓ HashMap/HashSet
are fastest but unordered ✓ TreeMap/TreeSet maintain sorted order ✓
LinkedHashMap/LinkedHashSet maintain insertion order ✓ Use Iterator for safe removal during
iteration ✓ Map keys must be unique, values can duplicate ✓ Set automatically prevents
duplicates ✓ Always check hasNext() before calling next() on Iterator ✓ [Link](key) returns
null if key doesn't exist ✓ Use containsKey() to check before accessing

You might also like