Bird
Raised Fist0
Goprogramming~10 mins

Common pointer use cases in Go - Interactive Code Practice

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
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' instead of '*int' for pointer type.
Using '&int' which is invalid syntax.
Using 'int*' which is not Go syntax.
2fill in blank
medium

Complete the code to change the value of an integer through its pointer.

Go
var x int = 5
var p *int = &x
*p [1] 20
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, not assignment.
Using '!=' which is a comparison operator.
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
}
Drag options to blanks, or click blank then click option'
A**
B&&
C&
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '&int' which is invalid syntax.
Using '**int' which is a pointer to a pointer, not needed here.
Using '&&int' which is invalid.
4fill in blank
hard

Fill both blanks to create a map where keys are strings and values are pointers to integers.

Go
m := map[string][1]{}
m["score"] = [2]
Drag options to blanks, or click blank then click option'
A*int
B&x
Cint
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' as map value type instead of '*int'.
Assigning 'x' instead of '&x' to the map value.
Using '&int' which is invalid.
5fill in blank
hard

Fill all three blanks to define a struct with a pointer field, create an instance, and assign a value through the pointer.

Go
type Person struct {
    age [1]
}
p := Person{age: nil}
var a int = 30
p.age = [2]
*[3] = 35
Drag options to blanks, or click blank then click option'
A*int
B&a
Cp.age
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Declaring age as int instead of *int.
Assigning 'a' instead of '&a' to p.age.
Dereferencing wrong variable instead of p.age.

Practice

(1/5)
1. What does a pointer in Go store?
easy
A. The memory address of a variable
B. The value of a variable
C. The type of a variable
D. The size of a variable

Solution

  1. Step 1: Understand what a pointer represents

    A pointer holds the location in memory where a variable is stored, not the value itself.
  2. Step 2: Differentiate pointer from value

    The value is stored at the address the pointer points to, not inside the pointer variable.
  3. Final Answer:

    The memory address of a variable -> Option A
  4. Quick Check:

    Pointers store addresses = B [OK]
Hint: Pointers hold addresses, not values directly [OK]
Common Mistakes:
  • Confusing pointer with the actual value
  • Thinking pointer stores variable type
  • Assuming pointer stores variable size
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 *Type, so a pointer to int is *int.
  2. Step 2: Check variable declaration format

    The variable name comes after var, then the type. So correct is var p *int.
  3. Final Answer:

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

    Pointer syntax is var name *Type = D [OK]
Hint: Pointer type uses * before type, not after variable [OK]
Common Mistakes:
  • Placing * after variable name
  • Using & instead of * for pointer type
  • Incorrect order of * and variable name
3. What is the output of this Go code?
func main() {
    x := 10
    p := &x
    *p = 20
    fmt.Println(x)
}
medium
A. 10
B. Memory address of x
C. Compilation error
D. 20

Solution

  1. Step 1: Understand pointer assignment

    p points to x's address. Changing *p changes the value at x's address.
  2. Step 2: Trace value change

    *p = 20 sets x to 20. So printing x outputs 20.
  3. Final Answer:

    20 -> Option D
  4. Quick Check:

    Pointer changes original value = 20 [OK]
Hint: Changing *p changes original variable value [OK]
Common Mistakes:
  • Thinking x remains 10 after *p = 20
  • Printing pointer address instead of value
  • Assuming compilation error due to pointer
4. Identify the error in this Go code snippet:
func updateValue(p *int) {
    *p = 100
}

func main() {
    var p *int
    updateValue(p)
}
medium
A. Syntax error in function declaration
B. p is nil and dereferenced causing runtime panic
C. updateValue should return an int
D. No error, code runs fine

Solution

  1. Step 1: Check pointer initialization

    p is declared but not assigned an address, so it is nil.
  2. Step 2: Analyze dereferencing nil pointer

    updateValue dereferences *p, causing a runtime panic because p is nil.
  3. Final Answer:

    p is nil and dereferenced causing runtime panic -> Option B
  4. Quick Check:

    Dereferencing nil pointer causes panic = A [OK]
Hint: Never dereference nil pointers without initialization [OK]
Common Mistakes:
  • Assuming nil pointer is safe to dereference
  • Thinking function must return a value
  • Ignoring runtime panic possibility
5. Given this Go code, what will be the output?
func swap(a, b *int) {
    *a, *b = *b, *a
}

func main() {
    x, y := 5, 10
    swap(&x, &y)
    fmt.Println(x, y)
}
hard
A. 10 5
B. 5 10
C. Memory addresses of x and y
D. Compilation error due to swap syntax

Solution

  1. Step 1: Understand swap function with pointers

    swap receives pointers to x and y, then swaps the values they point to.
  2. Step 2: Trace swap effect on variables

    After swap, x becomes 10 and y becomes 5.
  3. Final Answer:

    10 5 -> Option A
  4. Quick Check:

    Pointer swap changes original values = 10 5 [OK]
Hint: Swap values by swapping *a and *b via pointers [OK]
Common Mistakes:
  • Expecting original values unchanged
  • Printing pointer addresses instead of values
  • Thinking swap function syntax is invalid
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