0
0
Goprogramming~20 mins

Address and dereference operators in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pointer Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pointer dereference after assignment
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    x := 10
    p := &x
    *p = 20
    fmt.Println(x)
}
ACompilation error
B10
C0
D20
Attempts:
2 left
💡 Hint
Remember that *p changes the value at the address p points to.
Predict Output
intermediate
2:00remaining
Value of variable after pointer reassignment
What will be printed by this Go code?
Go
package main
import "fmt"
func main() {
    a := 5
    b := 10
    p := &a
    p = &b
    *p = 15
    fmt.Println(a, b)
}
A5 15
B5 10
C15 10
DCompilation error
Attempts:
2 left
💡 Hint
Pointer p is first set to a, then to b. Changing *p affects which variable?
🔧 Debug
advanced
2:00remaining
Identify the error in pointer usage
What error does this Go code produce?
Go
package main
func main() {
    var p *int
    *p = 10
}
ANo error, program prints 10
BCompilation error: cannot assign to *p
Cruntime error: invalid memory address or nil pointer dereference
DCompilation error: missing variable declaration
Attempts:
2 left
💡 Hint
p is declared but not initialized to point to valid memory.
🧠 Conceptual
advanced
2:00remaining
Effect of pointer on function argument
What will be the output of this Go program?
Go
package main
import "fmt"
func increment(n *int) {
    *n = *n + 1
}
func main() {
    x := 7
    increment(&x)
    fmt.Println(x)
}
A7
B8
C0
DCompilation error: cannot use &x
Attempts:
2 left
💡 Hint
The function changes the value at the address passed.
Predict Output
expert
2:00remaining
Pointer arithmetic and dereference behavior
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    arr := [3]int{1, 2, 3}
    p := &arr[0]
    fmt.Println(*p)
    p = &arr[1]
    fmt.Println(*p)
    p++
    fmt.Println(*p)
}
ACompilation error: invalid operation p++
B
1
2
Compilation error
C
1
2
0
D
1
2
3
Attempts:
2 left
💡 Hint
Go does not support pointer arithmetic like p++.