0
0
Goprogramming~20 mins

Common pointer use cases in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pointer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pointer dereference and modification
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    x := 10
    p := &x
    *p = 20
    fmt.Println(x)
}
A20
B10
CCompilation error
DRuntime panic
Attempts:
2 left
💡 Hint
Remember that modifying the value through a pointer changes the original variable.
Predict Output
intermediate
2:00remaining
Pointer nil check and output
What will this Go program print?
Go
package main
import "fmt"
func main() {
    var p *int
    if p == nil {
        fmt.Println("Pointer is nil")
    } else {
        fmt.Println("Pointer is not nil")
    }
}
ARuntime panic
BPointer is nil
CCompilation error
DPointer is not nil
Attempts:
2 left
💡 Hint
Uninitialized pointers have a zero value of nil.
Predict Output
advanced
2:00remaining
Effect of pointer on function argument
What is the output of this Go program?
Go
package main
import "fmt"
func increment(n *int) {
    *n = *n + 1
}
func main() {
    x := 5
    increment(&x)
    fmt.Println(x)
}
A5
BCompilation error
C6
DRuntime panic
Attempts:
2 left
💡 Hint
Passing a pointer allows the function to modify the original variable.
Predict Output
advanced
2:00remaining
Pointer to struct field modification
What will this Go program print?
Go
package main
import "fmt"
type Person struct {
    age int
}
func main() {
    p := Person{age: 30}
    ptr := &p.age
    *ptr = 40
    fmt.Println(p.age)
}
A40
BRuntime panic
CCompilation error
D30
Attempts:
2 left
💡 Hint
Pointers can point to struct fields and modify them directly.
🧠 Conceptual
expert
2:00remaining
Pointer zero value and memory safety
Which statement about pointers in Go is TRUE?
APointers in Go can be used to access private fields of other packages.
BA pointer variable always points to a valid memory address after declaration.
CGo allows pointer arithmetic similar to C.
DDereferencing a nil pointer causes a runtime panic.
Attempts:
2 left
💡 Hint
Think about what happens if you try to use a pointer that has not been assigned.