0
0
Goprogramming~20 mins

Pointer receivers in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pointer Receiver Mastery
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)
}
A6
BCompilation error
C0
D5
Attempts:
2 left
💡 Hint
Think about how pointer receivers modify the original struct.
Predict Output
intermediate
2:00remaining
Effect of value receiver on struct modification
What will be printed by this Go program?
Go
package main
import "fmt"
type Number struct {
    value int
}
func (n Number) Double() {
    n.value *= 2
}
func main() {
    num := Number{value: 10}
    num.Double()
    fmt.Println(num.value)
}
A0
B20
C10
DCompilation error
Attempts:
2 left
💡 Hint
Consider if the method changes the original struct or a copy.
🔧 Debug
advanced
2:30remaining
Why does this pointer receiver method not update the struct?
This Go code intends to update the field of a struct using a pointer receiver method, but it does not work as expected. What is the reason?
Go
package main
import "fmt"
type Item struct {
    name string
}
func (i *Item) Rename(newName string) {
    i = &Item{name: newName}
}
func main() {
    it := Item{name: "Old"}
    it.Rename("New")
    fmt.Println(it.name)
}
AThe method should have a value receiver instead of a pointer receiver.
BThe method Rename is missing a return statement.
CThe struct field name is unexported and cannot be modified.
DThe method assigns a new pointer to i, but does not modify the original struct's fields.
Attempts:
2 left
💡 Hint
Think about what happens when you assign to the pointer receiver variable inside the method.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in pointer receiver method
Which option contains a syntax error in defining a pointer receiver method in Go?
Afunc (p Person*) SetName(name string) { p.name = name }
B} eman = eman.p { )gnirts eman(emaNteS )nosreP* p( cnuf
Cunc (p *Person) SetName(name string) { p.name = name }
Dfunc (p *Person) SetName(name string) { p.name = name }
Attempts:
2 left
💡 Hint
Check the placement of the asterisk in the receiver syntax.
🚀 Application
expert
3:00remaining
Determine the final value after method calls with mixed receivers
Given the following Go code, what is the final value printed?
Go
package main
import "fmt"
type Counter struct {
    value int
}
func (c Counter) Add(n int) {
    c.value += n
}
func (c *Counter) AddPtr(n int) {
    c.value += n
}
func main() {
    c := Counter{value: 1}
    c.Add(2)
    c.AddPtr(3)
    fmt.Println(c.value)
}
A6
B4
C3
D1
Attempts:
2 left
💡 Hint
Remember which methods modify the original struct and which do not.