7.4. Preview Questions#
Complete these questions before class to prepare for the chapter. Use the dropdowns to check your answers.
7.4.1. True or False#
1. A List<T> in C# can grow and shrink dynamically.
Answer
True — List<T> manages its internal array automatically as items are added/removed.
2. Dictionary<K,V> allows duplicate keys.
Answer
False — Keys in a Dictionary must be unique; adding a duplicate key throws an exception.
3. List<T>.Count returns the number of elements currently in the list.
Answer
True — Count reflects the current number of items, unlike Capacity.
4. You can use foreach to iterate over both keys and values in a Dictionary.
Answer
True — Iterating a Dictionary yields KeyValuePair<K,V> entries.
5. Removing all elements from a List also frees its reserved memory capacity.
Answer
False — Clear() removes elements but keeps the allocated capacity.
7.4.2. Multiple Choice#
1. How do you add an element to a List<int> nums?
a) nums.Append(5)
b) nums.Add(5)
c) nums.Insert(0, 5)
d) nums.Push(5)
Answer
b) nums.Add(5) — Add() appends an element to the end of the list.
2. Which collection type maps unique keys to values?
a) List
b) Queue
c) Dictionary<K,V>
d) Stack
Answer
c) Dictionary<K,V> — Dictionary<K,V> is the standard key→value map in C#.
3. What method removes the first occurrence of a value from a List?
a) Delete()
b) Pop()
c) Remove()
d) Clear()
Answer
c) Remove() — Remove(value) finds and removes the first matching element.
4. How do you check if a key exists in a Dictionary<string,int> dict?
a) dict.Contains(key)
b) dict.HasKey(key)
c) dict.ContainsKey(key)
d) dict.Exists(key)
Answer
c) dict.ContainsKey(key) — ContainsKey() returns true if the key is present.
5. What is the average time complexity of a Dictionary key lookup?
a) O(n)
b) O(log n)
c) O(1)
d) O(n²)
Answer
c) O(1) — Hash-based lookup is O(1) average; O(n) worst-case due to collisions.
Footnotes