Assignment Test 1 Questions and Answers
Assignment Test 1 Questions and Answers
Marks : 10 marks
Theory : 4 marks ; Programming : 6 marks
Theory Questions
ArrayList
Basic Understanding:
An ArrayList is a resizable array implementation of the List interface. Unlike arrays, ArrayLists can
grow and shrink in size. They are part of the java.util package.
What is the time complexity of adding an element to an ArrayList? What about getting an element?
Adding an element to the end of an ArrayList is O(1) on average (amortized), but can be O(n) if
resizing occurs. Accessing an element is O(1).
Performance:
When would you choose an ArrayList over an array? What are the advantages?
You would choose an ArrayList over an array when you need dynamic resizing, easy insertion and
deletion, or additional helpful methods.
Manipulation:
What happens if you try to remove an element that does not exist?
If the element does not exist, remove(Object o) returns false.
Capacity Management:
Thread Safety:
Is ArrayList thread-safe? If not, how can you make it safe for use in a multi-threaded environment?
LinkedList
Basic Understanding:
A LinkedList is a doubly-linked list implementation of the List interface. It allows node insertion and
deletion without resizing.
Can you explain the difference between a singly linked list and a doubly linked list?
A singly linked list only has pointers to the next node, while a doubly linked list has pointers to both
previous and next nodes.
Performance:
What are the time complexities for adding and removing elements in a LinkedList compared to
ArrayList?
Adding/removing elements at the ends (head/tail) is O(1), while accessing elements is O(n) (linear
time).
Manipulation:
To implement a queue, use add(), remove(), and peek() methods on the LinkedList.
Traversal:
Can you implement a method to find the middle element of a LinkedList efficiently?
To find the middle element, use two pointers: move one pointer by one step and another by two steps.
When the fast pointer reaches the end, the slow pointer will be at the middle.
Memory Considerations:
HashSet
Basic Understanding:
Performance:
What is the average time complexity for adding, removing, and checking for the presence of an element
in a HashSet?
Average time complexities for add, remove, and contains are O(1).
Iterating:
How can you iterate through the elements of a HashSet? What order do elements appear in?
Usage:
Explain a scenario where the HashSet might not be suitable for a specific application.
A HashSet may not be suitable when you need to maintain a sorted order.
Thread Safety:
HashMap
Basic Understanding:
Hashtable (legacy based/ rather old class) is synchronized, meaning it is thread-safe and can be used
safely in a multi-threaded environment without external synchronization.
HashMap (non-legacy based) is not synchronized, so it is preferable in non-threaded applications due
to its performance advantages.
In a HashMap, every entry consists of a key, which acts as an identifier, and a value, which is the data
associated with that key. This structure is fundamental to how data is stored, retrieved, and manipulated
in a HashMap.
Keys can be any object type, but it is standard practice to use immutable objects (like String, Integer,
etc.)
Value:
The data associated with a specific key. Unlike keys, values can be duplicated, meaning multiple keys
can point to the same value. Values can also be any object type, allowing for flexibility in what data can
be stored within the HashMap.
Performance:
What are the average time complexities for get, put, and remove operations in a HashMap?
Average time complexities for get, put, and remove are O(1).
What happens to the elements of a HashMap when its load factor exceeds a certain threshold?
If the load factor exceeds (commonly 0.75), the HashMap resizes and rehashes its entries, which can be
expensive.
Iterating:
How can you iterate through both keys and values in a HashMap?
Iterate through keys using keySet(), values using values(), and both keys and values using entrySet().
Can you discuss the difference between using keySet(), values(), and entrySet() methods?
keySet() - set of keys
values() - set of values
entrySet() is generally the most efficient way to iterate over a HashMap.
Usage:
Mutable objects as keys can lead to problems if their state changes while they are in the HashMap.
Programming Questions
Q1 ArrayListExample
package Collections_Framework;
import java.util.ArrayList;
// Accessing an element
int firstElement = numbers.get(0);
System.out.println("First Element: " + firstElement); // Output: 10
// Removing an element
numbers.remove(2); // Removes the element at index 2 (30)
System.out.println("After Removal: " + numbers);
}
}
o/p
ArrayList: [10, 20, 30, 40]
First Element: 10
All Elements: 10 20 30 40
After Removal: [10, 20, 40]
Q2. LinkedListExample
package Collections_Framework;
import java.util.LinkedList;
// Accessing elements
String firstFruit = fruits.get(0);
System.out.println("First Fruit: " + firstFruit); // Output: Apple
// Removing an element
fruits.remove(1); // Removes "Banana"
System.out.println("After Removal: " + fruits);
}
}
o/p
package Collections_Framework;
import java.util.HashSet;
// Removing an element
colors.remove("Green");
System.out.println("After Removal: " + colors);
}
}
o/p
import java.util.HashMap;
// Modifying a value
map.put("Alice", 31); // Updating Alice's age
Bob's age: 25
{Bob=25, Alice=31}