MCQs on Maps | Go

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

  1. 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{}
  2. 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
  3. 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)
  4. 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]
  5. 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
  6. 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}
  7. Which keyword is used to declare a map in Go?
    a) var
    b) map
    c) declare
    d) new
  8. Which function is used to check if a key exists in a Go map?
    a) exists()
    b) contains()
    c) _, ok := m[key]
    d) checkKey()
  9. 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
  10. 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

  1. 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)
  2. 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)
  3. 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)
  4. 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)
  5. 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
  6. Which function is used to check if a key exists in a map?
    a) contains()
    b) hasKey()
    c) _, ok := map[key]
    d) find()
  7. 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)
  8. 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)
  9. 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
  10. 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

  1. 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()
  2. 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
  3. 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 { }
  4. 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
  5. 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
  6. 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
  7. 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()
  8. What type of data structure is a Go map?
    a) Linked list
    b) Array
    c) Hash table
    d) Binary tree
  9. 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
  10. 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

QNoAnswer (Option with Text)
1a) var m map[int]string
2c) Creates a map with a specific capacity
3a) var m = make(map[int]string, 5)
4a) var m map[string]string
5b) zero value of the value type
6a) map := map[string]int{“apple”: 1, “banana”: 2}
7b) map
8c) _, ok := m[key]
9b) It returns the zero value of the map’s value type
10a) map[string]int
11a) map[key] = value
12b) map[key]
13b) map[key] = new_value
14c) delete(map, key)
15b) The key-value pair is removed from the map
16c) _, ok := map[key]
17a) map[key] = new_value
18a) If the key doesn’t exist, it is automatically added with the value
19b) The map will remain unchanged
20d) Go maps do not have a function to clear all pairs
21b) for key, value := range map { }
22c) Both the keys and values of the map
23b) for key, value in range map { }
24b) No, the order is not guaranteed
25c) Iterates over the map in a random order
26b) Use for _, key := range map
27b) Use for _, value := range map
28c) Hash table
29c) It iterates over the map in a random order
30b) No, you cannot modify a map while iterating

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top