0
0
Goprogramming~20 mins

Defining methods in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Go Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of method with pointer 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 main() {
    c := Counter{count: 5}
    c.Increment()
    fmt.Println(c.count)
}
A0
B5
C6
DCompilation error
Attempts:
2 left
💡 Hint
Think about whether the method changes the original struct or a copy.
Predict Output
intermediate
2:00remaining
Output of method with value receiver
What will this Go program print?
Go
package main
import "fmt"
type Number struct {
    value int
}
func (n Number) Double() {
    n.value = n.value * 2
}
func main() {
    num := Number{value: 10}
    num.Double()
    fmt.Println(num.value)
}
A10
B20
C0
DCompilation error
Attempts:
2 left
💡 Hint
Does the method change the original struct or a copy?
🔧 Debug
advanced
2:00remaining
Why does this setter method not update the struct?
This Go code does not update the name. What is the cause of the error?
Go
package main
import "fmt"
type Person struct {
    name string
}
func (p Person) SetName(newName string) {
    p.name = newName
}
func main() {
    p := Person{name: "Alice"}
    p.SetName("Bob")
    fmt.Println(p.name)
}
ACannot assign to p.name because p is a value receiver and method cannot modify it
BThe method SetName must return the new name to update p.name
Cfmt.Println cannot access p.name because it is unexported
DMissing pointer receiver in SetName method to modify original struct
Attempts:
2 left
💡 Hint
Think about how methods with value receivers behave when modifying fields.
🧠 Conceptual
advanced
2:00remaining
Method sets and interface implementation
Given the interface and struct below, which method set allows the struct to implement the interface?
Go
package main
import "fmt"
type Speaker interface {
    Speak() string
}
type Dog struct {}
func (d Dog) Speak() string {
    return "Woof!"
}
func (d *Dog) Speak() string {
    return "Bark!"
}
func main() {
    var s Speaker
    d := Dog{}
    s = d
    fmt.Println(s.Speak())
}
ANeither Dog nor *Dog implement Speaker because of method name conflict
BDog implements Speaker because it has Speak() with value receiver
CBoth Dog and *Dog implement Speaker interface
D*Dog implements Speaker because it has Speak() with pointer receiver
Attempts:
2 left
💡 Hint
Value receiver methods belong to both value and pointer types, pointer receiver methods belong only to pointer types.
Predict Output
expert
3:00remaining
Output of method with embedded struct and pointer receiver
What is the output of this Go program?
Go
package main
import "fmt"
type Animal struct {
    name string
}
func (a *Animal) Speak() string {
    return "I am " + a.name
}
type Cat struct {
    Animal
}
func (c Cat) Speak() string {
    return "Meow, " + c.name
}
func main() {
    c := Cat{Animal{name: "Whiskers"}}
    var a *Animal = &c.Animal
    fmt.Println(c.Speak())
    fmt.Println(a.Speak())
}
A
Meow, Whiskers
I am Whiskers
B
Meow, Whiskers
Meow, Whiskers
C
I am Whiskers
Meow, Whiskers
DCompilation error due to method conflict
Attempts:
2 left
💡 Hint
Remember that method calls depend on the type of the variable, not just the underlying data.