Golang Maps Quiz

Golang
0 Passed
0% acceptance

35 comprehensive questions on Golang maps, covering map creation, key existence checking, entry deletion, iteration order, and reference behavior — with 18 code examples demonstrating map operations and common patterns.

35 Questions
~70 minutes
1

Question 1

What is a map in Golang?

A
An unordered collection of key-value pairs
B
An ordered list of elements
C
A fixed-size array
D
A reference to a slice
2

Question 2

How do you create a map using make()?

A
m := make(map[string]int)
B
m := map[string]int{}
C
m := new(map[string]int)
D
m := &map[string]int{}
3

Question 3

What is the difference between a nil map and an empty map?

A
Nil map panics on assignment, empty map works normally
B
They are identical
C
Empty map panics, nil map works
D
Nil map has zero length, empty map doesn't
4

Question 4

What happens when you assign to a nil map?

go
package main

func main() {
    var m map[string]int
    m["key"] = 1  // This line
}
A
Runtime panic
B
Assignment succeeds
C
Compilation error
D
Map gets initialized
5

Question 5

How do you create a map with initial values?

go
package main

import "fmt"

func main() {
    m := map[string]int{
        "a": 1,
        "b": 2,
    }
    fmt.Println(m)
}
A
Map literal syntax with key-value pairs
B
Cannot initialize with values
C
Use make() with initial values
D
Use append() function
6

Question 6

What is the comma ok idiom used for?

A
Checking if a key exists in a map
B
Checking if a map is nil
C
Checking map length
D
Checking map capacity
7

Question 7

What does this code print?

go
package main

import "fmt"

func main() {
    m := map[string]int{"a": 0}
    if v, ok := m["a"]; ok {
        fmt.Println("Found:", v)
    }
    if v, ok := m["b"]; ok {
        fmt.Println("Found:", v)
    } else {
        fmt.Println("Not found")
    }
}
A
Found: 0\nNot found
B
Found: 0\nFound: 0
C
Not found\nNot found
D
Compilation error
8

Question 8

How do you delete an entry from a map?

A
delete(m, key)
B
m[key] = nil
C
remove(m, key)
D
m.delete(key)
9

Question 9

What happens when you delete a non-existent key?

go
package main

func main() {
    m := make(map[string]int)
    delete(m, "nonexistent")  // This is safe
}
A
No operation - no panic
B
Runtime panic
C
Compilation error
D
Map becomes nil
10

Question 10

Is map iteration order guaranteed?

A
No, iteration order is randomized
B
Yes, insertion order is preserved
C
Yes, sorted by keys
D
Depends on map size
11

Question 11

What does this iteration code demonstrate?

go
package main

import "fmt"

func main() {
    m := map[string]int{"c": 3, "a": 1, "b": 2}
    for k, v := range m {
        fmt.Printf("%s:%d ", k, v)
    }
}
A
Order may vary between runs
B
Always prints in insertion order
C
Always prints in sorted order
D
Compilation error
12

Question 12

Are maps reference types?

A
Yes, maps are reference types like slices
B
No, maps are value types
C
Maps are primitive types
D
Depends on how they're created
13

Question 13

What happens when you pass a map to a function?

go
package main

import "fmt"

func modify(m map[string]int) {
    m["new"] = 99
}

func main() {
    m := make(map[string]int)
    modify(m)
    fmt.Println(len(m))  // 1
}
A
Modification is visible to caller
B
Map remains empty
C
Runtime panic
D
Compilation error
14

Question 14

In a caching system, you're implementing an LRU cache using maps. Why is the randomized iteration order actually beneficial?

A
Prevents algorithms from accidentally depending on insertion order
B
Makes iteration faster
C
Ensures fairness in cache eviction
D
Not beneficial
15

Question 15

What is the zero value of a map variable?

A
nil
B
Empty map
C
Map with zero capacity
D
Compilation error
16

Question 16

How do you check if a map is empty?

go
package main

import "fmt"

func main() {
    var m map[string]int
    if len(m) == 0 {
        fmt.Println("Empty")
    }
}
A
Use len(m) == 0
B
Use m == nil
C
Use cap(m) == 0
D
Cannot check emptiness
17

Question 17

What happens when you range over a nil map?

go
package main

func main() {
    var m map[string]int
    for k, v := range m {
        // This is safe
    }
}
A
No iteration - loop doesn't execute
B
Runtime panic
C
Compilation error
D
Infinite loop
18

Question 18

Can map keys be of any type?

A
No, keys must be comparable types
B
Yes, any type can be a key
C
Only built-in types
D
Only string and int types
19

Question 19

What happens when you use a slice as a map key?

go
package main

func main() {
    m := make(map[[]int]int)  // This fails
}
A
Compilation error - slices are not comparable
B
Works normally
C
Runtime panic
D
Slice gets converted
20

Question 20

In a configuration management system, you're storing user preferences in maps. If a user hasn't set a preference, how should you handle the zero value vs missing key distinction?

A
Use comma ok idiom to distinguish missing keys from zero values
B
Use a pointer type for values
C
Always initialize all preferences
D
Use a special sentinel value
21

Question 21

What is map capacity and how does it work?

A
Maps grow automatically, capacity is not directly controllable
B
make(map[K]V, capacity) pre-allocates space
C
Capacity limits maximum size
D
Maps don't have capacity
22

Question 22

How do you create a map that maps strings to functions?

go
package main

import "fmt"

func main() {
    m := map[string]func() string{
        "hello": func() string { return "Hello" },
    }
    fmt.Println(m["hello"]())
}
A
Map[string]func() string with function literals
B
Cannot store functions in maps
C
Use interface{} for values
D
Compilation error
23

Question 23

What does this code demonstrate about map assignment?

go
package main

import "fmt"

func main() {
    m1 := map[string]int{"a": 1}
    m2 := m1
    m2["b"] = 2
    fmt.Println(len(m1))  // 2
}
A
Maps share underlying data after assignment
B
m2 gets a copy of m1
C
Assignment fails
D
Compilation error
24

Question 24

In a word frequency counter, you're using a map[string]int to count occurrences. Why is this efficient?

A
Maps provide O(1) average-case lookup and insertion
B
Maps automatically sort entries
C
Maps compress duplicate values
D
Not efficient for this use case
25

Question 25

What happens when you modify a map during iteration?

go
package main

import "fmt"

func main() {
    m := map[string]int{"a": 1, "b": 2}
    for k := range m {
        if k == "a" {
            delete(m, "b")
            m["c"] = 3
        }
        fmt.Print(k, " ")
    }
}
A
Safe - modifications during iteration are allowed
B
Runtime panic
C
Only deletions are allowed
D
Compilation error
26

Question 26

How do you implement a set using maps?

go
package main

import "fmt"

func main() {
    set := make(map[string]bool)
    set["apple"] = true
    set["banana"] = true
    if set["apple"] {
        fmt.Println("Apple is in set")
    }
}
A
Use map[K]bool where presence indicates membership
B
Use map[K]struct{} for memory efficiency
C
Cannot implement sets with maps
D
Use slices instead
27

Question 27

What is the output of this nested map scenario?

go
package main

import "fmt"

func main() {
    m := make(map[string]map[string]int)
    m["users"] = make(map[string]int)
    m["users"]["alice"] = 25
    fmt.Println(m["users"]["alice"])
}
A
25
B
0
C
Runtime panic
D
Compilation error
28

Question 28

In a concurrent web server, multiple goroutines are updating a shared map of request counts. What synchronization is needed?

A
Mutex protection for all map operations
B
No synchronization - maps are thread-safe
C
Use sync.Map instead
D
Use channels to serialize access
29

Question 29

What does the built-in len() function return for maps?

A
Number of key-value pairs in the map
B
Capacity of the map
C
Memory usage
D
Always 0 for nil maps
30

Question 30

How do you clear all entries from a map?

go
package main

func main() {
    m := map[string]int{"a": 1, "b": 2}
    // To clear:
    for k := range m {
        delete(m, k)
    }
    // or
    m = make(map[string]int)  // new map
}
A
Iterate and delete, or assign new map
B
Use clear() function
C
Set to nil
D
Cannot clear maps
31

Question 31

What happens when you compare two maps?

go
package main

func main() {
    m1 := map[string]int{"a": 1}
    m2 := map[string]int{"a": 1}
    if m1 == m2 {  // This fails
        // ...
    }
}
A
Compilation error - maps are not comparable
B
Compares contents and returns true
C
Compares references
D
Runtime panic
32

Question 32

In a JSON API, you're unmarshaling data into a map[string]interface{}. How do you safely access nested values?

A
Use type assertions and check ok value
B
Direct access with []
C
Use reflection
D
Cannot access safely
33

Question 33

What is the performance characteristic of map operations?

A
O(1) average case for lookup, insert, delete
B
O(log n) for all operations
C
O(n) for lookup
D
Depends on key type
34

Question 34

How do you iterate over maps in a deterministic order?

go
package main

import (
    "fmt"
    "sort"
)

func main() {
    m := map[string]int{"c": 3, "a": 1, "b": 2}
    keys := make([]string, 0, len(m))
    for k := range m {
        keys = append(keys, k)
    }
    sort.Strings(keys)
    for _, k := range keys {
        fmt.Printf("%s:%d ", k, m[k])
    }
}
A
Collect keys, sort them, then iterate
B
Cannot achieve deterministic order
C
Use ordered map type
D
Iteration is always sorted
35

Question 35

What is the most important thing to remember about maps in concurrent programs?

A
Maps are not thread-safe - synchronize access
B
Maps automatically synchronize
C
Only read operations are thread-safe
D
Concurrency doesn't affect maps

QUIZZES IN Golang