Bird
Raised Fist0
Goprogramming~20 mins

Zero values in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Go Zero Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of uninitialized variables
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    var a int
    var b string
    var c bool
    fmt.Printf("%d %q %t", a, b, c)
}
A0 "" false
B0 "0" false
C0 "nil" false
D0 "" true
Attempts:
2 left
๐Ÿ’ก Hint
Think about what Go assigns to variables when you don't set them.
โ“ Predict Output
intermediate
2:00remaining
Zero value of a slice
What will this Go program print?
Go
package main
import "fmt"
func main() {
    var s []int
    fmt.Println(s == nil)
    fmt.Println(len(s))
}
A
false
0
B
true
-1
C
true
0
D
false
-1
Attempts:
2 left
๐Ÿ’ก Hint
Slices have a zero value of nil and length zero.
โ“ Predict Output
advanced
2:00remaining
Zero value of a map and its behavior
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    var m map[string]int
    fmt.Println(m == nil)
    fmt.Println(m["key"])
    m["key"] = 10
}
A
false
0
panic: assignment to entry in nil map
B
false
0
no panic
C
true
0
no panic
D
true
0
panic: assignment to entry in nil map
Attempts:
2 left
๐Ÿ’ก Hint
Maps have zero value nil, reading from nil map is allowed, writing causes panic.
โ“ Predict Output
advanced
2:00remaining
Zero value of a struct with embedded fields
What will this Go program print?
Go
package main
import "fmt"
type Inner struct {
    X int
}
type Outer struct {
    Inner
    Y string
}
func main() {
    var o Outer
    fmt.Printf("%d %q", o.X, o.Y)
}
A0 ""
B0 "nil"
C0 "0"
Dpanic at runtime
Attempts:
2 left
๐Ÿ’ก Hint
Embedded structs get zero values too.
โ“ Predict Output
expert
2:00remaining
Zero value of interface and type assertion
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    var i interface{}
    fmt.Println(i == nil)
    v, ok := i.(int)
    fmt.Println(v, ok)
}
A
false
0 false
B
true
0 false
C
true
0 true
D
false
0 true
Attempts:
2 left
๐Ÿ’ก Hint
An uninitialized interface is nil; type assertion fails safely.

Practice

(1/5)
1. What is the zero value of an int variable in Go?
easy
A. "" (empty string)
B. nil
C. 0
D. false

Solution

  1. Step 1: Understand zero values for basic types

    In Go, each type has a default zero value when not initialized.
  2. Step 2: Identify zero value for int type

    The zero value for int is the number 0.
  3. Final Answer:

    0 -> Option C
  4. Quick Check:

    int zero value = 0 [OK]
Hint: Remember: int zero value is always 0 [OK]
Common Mistakes:
  • Confusing int zero value with nil
  • Thinking zero value is empty string
  • Mixing boolean zero value with int
2. Which of the following is the correct way to declare a variable with its zero value in Go?
easy
A. var x int = false
B. var x int = nil
C. var x int = ""
D. var x int

Solution

  1. Step 1: Recall Go variable declaration syntax

    Declaring a variable without initialization sets it to its zero value.
  2. Step 2: Check each option for correctness

    var x int declares x as int without initialization, so it gets zero value 0. Other options assign invalid values for int.
  3. Final Answer:

    var x int -> Option D
  4. Quick Check:

    Declare without init = zero value [OK]
Hint: Declare without assignment to get zero value [OK]
Common Mistakes:
  • Assigning nil to int
  • Assigning string to int
  • Assigning boolean to int
3. What will be the output of this Go program?
package main
import "fmt"
func main() {
    var b bool
    fmt.Println(b)
}
medium
A. false
B. Compilation error
C. nil
D. true

Solution

  1. Step 1: Identify the variable type and initialization

    Variable b is declared as bool without explicit initialization.
  2. Step 2: Recall zero value for bool type

    The zero value for bool is false, so b is false by default.
  3. Final Answer:

    false -> Option A
  4. Quick Check:

    bool zero value = false [OK]
Hint: Uninitialized bool defaults to false [OK]
Common Mistakes:
  • Expecting true as default
  • Thinking nil is valid bool
  • Assuming compilation error
4. Identify the error in this Go code snippet related to zero values:
package main
func main() {
    var s string = nil
}
medium
A. Assigning nil to string causes a compile error
B. String zero value should be 0
C. Variable s should be declared as int
D. No error, code runs fine

Solution

  1. Step 1: Understand zero value for string type

    Zero value for string is an empty string "", not nil.
  2. Step 2: Check assignment validity

    Assigning nil to a string variable causes a compile-time error because nil is not a valid string value.
  3. Final Answer:

    Assigning nil to string causes a compile error -> Option A
  4. Quick Check:

    string zero value = "" not nil [OK]
Hint: Strings cannot be nil, use empty string "" [OK]
Common Mistakes:
  • Using nil for string
  • Confusing zero value with nil
  • Ignoring compile errors
5. You want to create a map in Go that stores string keys and integer values. You declare it as:
var m map[string]int
What is the zero value of m and what must you do before adding elements?
hard
A. m is zero; no initialization needed, just assign values
B. m is nil; you must initialize it with make before adding elements
C. m is an empty map; you can add elements immediately
D. m is a map with default zero values for keys and values

Solution

  1. Step 1: Identify zero value of map type

    Maps declared without initialization have zero value nil.
  2. Step 2: Understand map usage requirements

    You cannot add elements to a nil map; you must initialize it using make(map[string]int) before adding keys and values.
  3. Final Answer:

    m is nil; you must initialize it with make before adding elements -> Option B
  4. Quick Check:

    Maps zero value = nil; initialize with make [OK]
Hint: Maps start nil; use make() before adding keys [OK]
Common Mistakes:
  • Trying to add to nil map
  • Assuming map is empty but usable
  • Confusing zero value with empty map
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard