Bird
Raised Fist0
Goprogramming~20 mins

Passing values vs pointers in Go - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Passing Values and Pointers in Go
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of modifying a struct passed by value
What is the output of this Go program?
Go
package main
import "fmt"
type Point struct {
  X, Y int
}
func move(p Point) {
  p.X = 10
  p.Y = 20
}
func main() {
  pt := Point{1, 2}
  move(pt)
  fmt.Println(pt)
}
A{1 2}
B{10 20}
CCompilation error
D{0 0}
Attempts:
2 left
💡 Hint
Think about whether the function changes the original struct or a copy.
❓ Predict Output
intermediate
2:00remaining
Output of modifying a struct passed by pointer
What is the output of this Go program?
Go
package main
import "fmt"
type Point struct {
  X, Y int
}
func move(p *Point) {
  p.X = 10
  p.Y = 20
}
func main() {
  pt := Point{1, 2}
  move(&pt)
  fmt.Println(pt)
}
A{10 20}
B{0 0}
CCompilation error
D{1 2}
Attempts:
2 left
💡 Hint
Check if the function receives a pointer and modifies the original struct.
❓ Predict Output
advanced
2:00remaining
Effect of pointer vs value on slice modification
What is the output of this Go program?
Go
package main
import "fmt"
func modifySlice(s []int) {
  s[0] = 100
  s = append(s, 200)
}
func main() {
  slice := []int{1, 2, 3}
  modifySlice(slice)
  fmt.Println(slice)
}
A[100 2 3 200]
B[100 2 3]
C[1 2 3]
DRuntime panic
Attempts:
2 left
💡 Hint
Remember slices are references but append may create a new underlying array.
❓ Predict Output
advanced
2:00remaining
Output when passing map by value and modifying it
What is the output of this Go program?
Go
package main
import "fmt"
func modifyMap(m map[string]int) {
  m["a"] = 100
  m = make(map[string]int)
  m["b"] = 200
}
func main() {
  myMap := map[string]int{"a": 1}
  modifyMap(myMap)
  fmt.Println(myMap)
}
Amap[a:1]
Bmap[b:200]
Cmap[a:100]
DCompilation error
Attempts:
2 left
💡 Hint
Maps are reference types in Go. Changes to the map inside the function affect the original map.
🧠 Conceptual
expert
3:00remaining
Understanding pointer semantics with interface values
Consider this Go code snippet. What will be printed? package main import "fmt" type Data struct { Value int } func modify(d interface{}) { switch v := d.(type) { case *Data: v.Value = 42 case Data: v.Value = 42 } } func main() { d1 := Data{1} d2 := &Data{1} modify(d1) modify(d2) fmt.Println(d1.Value, d2.Value) }
A42 42
BCompilation error
C42 1
D1 42
Attempts:
2 left
💡 Hint
Think about whether the modify function changes the original data or a copy depending on the type assertion.

Practice

(1/5)
1. What happens when you pass a variable to a function by value in Go?
easy
A. The function receives a copy; changes inside do not affect the original variable.
B. The function receives the original variable; changes inside affect the original.
C. The function cannot modify the variable at all.
D. The function receives a reference but cannot change the original variable.

Solution

  1. Step 1: Understand passing by value

    Passing by value means the function gets a copy of the variable, not the original.
  2. Step 2: Effect of changes inside the function

    Changes to the copy do not affect the original variable outside the function.
  3. Final Answer:

    The function receives a copy; changes inside do not affect the original variable. -> Option A
  4. Quick Check:

    Passing by value = copy, no original change [OK]
Hint: Passing by value means function works on a copy [OK]
Common Mistakes:
  • Thinking changes inside function affect original variable
  • Confusing pointers with values
  • Assuming function cannot modify any data
2. Which of the following is the correct way to declare a function parameter as a pointer in Go?
easy
A. func updateValue(val *int) {}
B. func updateValue(&val int) {}
C. func updateValue(val int) {}
D. func updateValue(*val int) {}

Solution

  1. Step 1: Recall pointer syntax in Go function parameters

    In Go, a pointer parameter is declared by placing * before the type, e.g., *int.
  2. Step 2: Identify correct syntax

    func updateValue(val *int) {} is correct syntax, with * before the type. func updateValue(val int) {} declares a value parameter. func updateValue(&val int) {} and func updateValue(*val int) {} have invalid syntax.
  3. Final Answer:

    func updateValue(val *int) {} -> Option A
  4. Quick Check:

    Pointer parameter syntax = *type [OK]
Hint: Pointer parameter uses * before type, not variable name [OK]
Common Mistakes:
  • Putting * before variable name instead of type
  • Using & in parameter declaration
  • Confusing pointer syntax with value parameter
3. What is the output of this Go program?
package main
import "fmt"
func changeValue(val int) {
    val = 10
}
func main() {
    x := 5
    changeValue(x)
    fmt.Println(x)
}
medium
A. 10
B. 5
C. 0
D. Compilation error

Solution

  1. Step 1: Analyze function parameter passing

    The function changeValue receives x by value, so val is a copy of x.
  2. Step 2: Check if original variable changes

    Inside changeValue, val is set to 10, but this does not affect x in main.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    Passing by value means original x stays 5 [OK]
Hint: Passing by value means original variable unchanged [OK]
Common Mistakes:
  • Assuming x changes to 10 after function call
  • Confusing pointer and value passing
  • Expecting compilation error due to assignment
4. Identify the error in this Go code snippet:
func increment(num *int) {
    num = num + 1
}

func main() {
    x := 5
    increment(&x)
    fmt.Println(x)
}
medium
A. Missing & operator when calling increment.
B. x is not initialized before use.
C. Function parameter should not be a pointer.
D. Cannot add 1 to a pointer directly; need to dereference first.

Solution

  1. Step 1: Understand pointer arithmetic in Go

    Go does not support arithmetic on pointers like 'num + 1'. You must dereference the pointer to change the value.
  2. Step 2: Correct way to increment value pointed by pointer

    Use '*num = *num + 1' to increment the value at the pointer address.
  3. Final Answer:

    Cannot add 1 to a pointer directly; need to dereference first. -> Option D
  4. Quick Check:

    Pointer arithmetic invalid; dereference to modify [OK]
Hint: Use * to access pointer value before arithmetic [OK]
Common Mistakes:
  • Trying to add directly to pointer variable
  • Forgetting to dereference pointer
  • Confusing pointer and value types
5. You want to write a function in Go that doubles the value of an integer variable passed to it. Which approach correctly modifies the original variable?
hard
A. Return the doubled value and assign it outside the function.
B. Pass the integer by value and assign double inside the function.
C. Pass a pointer to the integer and modify the value via dereferencing.
D. Use a global variable instead of passing parameters.

Solution

  1. Step 1: Understand how to modify original variable

    Passing by value copies the variable, so changes inside function won't affect original.
  2. Step 2: Use pointer to modify original variable

    Passing a pointer allows the function to access and change the original variable's value via dereferencing.
  3. Step 3: Consider alternatives

    Returning doubled value requires assignment outside; using global variables is bad practice.
  4. Final Answer:

    Pass a pointer to the integer and modify the value via dereferencing. -> Option C
  5. Quick Check:

    Pointer passing modifies original variable [OK]
Hint: Use pointer to change original variable inside function [OK]
Common Mistakes:
  • Assuming passing by value changes original variable
  • Relying on return value without assignment
  • Using global variables unnecessarily
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