Bird
Raised Fist0
Goprogramming~20 mins

Method call behavior in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Go Method Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of method call with pointer vs value receiver

What is the output of this Go program?

Go
package main
import "fmt"
type Counter struct {
    count int
}
func (c Counter) Increment() {
    c.count++
}
func (c *Counter) IncrementPtr() {
    c.count++
}
func main() {
    c := Counter{count: 5}
    c.Increment()
    fmt.Println(c.count)
    c.IncrementPtr()
    fmt.Println(c.count)
}
A
6
5
B
5
5
C
5
6
D
6
6
Attempts:
2 left
💡 Hint

Think about whether the method receiver is a value or a pointer and how it affects the original struct.

❓ Predict Output
intermediate
2:00remaining
Method call on nil pointer receiver

What will this Go program print?

Go
package main
import "fmt"
type Node struct {
    value int
    next  *Node
}
func (n *Node) PrintValue() {
    if n == nil {
        fmt.Println("nil node")
        return
    }
    fmt.Println(n.value)
}
func main() {
    var n *Node = nil
    n.PrintValue()
}
Acompilation error
B0
Cruntime error: invalid memory address or nil pointer dereference
Dnil node
Attempts:
2 left
💡 Hint

Check how method calls on nil pointers behave when the method has a pointer receiver.

🔧 Debug
advanced
2:00remaining
Why does this method call cause a compilation error?

Consider this Go code snippet. Why does calling p.Increment() cause a compilation error?

Go
package main
import "fmt"
type Point struct {
    x, y int
}
func (p *Point) Increment() {
    p.x++
    p.y++
}
func main() {
    var p Point
    p.Increment()
    fmt.Println(p)
}
ANo error; the code compiles and prints {1 1}
BCompilation error because method Increment has pointer receiver but called on value without address
CRuntime panic due to nil pointer dereference
DCompilation error because method Increment is not defined for type Point
Attempts:
2 left
💡 Hint

Think about how Go handles method calls with pointer receivers on values.

❓ Predict Output
advanced
2:00remaining
Output of interface method call with pointer and value receivers

What is the output of this Go program?

Go
package main
import "fmt"
type Printer interface {
    Print()
}
type Data struct {
    value int
}
func (d Data) Print() {
    fmt.Println("Value:", d.value)
}
func (d *Data) PrintPtr() {
    fmt.Println("Pointer Value:", d.value)
}
func main() {
    d := Data{value: 10}
    var p Printer = d
    p.Print()
    var p2 Printer = &d
    p2.Print()
}
A
Value: 10
Pointer Value: 10
B
Value: 10
Value: 10
C
Pointer Value: 10
Pointer Value: 10
DCompilation error: cannot assign &d to Printer
Attempts:
2 left
💡 Hint

Check which methods satisfy the interface for value and pointer types.

🧠 Conceptual
expert
2:00remaining
Method sets and interface implementation rules

Which statement about method sets and interface implementation in Go is correct?

AA pointer to type T implements an interface if all interface methods have pointer receivers or value receivers on T
BA pointer to type T implements an interface if all interface methods have value receivers on T
CA value of type T implements an interface if all interface methods have pointer receivers on T
DA value of type T implements an interface only if all interface methods have pointer receivers on T
Attempts:
2 left
💡 Hint

Recall how Go defines method sets for values and pointers.

Practice

(1/5)
1. In Go, what happens when you call a method with a value receiver on a variable?
easy
A. The method can only read but not access the variable's fields.
B. The method works on a copy of the variable, so original data is not changed.
C. The method call causes a compile-time error.
D. The method directly modifies the original variable.

Solution

  1. Step 1: Understand value receiver behavior

    Methods with value receivers get a copy of the variable, not the original.
  2. Step 2: Effect on original variable

    Since the method works on a copy, changes inside the method do not affect the original variable.
  3. Final Answer:

    The method works on a copy of the variable, so original data is not changed. -> Option B
  4. Quick Check:

    Value receiver = copy, no original change [OK]
Hint: Value receivers work on copies, so original stays unchanged [OK]
Common Mistakes:
  • Thinking value receivers modify original data
  • Confusing pointer and value receivers
  • Assuming method call causes error
2. Which of the following is the correct syntax to define a method with a pointer receiver in Go?
easy
A. func (p *Person) UpdateName(newName string) {}
B. func (p Person*) UpdateName(newName string) {}
C. func (p Person) *UpdateName(newName string) {}
D. func *Person (p) UpdateName(newName string) {}

Solution

  1. Step 1: Recall pointer receiver syntax

    Pointer receivers are declared as (variable *Type) before method name.
  2. Step 2: Match correct syntax

    func (p *Person) UpdateName(newName string) {} matches the correct syntax: func (p *Person) MethodName(params) {}.
  3. Final Answer:

    func (p *Person) UpdateName(newName string) {} -> Option A
  4. Quick Check:

    Pointer receiver syntax = (p *Type) [OK]
Hint: Pointer receiver syntax: (var *Type) before method name [OK]
Common Mistakes:
  • Placing * after type name incorrectly
  • Using * before method name
  • Confusing pointer receiver with pointer return type
3. What will be the output of this Go program?
package main
import "fmt"
type Counter struct { count int }
func (c Counter) Increment() { c.count++ }
func (c *Counter) IncrementPtr() { c.count++ }
func main() {
  c := Counter{count: 5}
  c.Increment()
  fmt.Println(c.count)
  c.IncrementPtr()
  fmt.Println(c.count)
}
medium
A. 6\n7
B. 5\n5
C. 6\n6
D. 5\n6

Solution

  1. Step 1: Analyze Increment method call

    Increment has a value receiver, so c.count++ modifies a copy, original c.count remains 5.
  2. Step 2: Analyze IncrementPtr method call

    IncrementPtr has a pointer receiver, so c.count++ modifies original c.count, changing it from 5 to 6.
  3. Final Answer:

    5\n6 -> Option D
  4. Quick Check:

    Value receiver no change, pointer receiver changes original [OK]
Hint: Value receiver copies; pointer receiver modifies original [OK]
Common Mistakes:
  • Assuming value receiver changes original
  • Confusing method call effects
  • Ignoring pointer receiver behavior
4. Identify the error in this Go code snippet:
type Item struct { value int }
func (i Item) SetValue(v int) { i.value = v }
func main() {
  item := Item{value: 10}
  item.SetValue(20)
  fmt.Println(item.value)
}
medium
A. SetValue method uses a value receiver, so item.value is not updated.
B. Missing pointer receiver causes compile error.
C. fmt package is not imported, causing error.
D. SetValue method should return the new value.

Solution

  1. Step 1: Check method receiver type

    SetValue uses a value receiver, so it works on a copy of item.
  2. Step 2: Effect on original item

    Changes inside SetValue do not affect original item.value, so it remains 10.
  3. Final Answer:

    SetValue method uses a value receiver, so item.value is not updated. -> Option A
  4. Quick Check:

    Value receiver means no original update [OK]
Hint: Value receiver methods can't update original struct fields [OK]
Common Mistakes:
  • Assuming pointer receiver is mandatory for compilation
  • Thinking method return needed to update value
  • Ignoring import fmt requirement (not shown here)
5. Given this Go code, what will be the output?
package main
import "fmt"
type Number struct { n int }
func (num Number) Double() Number { num.n *= 2; return num }
func (num *Number) Triple() { num.n *= 3 }
func main() {
  x := Number{n: 2}
  y := x.Double()
  y.Triple()
  fmt.Println(x.n, y.n)
}
hard
A. 4 6
B. 2 6
C. 2 12
D. 4 12

Solution

  1. Step 1: Call x.Double()

    Double has a value receiver, so it works on a copy of x, doubles n from 2 to 4, and returns it as y.
  2. Step 2: Call y.Triple()

    Triple has a pointer receiver, so it modifies y.n in place, tripling 4 to 12.
  3. Step 3: Print values

    x.n remains 2 (unchanged), y.n is 12 after doubling then tripling.
  4. Final Answer:

    2 12 -> Option C
  5. Quick Check:

    Value receiver returns copy doubled, pointer receiver modifies copy [OK]
Hint: Value receiver returns copy; pointer receiver modifies that copy [OK]
Common Mistakes:
  • Assuming x.n changes after Double()
  • Confusing which variable is modified by Triple()
  • Ignoring return value from Double()
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