Golang Methods & Receivers Quiz
35 comprehensive questions on Golang methods and receivers, covering value vs pointer receivers, method sets, interface satisfaction, and practical usage patterns — with 18 code examples demonstrating method behavior and common scenarios.
Question 1
What is a method in Golang?
Question 2
What is a receiver in a Golang method?
Question 3
What is the difference between value and pointer receivers?
Question 4
What will this value receiver method do?
package main
import "fmt"
type Point struct {
x, y int
}
func (p Point) move(dx, dy int) {
p.x += dx
p.y += dy
}
func main() {
pt := Point{1, 2}
pt.move(3, 4)
fmt.Println(pt)
}Question 5
What will this pointer receiver method do?
package main
import "fmt"
type Point struct {
x, y int
}
func (p *Point) move(dx, dy int) {
p.x += dx
p.y += dy
}
func main() {
pt := Point{1, 2}
pt.move(3, 4)
fmt.Println(pt)
}Question 6
When should you use a pointer receiver?
Question 7
Can you call a pointer receiver method on a value?
Question 8
Can you call a value receiver method on a pointer?
Question 9
What is a method set?
Question 10
What methods are in the method set of a type T?
Question 11
What methods are in the method set of a pointer type *T?
Question 12
How does interface satisfaction work with receivers?
Question 13
In this scenario, does MyType satisfy the Writer interface?
package main
type Writer interface {
Write([]byte) (int, error)
}
type MyType struct{}
func (mt *MyType) Write(data []byte) (int, error) {
return len(data), nil
}Question 14
What happens if you mix receiver types for the same method name?
Question 15
Why might you choose a value receiver for a small struct?
Question 16
Imagine you're building a banking application. You have an Account struct with balance field. Which receiver type should the Deposit method use?
Question 17
In the same banking scenario, which receiver type should the GetBalance method use?
Question 18
What will this method call do?
package main
import "fmt"
type Counter struct {
value int
}
func (c *Counter) Increment() {
c.value++
}
func main() {
var c Counter
c.Increment()
fmt.Println(c.value)
}Question 19
What is the output of this interface satisfaction example?
package main
import "fmt"
type Speaker interface {
Speak() string
}
type Dog struct {
name string
}
func (d Dog) Speak() string {
return "Woof!"
}
func main() {
var s Speaker = Dog{"Buddy"}
fmt.Println(s.Speak())
}Question 20
In a large codebase with many developers, why is consistent receiver choice important?
Question 21
What happens when you embed a type with methods?
Question 22
Consider a scenario where you have a large struct with many fields. You're implementing a String() method for debugging. Which receiver type should you use?
Question 23
What is the method set of an interface type?
Question 24
In this embedded type scenario, which methods are available?
package main
type Engine struct {
power int
}
func (e Engine) Start() {
// start engine
}
type Car struct {
Engine
model string
}
func main() {
c := Car{}
c.Start() // This works
}Question 25
Why do some standard library types use pointer receivers?
Question 26
What happens if you try to modify a value receiver?
package main
type Point struct {
x, y int
}
func (p Point) SetX(x int) {
p.x = x // This modifies the copy
}
func main() {
pt := Point{1, 2}
pt.SetX(10)
// pt.x is still 1
}Question 27
In a team project, should receiver types be consistent across similar methods?
Question 28
What is the impact of receiver choice on performance?
Question 29
Can methods be defined on any type?
Question 30
What happens when you define a method on a built-in type?
Question 31
In this complex scenario, what is the output?
package main
import "fmt"
type Counter struct {
value int
}
func (c *Counter) Inc() {
c.value++
}
func (c Counter) String() string {
return fmt.Sprintf("Count: %d", c.value)
}
func main() {
c := &Counter{5}
c.Inc()
fmt.Println(c.String())
}Question 32
Why is the receiver the first parameter in method syntax?
Question 33
In a microservices architecture, you're implementing a UserService with methods like CreateUser, GetUser, UpdateUser. Which receiver types should these methods use?
Question 34
What is the key insight about method sets and interface satisfaction?
Question 35
What is the fundamental principle behind Golang's method system?
