Sample questions
Question 1Easy
What is printed?
m := map[string]int{"a": 1}
fmt.Println(m["x"])
- 0
- nil
- Panic: key not found
- Compile error
Answer: A. 0
Explanation
Reading a missing key returns the zero value of the value type. Use v, ok := m["x"] to tell if it existed.
Step-by-step solution
x is not in the map.- Lookup returns the zero value for int.
- Output is 0.
Why the other options are wrong
- B. int cannot be nil.
- C. Missing keys do not panic.
- D. Reading is legal.
Tip: Use the comma ok form to test presence.
Question 2Easy
Is a Go map safe for concurrent writes from multiple goroutines?
- Yes, maps are always thread-safe
- Only on 64-bit systems
- Only if the keys are strings
- No; use a mutex or sync.Map
Answer: D. No; use a mutex or sync.Map
Explanation
Concurrent map writes can crash the program with 'concurrent map writes'.
Step-by-step solution
- Go maps are not thread-safe for concurrent writes.
- Writing to a map from multiple goroutines can cause data races.
- To safely write to a map concurrently, use a mutex or sync.Map.
- So the correct answer is No; use a mutex or sync.Map.
Why the other options are wrong
- A. Go maps are not thread-safe; concurrent writes can cause a data race or crash.
- B. The 64-bit platform does not make maps safe; concurrent writes are unsafe on every system.
- C. Key type does not matter; concurrent writes are unsafe for any key type.
Tip: Always synchronize map access in Go when multiple goroutines write.
Question 3Easy
What is the output?
package main
import "fmt"
func main() {
m := map[string]int{"a": 1}
fmt.Println(m["b"])
}
- Compile error
- Runtime panic
- nil
- 0
Answer: D. 0
Explanation
Looking up a missing key returns the value type's zero value.
Step-by-step solution
- The map m has key 'a' with value 1.
- Looking up 'b' which is not in the map returns the zero value for int.
- Zero value of int is 0.
- So the output is 0.
Why the other options are wrong
- A. Reading a missing key is valid in Go and does not fail to compile.
- B. Runtime panic is wrong: reading a missing key returns the zero value, which is 0 here.
- C. nil is not the result, because int values use 0 as their zero value, not nil.
Tip: Remember: missing keys return zero values in maps.
Question 4Easy
What can you assume about the iteration order of a Go map with range?
- It is always insertion order
- It is always sorted by key, because Go maps are implemented as balanced binary trees
- Nothing; it is unspecified and may differ between runs
- It is reverse insertion order
Answer: C. Nothing; it is unspecified and may differ between runs
Explanation
Go deliberately randomises map iteration; sort the keys if you need a stable order.
Step-by-step solution
- Go maps use hash tables, not trees or lists.
- The runtime randomises the starting bucket for each range loop.
- This means the order changes every time the code runs.
- Therefore, the iteration order is unspecified and may differ between runs.
Why the other options are wrong
- A. Go does not keep insertion order; a range over a map is randomised on purpose.
- B. Go maps are hash tables, not balanced trees, so they have no sorted key order.
- D. Reverse insertion order is not guaranteed either; the order is unspecified and can change.
Tip: Always sort map keys explicitly if you need a deterministic output order.
Question 5Medium
What is the output?
package main
import "fmt"
func main() {
var m map[string]int
fmt.Println(len(m))
m["x"] = 1
fmt.Println(m)
}
- Prints nothing and exits
- Prints 0, then panics
- Compile error
- 0 map[x:1]
Answer: B. Prints 0, then panics
Explanation
Reading from a nil map is fine, but writing panics: 'assignment to entry in nil map'. Use make first.
Step-by-step solution
- The variable
m is declared but not initialized, so it is a nil map. - Calling
len(m) on a nil map returns 0 without error. - The first
fmt.Println prints 0. - The line
m["x"] = 1 attempts to write to a nil map. - Writing to a nil map causes a runtime panic.
- The program prints 0, then panics.
Why the other options are wrong
- A. A nil map has length 0, so the first print runs; the program does not exit silently.
- C. Compile error is wrong: a nil map, len, and fmt.Println are all valid Go.
- D. The map is nil, so the write panics before any printing of map[x:1].
Tip: Always use make(map[string]int) to initialize a map before writing to it.
Question 6Medium
What happens when this runs?
var m map[string]int
m["a"] = 1
fmt.Println(m)
- It prints map[a:1]
- It prints map[]
- It does not compile
- It panics because the map is nil
Answer: D. It panics because the map is nil
Explanation
A nil map can be read but not written. Create it with make(map[string]int) or a literal first.
Step-by-step solution
var m map[string]int is nil.m["a"] = 1 assigns into it.- Runtime panic: assignment to entry in nil map.
Why the other options are wrong
- A. The write fails, so nothing is stored.
- B. The program stops before printing.
- C. The code compiles; the failure is at run time.
Question 7Medium
What is the output?
package main
import "fmt"
func main() {
m := map[string]int{"a": 0}
v, ok := m["a"]
w, ok2 := m["z"]
fmt.Println(v, ok, w, ok2)
}
- 0 true 0 false
- 0 true nil false
- 0 false 0 false
- Compile error
Answer: A. 0 true 0 false
Explanation
The comma-ok form distinguishes a stored zero value from a missing key.
Step-by-step solution
- The map m is created with key "a" mapped to 0.
- Accessing m["a"] returns v=0 and ok=true because the key exists.
- Accessing m["z"] returns w=0 and ok2=false since the key is absent.
- fmt.Println prints v, ok, w, ok2 resulting in 0 true 0 false.
Why the other options are wrong
- B. nil is not returned; a missing key gives 0, not nil.
- C. ok is true for key a because it exists, so the first ok is not false.
- D. The code compiles; the two-value map lookups are valid and all four names are used.
Tip: Always use the second boolean result to test key existence in a map.
Question 8Medium
What is the output?
package main
import "fmt"
func main() {
m := map[string]int{"a": 1, "b": 2}
delete(m, "a")
delete(m, "zzz")
fmt.Println(len(m))
}
- 2
- 1
- 0
- Runtime panic
Answer: B. 1
Explanation
delete removes a key and is a no-op for a missing key.
Step-by-step solution
- M starts with keys 'a' and 'b'.
- delete(m, 'a') removes 'a', leaving 'b'.
- delete(m, 'zzz') does nothing as 'zzz' is not present.
- len(m) counts remaining keys: 1.
- Final answer: 1.
Why the other options are wrong
- A. delete removes key a, so the length drops from 2 to 1, not stays at 2.
- C. Zero is wrong: deleting key a leaves key b in the map, so the length is not 0.
- D. A missing key in delete is not a panic; Go silently does nothing for an absent key.
Tip: delete() ignores missing keys. Use len() to count remaining entries.
Question 9Hard
What is the output?
package main
import "fmt"
type P struct{ X int }
func main() {
m := map[string]P{"a": {1}}
m["a"].X = 5
fmt.Println(m)
}
- Runtime panic
- map[a:{5}]
- Compile error
- map[a:{1}]
Answer: C. Compile error
Explanation
Map elements are not addressable, so you cannot assign to a field of a struct value in a map; reassign the whole struct or store pointers.
Step-by-step solution
- The map m holds values of type P, not pointers.
- Accessing m["a"] returns a copy of the struct, which is not addressable.
- Assigning to m["a"].X attempts to modify a non‑addressable value, which is illegal.
- Compile error.
Why the other options are wrong
- A. The code is rejected before it runs, so there is no runtime panic.
- B. Go does not allow this assignment, so the map never shows {5}.
- D. The code does not compile, so nothing prints; {1} is never shown as output.
Tip: Update the entire struct or store a pointer to modify fields inside a map.
Question 10Hard
What is the output?
package main
import "fmt"
func add(m map[string]int) {
m["k"] = 42
}
func main() {
m := map[string]int{}
add(m)
fmt.Println(m["k"])
}
- Compile error
- Runtime panic
- 0
- 42
Answer: D. 42
Explanation
A map value refers to the same underlying hash table, so changes in the function are visible to the caller.
Step-by-step solution
- The
add function receives a map by reference. - It sets
m["k"] = 42 which modifies the original map. - The
main function prints m["k"] which is now 42. - So the output is 42.
Why the other options are wrong
- A. The code compiles; passing a map to a function and assigning a key are both valid Go.
- B. Writing to a non-nil map inside a function is safe, so there is no panic here.
- C. The key was written before the read, so the lookup returns 42, not the zero value.
Tip: Remember maps are mutable and passed by reference in Go.
Practise all 12 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →