Challenge - 5 Problems
Pointer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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) }
Attempts:
2 left
💡 Hint
Remember that modifying the value through a pointer changes the original variable.
✗ Incorrect
The pointer p points to x. Changing *p changes x's value to 20. So printing x outputs 20.
❓ Predict Output
intermediate2: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") } }
Attempts:
2 left
💡 Hint
Uninitialized pointers have a zero value of nil.
✗ Incorrect
The pointer p is declared but not assigned, so it is nil. The if condition is true, printing "Pointer is nil".
❓ Predict Output
advanced2: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) }
Attempts:
2 left
💡 Hint
Passing a pointer allows the function to modify the original variable.
✗ Incorrect
The function increment receives a pointer to x and increases the value it points to by 1. So x becomes 6.
❓ Predict Output
advanced2: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) }
Attempts:
2 left
💡 Hint
Pointers can point to struct fields and modify them directly.
✗ Incorrect
ptr points to p.age. Changing *ptr changes p.age to 40. So printing p.age outputs 40.
🧠 Conceptual
expert2:00remaining
Pointer zero value and memory safety
Which statement about pointers in Go is TRUE?
Attempts:
2 left
💡 Hint
Think about what happens if you try to use a pointer that has not been assigned.
✗ Incorrect
In Go, a pointer declared but not assigned is nil. Dereferencing nil causes a runtime panic. Go does not support pointer arithmetic and enforces package privacy.