Module 4: Final project and assessment: Data structures and algorithms
Course quiz: Data structures and algorithms
Practice Assignment
1. You are developing a weather prediction system that needs to store daily temperature values for a week. Which of the following correctly declares an array to store daily temperatures?
- int temperatures = new int[7];
- int[] temperatures = int[7];
- int[] temperatures = new int[7];
- int temperatures[7] = new int();
2. You're tasked with developing a to-do list application that manages tasks using an ArrayList. Which method would you use to remove a task named "Buy Groceries"?
- tasks.delete(“Buy Groceries”);
- tasks.clear(“Buy Groceries”);
- tasks.remove(“Buy Groceries”);
- tasks.pop(“Buy Groceries”);
3. You need to find the index of the number 15 in a sorted array of integers. Given the sorted array int[] sortedArray = {1, 5, 10, 15, 20};, which of the following code snippets correctly performs a binary search for the number 15?
4. Which code snippet correctly checks for balanced parentheses in the string "{[()]}" using a stack?
5. What is the most efficient way to add a new node at the beginning of a singly linked list?
- Set the new node’s next reference to the current head and update the head to the new node.
- Traverse to the end of the list and then insert the node.
- Traverse to the middle of the list and insert the node there.
- Set the current tail’s next reference to the new node.
6. What is a characteristic of a doubly linked list that a singly linked list does not have?
- Nodes contain references to the next node only.
- Nodes are stored in contiguous memory locations.
- Each node has two references: one to the next node and one to the previous node.
- It can only be traversed in one direction.
7. In a HashMap map, you want to update the value associated with the key id only if the key already exists. Which method would you use?
- map.containsKey(“id”, newValue);
- map.update(“id”, newValue);
- map.put(“id”, newValue);
- *A:map.replace(“id”, newValue);
8. What does the assertEquals method do in JUnit?
- Compares two integers.
- Checks if a collection is empty.
- Asserts that two values are equal.
- Throws an exception.
9. Which sorting algorithm is not suitable for very large datasets due to its quadratic time complexity?
- Merge Sort
- Quick Sort
- Insertion Sort
- Heap Sort
10. How does Merge Sort operate?
- It repeatedly selects the smallest element and moves it to the beginning.
- It divides the array into halves, sorts each half, and then merges them.
- It swaps adjacent elements if they are in the wrong order.
- It partitions the array around a pivot element.
11. How can the worst-case scenario for Quick Sort be avoided?
- Always choose the first element as a pivot.
- Increase the size of the array.
- Use randomization or the “median of three” strategy.
- Use a recursive function with a higher depth.
12. In a sorted array of 1000 elements, how many comparisons would binary search take in the worst case?
- 1
- 10
- 100
- 1000
13. Which method is used to retrieve the greatest key less than or equal to the given key in a TreeMap?
- map.floorEntry(K key);
- map.ceilingEntry(K key);
- map.lowerEntry(K key);
- map.higherEntry(K key);
14. What is a key characteristic of a LinkedHashMap compared to a HashMap?
- It maintains insertion order of elements.
- It sorts elements in descending order.
- It allows duplicate keys.
- It does not permit null keys.
15. True or False: In a doubly linked list, theremoveFirst() method deletes the first node by setting the prev reference of the second node to null.
- True
- False