Bird
Raised Fist0
Goprogramming~10 mins

Why pointers are needed in Go - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a pointer to an integer variable.

Go
var x int = 10
var p [1] = &x
Drag options to blanks, or click blank then click option'
A*int
Bint
C&int
Dpointer
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' instead of '*int' for pointer type.
Using '&int' which is invalid syntax.
2fill in blank
medium

Complete the code to change the value of x using its pointer p.

Go
var x int = 5
var p *int = &x
*p [1] 20
fmt.Println(x)
Drag options to blanks, or click blank then click option'
A==
B&
C:=
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which is a comparison, not assignment.
Using ':=' which declares a new variable.
3fill in blank
hard

Fix the error in the code to correctly swap two integers using pointers.

Go
func swap(a, b [1] int) {
    temp := *a
    *a = *b
    *b = temp
}

x, y := 1, 2
swap(&x, &y)
fmt.Println(x, y)
Drag options to blanks, or click blank then click option'
Apointer
B*
Cref
D&
Attempts:
3 left
💡 Hint
Common Mistakes
Using '&' which is the address operator, not a type.
Using 'ref' or 'pointer' which are not Go types.
4fill in blank
hard

Fill both blanks to correctly swap values of two integers using pointers.

Go
func swap(a, b [1] int) {
    temp := *a
    *a = *b
    *b = temp
}

x, y := 3, 4
swap([2]x, &y)
fmt.Println(x, y)
Drag options to blanks, or click blank then click option'
A*
B&
Cref
Dpointer
Attempts:
3 left
💡 Hint
Common Mistakes
Passing variables directly instead of their addresses.
Using 'ref' or 'pointer' which are not valid Go types.
5fill in blank
hard

Fill all three blanks to create a function that increments an integer value using a pointer.

Go
func increment([1] [2]int) {
    *[3] += 1
}

var num int = 7
increment(&num)
fmt.Println(num)
Drag options to blanks, or click blank then click option'
Ap
B*
C&
Dptr
Attempts:
3 left
💡 Hint
Common Mistakes
Using '&' as a type instead of '*'.
Using different names for parameter and dereference.

Practice

(1/5)
1. Why do we need pointers in Go programming?
easy
A. To make the program run slower
B. To allow functions to modify the original variable's value
C. To create new variables automatically
D. To avoid using variables altogether

Solution

  1. Step 1: Understand what pointers do

    Pointers hold the memory address of a variable, not a copy of its value.
  2. Step 2: Why modify original data matters

    Using pointers lets functions change the actual variable, not just a copy, which is efficient and necessary in many cases.
  3. Final Answer:

    To allow functions to modify the original variable's value -> Option B
  4. Quick Check:

    Pointers let functions change original data = C [OK]
Hint: Pointers let functions change original data, not copies [OK]
Common Mistakes:
  • Thinking pointers create new variables
  • Believing pointers slow down programs
  • Assuming pointers avoid variables
2. Which of the following is the correct way to declare a pointer to an int in Go?
easy
A. var p *int
B. var p int*
C. var *p int
D. var p &int

Solution

  1. Step 1: Recall Go pointer syntax

    In Go, a pointer type is written as * followed by the type, e.g., *int for pointer to int.
  2. Step 2: Match syntax to options

    var p *int uses 'var p *int', which is the correct syntax for declaring a pointer to int.
  3. Final Answer:

    var p *int -> Option A
  4. Quick Check:

    Pointer declaration = var p *int [OK]
Hint: Pointer type uses * before type, like *int [OK]
Common Mistakes:
  • Placing * after variable name
  • Using & instead of * for pointer type
  • Mixing pointer syntax with other languages
3. What will be the output of this Go code?
package main
import "fmt"
func main() {
    x := 10
    changeValue(&x)
    fmt.Println(x)
}

func changeValue(p *int) {
    *p = 20
}
medium
A. 20
B. Compilation error
C. 0
D. 10

Solution

  1. Step 1: Understand pointer usage in changeValue

    The function receives a pointer to x, so *p = 20 changes the original x value to 20.
  2. Step 2: Print the updated value of x

    After calling changeValue, x is updated to 20, so fmt.Println(x) prints 20.
  3. Final Answer:

    20 -> Option A
  4. Quick Check:

    Pointer changes original variable = 20 [OK]
Hint: Pointer lets function change original variable value [OK]
Common Mistakes:
  • Thinking x remains 10 because of pass by value
  • Confusing pointer dereference syntax
  • Expecting compilation error due to pointer
4. Identify the error in this Go code that tries to update a variable using a pointer:
package main
import "fmt"
func main() {
    x := 5
    changeValue(x)
    fmt.Println(x)
}

func changeValue(p *int) {
    *p = 10
}
medium
A. No error, code works fine
B. changeValue should not use pointers
C. x should be a pointer variable
D. Missing & when calling changeValue

Solution

  1. Step 1: Check how changeValue is called

    changeValue expects a pointer (*int), but main passes x (int) without & operator.
  2. Step 2: Correct call with pointer

    To fix, call changeValue(&x) to pass the address of x.
  3. Final Answer:

    Missing & when calling changeValue -> Option D
  4. Quick Check:

    Pass pointer with & operator = A [OK]
Hint: Pass variable address with & when function expects pointer [OK]
Common Mistakes:
  • Passing variable instead of its address
  • Changing function signature unnecessarily
  • Assuming no error occurs
5. You want to write a function in Go that swaps the values of two integers. Why must you use pointers for the parameters?
hard
A. Because Go does not allow returning multiple values
B. Because pointers make the function run slower
C. Because passing by value copies variables, so original values won't change
D. Because pointers automatically create new variables

Solution

  1. Step 1: Understand parameter passing in Go

    Go passes arguments by value, so without pointers, the function works on copies.
  2. Step 2: Why pointers are needed for swapping

    Using pointers lets the function access and modify the original variables, enabling the swap.
  3. Final Answer:

    Because passing by value copies variables, so original values won't change -> Option C
  4. Quick Check:

    Pointers needed to modify originals, not copies = B [OK]
Hint: Swap needs pointers to change original variables [OK]
Common Mistakes:
  • Thinking Go can't return multiple values
  • Believing pointers slow down swap
  • Assuming pointers create new variables
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard