0
0
Goprogramming~20 mins

Adding and updating values in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Go Map Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Go code when adding and updating map values?
Consider the following Go program that adds and updates values in a map. What will it print?
Go
package main
import "fmt"
func main() {
    scores := map[string]int{"Alice": 10, "Bob": 15}
    scores["Alice"] += 5
    scores["Charlie"] = 20
    fmt.Println(scores)
}
Amap[Alice:15 Bob:15 Charlie:20]
Bmap[Alice:10 Bob:15 Charlie:20]
Cmap[Alice:15 Bob:15]
Dmap[Alice:5 Bob:15 Charlie:20]
Attempts:
2 left
💡 Hint
Remember that updating a map value with += adds to the existing value.
Predict Output
intermediate
2:00remaining
What happens when you update a map value for a non-existent key?
Look at this Go code snippet. What will be the output?
Go
package main
import "fmt"
func main() {
    ages := map[string]int{"John": 30}
    ages["Mary"] += 5
    fmt.Println(ages)
}
Amap[John:30 Mary:5]
Bmap[John:30 Mary:0]
Cmap[John:30]
DCompilation error
Attempts:
2 left
💡 Hint
When you add to a zero value in a map, Go initializes it to zero first.
🔧 Debug
advanced
2:00remaining
Why does this Go code cause a runtime panic when updating a map?
Examine the code below. It causes a runtime panic. What is the reason?
Go
package main
func main() {
    var data map[string]int
    data["key"] = 1
}
AThe map must be declared with 'make' keyword to allow updates.
BThe key 'key' is invalid for map assignment.
CThe map 'data' is nil and not initialized before assignment.
DThe map keys must be integers, not strings.
Attempts:
2 left
💡 Hint
Maps must be initialized before you can add or update values.
Predict Output
advanced
2:00remaining
What is the output after updating nested map values?
Given this Go code with nested maps, what will be printed?
Go
package main
import "fmt"
func main() {
    users := map[string]map[string]int{
        "user1": {"score": 10},
        "user2": {"score": 20},
    }
    users["user1"]["score"] += 5
    users["user3"] = map[string]int{"score": 15}
    fmt.Println(users)
}
ACompilation error due to nested map assignment
Bmap[user1:map[score:15] user2:map[score:20] user3:map[score:15]]
Cmap[user1:map[score:15] user2:map[score:20]]
Dmap[user1:map[score:10] user2:map[score:20] user3:map[score:15]]
Attempts:
2 left
💡 Hint
You can update nested map values by accessing keys step by step.
🧠 Conceptual
expert
2:00remaining
How many items are in the map after these operations?
Consider this Go code snippet. How many key-value pairs does the map contain at the end?
Go
package main
import "fmt"
func main() {
    m := make(map[int]string)
    for i := 0; i < 5; i++ {
        m[i] = fmt.Sprintf("val%d", i)
    }
    m[2] = "updated"
    delete(m, 4)
    fmt.Println(len(m))
}
A6
B5
C3
D4
Attempts:
2 left
💡 Hint
Remember that deleting a key reduces the map size by one.