0
0
Goprogramming~20 mins

Method call behavior in Go - Practice Problems & Coding Challenges

Choose your learning style9 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.