Maps in Go are powerful data structures used for key-value pairs. Understanding how to declare, use, update, delete, and iterate over maps is crucial for effective Go programming.
Topics and MCQs
1. Declaring and Using Maps
How do you declare a map in Go? a) var m map[int]string b) map[int] = string{} c) var m map(string, int) d) map[]int = string{}
What does the make() function do when used with maps? a) Initializes a map with predefined values b) Creates a new map with a default size c) Creates a map with a specific capacity d) Declares a map without initializing it
How do you declare a map with an initial size in Go? a) var m = make(map[int]string, 5) b) var m map[int]string(5) c) var m map[int]string = make(5) d) map[int]string = make(5)
Which of the following is a valid map declaration? a) var m map[string]string b) var m map[string] c) map int d) var m map[int]
What is the default value for a map element if the key doesn’t exist? a) null b) zero value of the value type c) empty string d) undefined
How can you initialize a map with key-value pairs? a) map := map[string]int{“apple”: 1, “banana”: 2} b) map := {apple: 1, banana: 2} c) var map[string]int = {“apple”: 1, “banana”: 2} d) map = {“apple”: 1, “banana”: 2}
Which keyword is used to declare a map in Go? a) var b) map c) declare d) new
Which function is used to check if a key exists in a Go map? a) exists() b) contains() c) _, ok := m[key] d) checkKey()
What happens if you access a map key that doesn’t exist in Go? a) A panic occurs b) It returns the zero value of the map’s value type c) It returns nil d) It returns undefined
How do you declare a map with string keys and integer values in Go? a) map[string]int b) map[int]string c) map[int]int d) map[string]
2. Adding, Retrieving, Updating, and Deleting Elements
How do you add a new key-value pair to an existing map? a) map[key] = value b) map.add(key, value) c) map.put(key, value) d) map.addPair(key, value)
Which of the following is used to retrieve a value from a map? a) map.get(key) b) map[key] c) map.retrieve(key) d) map.find(key)
How do you update a value in a map? a) map.update(key, new_value) b) map[key] = new_value c) map.set(key, new_value) d) map.add(key, new_value)
Which of the following statements removes a key-value pair from a map? a) map.remove(key) b) map.delete(key) c) delete(map, key) d) map.deletePair(key)
What happens when you delete a key from a map in Go? a) The key is replaced with a null value b) The key-value pair is removed from the map c) The map becomes empty d) The map grows in size
Which function is used to check if a key exists in a map? a) contains() b) hasKey() c) _, ok := map[key] d) find()
What is the correct syntax to update a value in a map? a) map[key] = new_value b) map.put(key, new_value) c) map.set(key, new_value) d) map.update(key, new_value)
How can you add a new key-value pair to a map if the key does not exist? a) If the key doesn’t exist, it is automatically added with the value b) Use the add() method to insert the key-value pair c) Maps in Go don’t allow new keys to be added d) By calling insert(key, value)
What will happen if you try to delete a key from a map that does not exist? a) A panic will occur b) The map will remain unchanged c) It will throw an error d) The map will shrink in size
Which function can be used to clear all key-value pairs from a map? a) map.clear() b) delete(map) c) clear(map) d) Go maps do not have a function to clear all pairs
3. Iterating Over Maps
Which of the following is used to iterate over a map in Go? a) forEach() b) for key, value := range map { } c) for map[key] { } d) map.iterate()
What will the range keyword return when iterating over a map? a) Only the keys of the map b) Only the values of the map c) Both the keys and values of the map d) The map’s size
How do you access both the key and value in a map during iteration? a) for _, value in range map { } b) for key, value in range map { } c) for key in range map { } d) for key in map { }
Can you iterate over a map in Go in a specific order? a) Yes, by sorting the keys first b) No, the order is not guaranteed c) Yes, by using the orderedMap function d) Yes, by specifying the order in range
What does range do when iterating over a map in Go? a) Iterates in reverse order b) Iterates over the keys only c) Iterates over the map in a random order d) Iterates over the values only
How can you access only the keys when iterating over a map? a) Use range map[key] b) Use for key in range map c) Use for _, key := range map d) It’s not possible to get only keys in Go maps
How can you iterate over the values in a Go map? a) Use for value in range map b) Use for _, value := range map c) Use for key in range map d) Use map.values()
What type of data structure is a Go map? a) Linked list b) Array c) Hash table d) Binary tree
How does the iteration order work when using range on a map? a) It iterates over the keys in insertion order b) It iterates over the map in reverse order c) It iterates over the map in a random order d) It iterates over the map in sorted order
Can you modify the value of a map during iteration in Go? a) Yes, but the key cannot be modified b) No, you cannot modify a map while iterating c) Yes, you can modify both key and value d) Yes, only the value can be modified
Answers Table
QNo
Answer (Option with Text)
1
a) var m map[int]string
2
c) Creates a map with a specific capacity
3
a) var m = make(map[int]string, 5)
4
a) var m map[string]string
5
b) zero value of the value type
6
a) map := map[string]int{“apple”: 1, “banana”: 2}
7
b) map
8
c) _, ok := m[key]
9
b) It returns the zero value of the map’s value type
10
a) map[string]int
11
a) map[key] = value
12
b) map[key]
13
b) map[key] = new_value
14
c) delete(map, key)
15
b) The key-value pair is removed from the map
16
c) _, ok := map[key]
17
a) map[key] = new_value
18
a) If the key doesn’t exist, it is automatically added with the value
19
b) The map will remain unchanged
20
d) Go maps do not have a function to clear all pairs