Bird
Raised Fist0
Goprogramming~20 mins

Address and dereference operators in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pointer Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of pointer dereference after assignment
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    x := 10
    p := &x
    *p = 20
    fmt.Println(x)
}
ACompilation error
B10
C0
D20
Attempts:
2 left
💡 Hint
Remember that *p changes the value at the address p points to.
❓ Predict Output
intermediate
2:00remaining
Value of variable after pointer reassignment
What will be printed by this Go code?
Go
package main
import "fmt"
func main() {
    a := 5
    b := 10
    p := &a
    p = &b
    *p = 15
    fmt.Println(a, b)
}
A5 15
B5 10
C15 10
DCompilation error
Attempts:
2 left
💡 Hint
Pointer p is first set to a, then to b. Changing *p affects which variable?
🔧 Debug
advanced
2:00remaining
Identify the error in pointer usage
What error does this Go code produce?
Go
package main
func main() {
    var p *int
    *p = 10
}
ANo error, program prints 10
BCompilation error: cannot assign to *p
Cruntime error: invalid memory address or nil pointer dereference
DCompilation error: missing variable declaration
Attempts:
2 left
💡 Hint
p is declared but not initialized to point to valid memory.
🧠 Conceptual
advanced
2:00remaining
Effect of pointer on function argument
What will be the output of this Go program?
Go
package main
import "fmt"
func increment(n *int) {
    *n = *n + 1
}
func main() {
    x := 7
    increment(&x)
    fmt.Println(x)
}
A7
B8
C0
DCompilation error: cannot use &x
Attempts:
2 left
💡 Hint
The function changes the value at the address passed.
❓ Predict Output
expert
2:00remaining
Pointer arithmetic and dereference behavior
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    arr := [3]int{1, 2, 3}
    p := &arr[0]
    fmt.Println(*p)
    p = &arr[1]
    fmt.Println(*p)
    p++
    fmt.Println(*p)
}
ACompilation error: invalid operation p++
B
1
2
Compilation error
C
1
2
0
D
1
2
3
Attempts:
2 left
💡 Hint
Go does not support pointer arithmetic like p++.

Practice

(1/5)
1. What does the & operator do in Go?
easy
A. It declares a new variable.
B. It returns the memory address of a variable.
C. It multiplies two numbers.
D. It dereferences a pointer to get the value.

Solution

  1. Step 1: Understand the & operator

    The & operator is used to get the address of a variable in memory.
  2. Step 2: Differentiate from * operator

    The * operator is used to access the value at an address, not to get the address itself.
  3. Final Answer:

    It returns the memory address of a variable. -> Option B
  4. Quick Check:

    & operator = address retrieval [OK]
Hint: Remember & gets address, * gets value at address [OK]
Common Mistakes:
  • Confusing & with * operator
  • Thinking & multiplies values
  • Assuming & declares variables
2. Which of the following is the correct way to declare a pointer to an int variable num in Go?
easy
A. var p &int = num
B. var p int = *num
C. var p *int = &num
D. var p *num = &int

Solution

  1. Step 1: Understand pointer declaration syntax

    In Go, a pointer to an int is declared as *int. To assign it, use the address operator & on the variable.
  2. Step 2: Check each option

    var p *int = &num correctly declares p as a pointer to int and assigns the address of num. Others misuse types or operators.
  3. Final Answer:

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

    Pointer declaration = var p *int = &num [OK]
Hint: Pointer syntax: var p *Type = &variable [OK]
Common Mistakes:
  • Using * on variable instead of type
  • Using & with type instead of variable
  • Mixing variable and type names
3. What will be the output of this Go code?
package main
import "fmt"
func main() {
  x := 10
  p := &x
  *p = 20
  fmt.Println(x)
}
medium
A. 10
B. Compilation error
C. Memory address of x
D. 20

Solution

  1. Step 1: Understand pointer assignment

    Variable p stores the address of x. Using *p = 20 changes the value at that address.
  2. Step 2: Check final value of x

    Since *p points to x, changing *p to 20 updates x to 20.
  3. Final Answer:

    20 -> Option D
  4. Quick Check:

    Dereferencing pointer changes original variable [OK]
Hint: Changing *p changes x because p points to x [OK]
Common Mistakes:
  • Thinking *p = 20 creates new variable
  • Expecting print to show address
  • Confusing & and * operators
4. Identify the error in this Go code snippet:
func main() {
  var a int = 5
  var p *int
  *p = a
  fmt.Println(*p)
}
medium
A. Pointer p is not initialized before dereferencing.
B. Variable a should be a pointer.
C. Cannot assign int to *int directly.
D. fmt.Println cannot print pointer values.

Solution

  1. Step 1: Check pointer initialization

    Pointer p is declared but not assigned an address before dereferencing with *p = a.
  2. Step 2: Understand consequences

    Dereferencing an uninitialized pointer causes a runtime panic (nil pointer dereference).
  3. Final Answer:

    Pointer p is not initialized before dereferencing. -> Option A
  4. Quick Check:

    Dereference only initialized pointers [OK]
Hint: Always assign pointer before dereferencing it [OK]
Common Mistakes:
  • Dereferencing nil pointer
  • Confusing pointer and value assignment
  • Assuming fmt.Println can't print pointers
5. Given the code below, what will be the output?
package main
import "fmt"
func updateValue(p *int) {
  *p = *p + 5
}
func main() {
  val := 10
  updateValue(&val)
  fmt.Println(val)
}
hard
A. 15
B. 10
C. Memory address of val
D. Compilation error

Solution

  1. Step 1: Understand function parameter

    The function updateValue takes a pointer to an int. Passing &val sends the address of val.
  2. Step 2: Analyze pointer dereference and update

    Inside the function, *p = *p + 5 adds 5 to the value at the address, updating val to 15.
  3. Final Answer:

    15 -> Option A
  4. Quick Check:

    Pointer parameter updates original variable [OK]
Hint: Passing &var lets function modify original value [OK]
Common Mistakes:
  • Expecting original val unchanged
  • Confusing pointer with value copy
  • Thinking function returns new value
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