0
0
Goprogramming~20 mins

Struct pointers in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Struct Pointer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of struct pointer field modification
What is the output of this Go program?
Go
package main
import "fmt"
type Person struct {
    Name string
    Age  int
}
func main() {
    p := Person{"Alice", 30}
    ptr := &p
    ptr.Age = 31
    fmt.Println(p.Age)
}
ACompilation error
B0
C30
D31
Attempts:
2 left
💡 Hint
Remember that modifying a struct through its pointer changes the original struct.
Predict Output
intermediate
2:00remaining
Output when assigning struct pointer to another variable
What will be printed by this Go program?
Go
package main
import "fmt"
type Point struct {
    X, Y int
}
func main() {
    p1 := &Point{X: 1, Y: 2}
    p2 := p1
    p2.X = 10
    fmt.Println(p1.X)
}
A0
B1
C10
DCompilation error
Attempts:
2 left
💡 Hint
Both p1 and p2 point to the same struct in memory.
🔧 Debug
advanced
2:00remaining
Identify the runtime error in struct pointer usage
What error does this Go program produce when run?
Go
package main
import "fmt"
type Data struct {
    Value int
}
func main() {
    var d *Data
    d.Value = 5
    fmt.Println(d.Value)
}
Apanic: runtime error: invalid memory address or nil pointer dereference
BOutput: 5
CCompilation error: cannot assign to d.Value
DNo output, program hangs
Attempts:
2 left
💡 Hint
Check if the pointer d is initialized before accessing its fields.
🧠 Conceptual
advanced
2:00remaining
Effect of passing struct pointer to a function
Consider this Go code snippet. What will be the output after calling updateAge(&p)?
Go
package main
import "fmt"
type Person struct {
    Age int
}
func updateAge(p *Person) {
    p.Age = 40
}
func main() {
    p := Person{Age: 25}
    updateAge(&p)
    fmt.Println(p.Age)
}
A25
B40
C0
DCompilation error
Attempts:
2 left
💡 Hint
Passing a pointer allows the function to modify the original struct.
🔧 Debug
expert
2:00remaining
Which option causes a runtime panic with struct pointers?
Given the struct type Node, which code snippet will cause a runtime panic?
Go
type Node struct {
    Value int
    Next  *Node
}
A
var n *Node
n.Value = 10
B
n := &Node{Value: 5}
n.Next = &Node{Value: 10}
C
var n Node
n.Value = 3
D
n := Node{Value: 1}
var p *Node = &n
p.Value = 2
Attempts:
2 left
💡 Hint
Check if the pointer variable is initialized before accessing fields.