Golang Pointers Quiz
50 comprehensive questions on Golang pointers, covering pointer basics, types, nil pointers, receiver semantics, and common pitfalls — with 15 code examples demonstrating pointer operations and memory management.
Question 1
What is a pointer in Golang?
Question 2
What does the & operator do in Golang?
Question 3
What does the * operator do when used with pointers?
Question 4
What will this basic pointer example print?
package main
import "fmt"
func main() {
x := 42
p := &x
fmt.Println(*p)
}Question 5
How do you declare a pointer variable in Golang?
Question 6
What is the zero value of a pointer in Golang?
Question 7
What happens if you dereference a nil pointer?
Question 8
What will this nil pointer dereference do?
package main
func main() {
var p *int
*p = 42 // This will panic
}Question 9
How can you safely check if a pointer is nil before dereferencing?
Question 10
What is a pointer to a pointer in Golang?
Question 11
What will this pointer to pointer example print?
package main
import "fmt"
func main() {
x := 42
p := &x
pp := &p
fmt.Println(**pp)
}Question 12
Can you perform pointer arithmetic in Golang like in C?
Question 13
How do you compare two pointers in Golang?
Question 14
What will this pointer comparison example print?
package main
import "fmt"
func main() {
x := 42
y := 42
p1 := &x
p2 := &x
p3 := &y
fmt.Println(p1 == p2, p1 == p3)
}Question 15
What is the difference between passing a value and passing a pointer to a function?
Question 16
What will this pass by pointer example do?
package main
import "fmt"
func modify(p *int) {
*p = 100
}
func main() {
x := 50
modify(&x)
fmt.Println(x)
}Question 17
When should you use pointers as function parameters?
Question 18
What is a method receiver in Golang?
Question 19
What is the difference between value receivers and pointer receivers?
Question 20
What will this value receiver method do?
package main
import "fmt"
type Counter struct {
value int
}
func (c Counter) increment() {
c.value++
}
func main() {
counter := Counter{0}
counter.increment()
fmt.Println(counter.value)
}Question 21
What will this pointer receiver method do?
package main
import "fmt"
type Counter struct {
value int
}
func (c *Counter) increment() {
c.value++
}
func main() {
counter := Counter{0}
counter.increment()
fmt.Println(counter.value)
}Question 22
When should you use pointer receivers for methods?
Question 23
Can you mix value and pointer receivers for the same type?
Question 24
What is a common pitfall with pointer receivers?
Question 25
What will this nil receiver method call do?
package main
import "fmt"
type Person struct {
name string
}
func (p *Person) getName() string {
return p.name
}
func main() {
var p *Person
fmt.Println(p.getName()) // This will panic
}Question 26
How can you safely handle nil pointer receivers?
Question 27
What is pointer aliasing?
Question 28
What is a common pitfall with pointer aliasing?
Question 29
What will this pointer aliasing example print?
package main
import "fmt"
func main() {
x := 42
p1 := &x
p2 := p1 // p2 aliases p1
*p2 = 100
fmt.Println(x, *p1, *p2)
}Question 30
What is the new() function used for in Golang?
Question 31
What will this new() example print?
package main
import "fmt"
func main() {
p := new(int)
fmt.Println(*p)
*p = 42
fmt.Println(*p)
}Question 32
What is the difference between &T{} and new(T)?
Question 33
What is a dangling pointer?
Question 34
Does Golang have dangling pointers like C/C++?
Question 35
What is pointer escaping in Golang?
Question 36
What will this escaping pointer example do?
package main
import "fmt"
func createPointer() *int {
x := 42 // x would normally be stack-allocated
return &x // but &x escapes, so x is heap-allocated
}
func main() {
p := createPointer()
fmt.Println(*p)
}Question 37
What is a common pitfall when returning pointers from functions?
Question 38
What is the unsafe.Pointer type in Golang?
Question 39
When should you use the unsafe package?
Question 40
What is a common mistake when working with pointers to slices?
Question 41
What will this slice pointer example do?
package main
import "fmt"
func modifySlice(s *[]int) {
*s = append(*s, 42)
}
func main() {
slice := []int{1, 2, 3}
modifySlice(&slice)
fmt.Println(slice)
}Question 42
What is the difference between *[]int and []*int?
Question 43
What is a common pitfall with maps and pointers?
Question 44
What will this map with pointers example do?
package main
import "fmt"
func main() {
m := make(map[string]*int)
x := 42
m["key"] = &x
*m["key"] = 100
fmt.Println(x)
}Question 45
What is the main reason Golang uses pointers?
Question 46
What is the most important rule for safe pointer usage in Golang?
Question 47
What is a pointer receiver method set?
Question 48
Can a type T implement an interface if it only has value receiver methods?
Question 49
What is the most common pointer-related performance issue in Golang?
Question 50
What is the key takeaway about pointers in Golang?
