Golang Pointers Quiz

Golang
0 Passed
0% acceptance

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.

50 Questions
~100 minutes
1

Question 1

What is a pointer in Golang?

A
A variable that stores the memory address of another variable
B
A variable that stores a value directly
C
A special kind of integer
D
A reference to a function
2

Question 2

What does the & operator do in Golang?

A
Takes the address of a variable (address-of operator)
B
Dereferences a pointer
C
Creates a new variable
D
Performs bitwise AND
3

Question 3

What does the * operator do when used with pointers?

A
Dereferences a pointer to access the value it points to
B
Creates a pointer
C
Multiplies values
D
Declares a pointer type
4

Question 4

What will this basic pointer example print?

go
package main

import "fmt"

func main() {
    x := 42
    p := &x
    fmt.Println(*p)
}
A
42
B
Address of x
C
Compilation error
D
0
5

Question 5

How do you declare a pointer variable in Golang?

A
var p *int - using * before the type
B
var p int* - using * after the type
C
var p &int - using & before the type
D
var p int - no special syntax
6

Question 6

What is the zero value of a pointer in Golang?

A
nil
B
0
C
null
D
empty address
7

Question 7

What happens if you dereference a nil pointer?

A
Runtime panic occurs
B
Returns zero value
C
Ignores the operation
D
Compilation error
8

Question 8

What will this nil pointer dereference do?

go
package main

func main() {
    var p *int
    *p = 42  // This will panic
}
A
Runtime panic
B
Sets value to 42
C
Compilation error
D
Does nothing
9

Question 9

How can you safely check if a pointer is nil before dereferencing?

A
if p != nil { *p = value }
B
if p == 0 { *p = value }
C
if p { *p = value }
D
No need to check, Golang handles it
10

Question 10

What is a pointer to a pointer in Golang?

A
A pointer that points to another pointer: **Type
B
A pointer to a function
C
A special pointer type
D
Not allowed in Golang
11

Question 11

What will this pointer to pointer example print?

go
package main

import "fmt"

func main() {
    x := 42
    p := &x
    pp := &p
    fmt.Println(**pp)
}
A
42
B
Address of p
C
Compilation error
D
nil
12

Question 12

Can you perform pointer arithmetic in Golang like in C?

A
No, Golang does not support pointer arithmetic
B
Yes, with + and - operators
C
Only for arrays
D
Only for slices
13

Question 13

How do you compare two pointers in Golang?

A
Using == and != operators to compare memory addresses
B
Using < and > operators
C
Cannot compare pointers
D
Only with special functions
14

Question 14

What will this pointer comparison example print?

go
package main

import "fmt"

func main() {
    x := 42
    y := 42
    p1 := &x
    p2 := &x
    p3 := &y
    fmt.Println(p1 == p2, p1 == p3)
}
A
true false
B
false true
C
true true
D
Compilation error
15

Question 15

What is the difference between passing a value and passing a pointer to a function?

A
Value copies the data, pointer copies only the address (more efficient for large types)
B
They are the same
C
Pointers are always slower
D
Values cannot be modified, pointers can
16

Question 16

What will this pass by pointer example do?

go
package main

import "fmt"

func modify(p *int) {
    *p = 100
}

func main() {
    x := 50
    modify(&x)
    fmt.Println(x)
}
A
Prints 100
B
Prints 50
C
Compilation error
D
Runtime panic
17

Question 17

When should you use pointers as function parameters?

A
When you need to modify the original value or avoid copying large structs
B
Always, for better performance
C
Never, pass by value is better
D
Only for primitive types
18

Question 18

What is a method receiver in Golang?

A
The type that a method is defined on, appearing before the method name
B
A special parameter
C
A return value
D
A function parameter
19

Question 19

What is the difference between value receivers and pointer receivers?

A
Value receivers work on copies, pointer receivers work on the original instance
B
They are identical
C
Value receivers are for structs, pointer for primitives
D
Pointer receivers don't exist
20

Question 20

What will this value receiver method do?

go
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)
}
A
Prints 0
B
Prints 1
C
Compilation error
D
Runtime panic
21

Question 21

What will this pointer receiver method do?

go
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)
}
A
Prints 1
B
Prints 0
C
Compilation error
D
Runtime panic
22

Question 22

When should you use pointer receivers for methods?

A
When the method needs to modify the receiver or the receiver is a large struct
B
Always, for consistency
C
Never, value receivers are better
D
Only for interface implementation
23

Question 23

Can you mix value and pointer receivers for the same type?

A
Yes, but they define different method sets
B
No, all methods must use the same receiver type
C
Only for built-in types
D
Compilation error
24

Question 24

What is a common pitfall with pointer receivers?

A
Calling methods on nil pointer receivers can cause panics if not handled
B
They are slower than value receivers
C
They cannot be used with interfaces
D
They always copy the receiver
25

Question 25

What will this nil receiver method call do?

go
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
}
A
Runtime panic
B
Prints empty string
C
Compilation error
D
Prints nil
26

Question 26

How can you safely handle nil pointer receivers?

A
Check if receiver is nil at the beginning of the method
B
Use value receivers instead
C
Golang handles it automatically
D
Return default values
27

Question 27

What is pointer aliasing?

A
When multiple pointers refer to the same memory location
B
When pointers have different types
C
When pointers are nil
D
A type of pointer arithmetic
28

Question 28

What is a common pitfall with pointer aliasing?

A
Modifying a value through one pointer affects all aliases, causing unexpected behavior
B
Pointers become invalid
C
Memory leaks occur
D
Compilation errors
29

Question 29

What will this pointer aliasing example print?

go
package main

import "fmt"

func main() {
    x := 42
    p1 := &x
    p2 := p1  // p2 aliases p1
    *p2 = 100
    fmt.Println(x, *p1, *p2)
}
A
100 100 100
B
42 42 100
C
42 100 100
D
Compilation error
30

Question 30

What is the new() function used for in Golang?

A
Allocates memory for a type and returns a pointer to the zero value
B
Creates new variables
C
Initializes pointers
D
Frees memory
31

Question 31

What will this new() example print?

go
package main

import "fmt"

func main() {
    p := new(int)
    fmt.Println(*p)
    *p = 42
    fmt.Println(*p)
}
A
0\n42
B
42\n42
C
Compilation error
D
nil\n42
32

Question 32

What is the difference between &T{} and new(T)?

A
They are equivalent - both return *T pointing to zero value
B
&T{} creates a value, new(T) creates a pointer
C
new(T) is more efficient
D
They are completely different
33

Question 33

What is a dangling pointer?

A
A pointer that points to memory that has been freed or gone out of scope
B
A nil pointer
C
A pointer to a local variable
D
A pointer that has been reassigned
34

Question 34

Does Golang have dangling pointers like C/C++?

A
No, garbage collection prevents accessing freed memory
B
Yes, same as C/C++
C
Only for manual memory management
D
Only in unsafe code
35

Question 35

What is pointer escaping in Golang?

A
When a pointer to a local variable is returned or stored globally, forcing heap allocation
B
When pointers become invalid
C
When pointers are compared
D
A type of pointer error
36

Question 36

What will this escaping pointer example do?

go
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)
}
A
Prints 42 safely
B
Runtime panic
C
Compilation error
D
Undefined behavior
37

Question 37

What is a common pitfall when returning pointers from functions?

A
Returning pointers to local variables that go out of scope
B
Forgetting to dereference
C
Using wrong pointer types
D
Nil pointer returns
38

Question 38

What is the unsafe.Pointer type in Golang?

A
A special type for low-level memory operations that bypass type safety
B
A pointer that cannot be nil
C
A safe pointer type
D
Not available in Golang
39

Question 39

When should you use the unsafe package?

A
Rarely, only for interfacing with C code or low-level system operations
B
For all pointer operations
C
To improve performance
D
Instead of regular pointers
40

Question 40

What is a common mistake when working with pointers to slices?

A
Thinking that modifying the slice through a pointer affects the original slice header
B
Forgetting to initialize slices
C
Using wrong slice indices
D
Slice capacity issues
41

Question 41

What will this slice pointer example do?

go
package main

import "fmt"

func modifySlice(s *[]int) {
    *s = append(*s, 42)
}

func main() {
    slice := []int{1, 2, 3}
    modifySlice(&slice)
    fmt.Println(slice)
}
A
[1 2 3 42]
B
[1 2 3]
C
Compilation error
D
Runtime panic
42

Question 42

What is the difference between *[]int and []*int?

A
*[]int is a pointer to a slice, []*int is a slice of pointers to int
B
They are the same
C
*[]int is invalid syntax
D
[]*int is a pointer to a slice of ints
43

Question 43

What is a common pitfall with maps and pointers?

A
Maps cannot store pointers directly, but pointer values can be map values
B
Pointers in maps become nil
C
Map operations on pointers are slow
D
Pointers cannot be map keys
44

Question 44

What will this map with pointers example do?

go
package main

import "fmt"

func main() {
    m := make(map[string]*int)
    x := 42
    m["key"] = &x
    *m["key"] = 100
    fmt.Println(x)
}
A
100
B
42
C
Compilation error
D
nil
45

Question 45

What is the main reason Golang uses pointers?

A
To enable efficient pass-by-reference and avoid copying large data structures
B
To make code more complex
C
To replace all value types
D
For pointer arithmetic like C
46

Question 46

What is the most important rule for safe pointer usage in Golang?

A
Never dereference a nil pointer
B
Always use pointers
C
Avoid pointers for small types
D
Use unsafe.Pointer everywhere
47

Question 47

What is a pointer receiver method set?

A
The set of methods that can be called on a pointer to the type
B
Methods that return pointers
C
Methods that take pointer parameters
D
All methods of a type
48

Question 48

Can a type T implement an interface if it only has value receiver methods?

A
Yes, pointers to T can still call value receiver methods
B
No, interfaces require pointer receivers
C
Only if T is a pointer type
D
Compilation error
49

Question 49

What is the most common pointer-related performance issue in Golang?

A
Unnecessary heap allocation due to escaping pointers
B
Pointer dereference overhead
C
Pointer comparison slowness
D
Memory leaks
50

Question 50

What is the key takeaway about pointers in Golang?

A
Pointers enable efficiency and mutation but require careful nil handling
B
Pointers are dangerous and should be avoided
C
Golang has no real pointers
D
Pointers work exactly like in C

QUIZZES IN Golang