Golang Methods & Receivers Quiz

Golang
0 Passed
0% acceptance

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.

35 Questions
~70 minutes
1

Question 1

What is a method in Golang?

A
A function associated with a specific type, called on values of that type
B
A function that takes no parameters
C
A special kind of variable
D
A built-in function
2

Question 2

What is a receiver in a Golang method?

A
The type instance the method is called on, appearing before the method name
B
The return value of the method
C
A parameter passed to the method
D
The method's name
3

Question 3

What is the difference between value and pointer receivers?

A
Value receivers work with a copy, pointer receivers work with the original
B
Value receivers are faster
C
Pointer receivers are only for built-in types
D
They are identical
4

Question 4

What will this value receiver method do?

go
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)
}
A
{1 2}
B
{4 6}
C
Compilation error
D
Runtime panic
5

Question 5

What will this pointer receiver method do?

go
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)
}
A
{4 6}
B
{1 2}
C
Compilation error
D
Runtime panic
6

Question 6

When should you use a pointer receiver?

A
When the method needs to modify the receiver or avoid copying large structs
B
Always, because it's more efficient
C
Never, value receivers are better
D
Only for built-in types
7

Question 7

Can you call a pointer receiver method on a value?

A
Yes, Golang automatically takes the address
B
No, compilation error
C
Only if the value is addressable
D
Only for interface methods
8

Question 8

Can you call a value receiver method on a pointer?

A
Yes, Golang automatically dereferences
B
No, compilation error
C
Only if the pointer is not nil
D
Only for interface methods
9

Question 9

What is a method set?

A
The collection of methods available on a type (value or pointer)
B
A set of method names
C
Built-in methods
D
Interface methods
10

Question 10

What methods are in the method set of a type T?

A
All methods with value receivers defined on T
B
All methods with pointer receivers
C
Only interface methods
D
No methods
11

Question 11

What methods are in the method set of a pointer type *T?

A
All methods with value or pointer receivers defined on T
B
Only pointer receiver methods
C
Only value receiver methods
D
No methods
12

Question 12

How does interface satisfaction work with receivers?

A
A type satisfies an interface if its method set contains all interface methods
B
Only value receivers satisfy interfaces
C
Only pointer receivers satisfy interfaces
D
Interfaces don't use receivers
13

Question 13

In this scenario, does MyType satisfy the Writer interface?

go
package main

type Writer interface {
    Write([]byte) (int, error)
}

type MyType struct{}

func (mt *MyType) Write(data []byte) (int, error) {
    return len(data), nil
}
A
No, because Write has a pointer receiver but MyType value doesn't have it in method set
B
Yes, pointer receivers work
C
Compilation error
D
Runtime error
14

Question 14

What happens if you mix receiver types for the same method name?

A
Compilation error - cannot define both value and pointer receivers for same method
B
It's allowed and works fine
C
Only the pointer receiver is used
D
Only the value receiver is used
15

Question 15

Why might you choose a value receiver for a small struct?

A
To avoid accidental modification and for immutability
B
To make it faster
C
To satisfy more interfaces
D
To use less memory
16

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?

A
Pointer receiver, because it needs to modify the balance
B
Value receiver, because balance changes should not happen
C
Either, it doesn't matter
D
No receiver needed
17

Question 17

In the same banking scenario, which receiver type should the GetBalance method use?

A
Value receiver, because it doesn't modify the account
B
Pointer receiver, to be consistent with other methods
C
Either, but pointer is more efficient
D
No receiver, make it a function
18

Question 18

What will this method call do?

go
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)
}
A
1
B
0
C
Compilation error
D
Runtime panic
19

Question 19

What is the output of this interface satisfaction example?

go
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())
}
A
Woof!
B
Compilation error
C
Runtime panic
D
Buddy
20

Question 20

In a large codebase with many developers, why is consistent receiver choice important?

A
It affects interface satisfaction and method availability
B
It makes code look nicer
C
It reduces compilation time
D
It doesn't matter
21

Question 21

What happens when you embed a type with methods?

A
The embedding type inherits the methods of the embedded type
B
The embedded type loses its methods
C
Methods are duplicated
D
No methods are inherited
22

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?

A
Value receiver, to avoid copying the large struct unnecessarily
B
Pointer receiver, because String() might be called on pointers
C
Either, performance difference is negligible
D
No receiver, make it a function
23

Question 23

What is the method set of an interface type?

A
The methods declared in the interface
B
All methods of all types
C
No methods
D
Built-in methods
24

Question 24

In this embedded type scenario, which methods are available?

go
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
}
A
Start() method is promoted to Car
B
Start() is not available on Car
C
Compilation error
D
Runtime error
25

Question 25

Why do some standard library types use pointer receivers?

A
To allow modification and avoid copying large structures
B
Because they are built-in
C
To prevent interface satisfaction
D
For performance only
26

Question 26

What happens if you try to modify a value receiver?

go
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
}
A
The original Point is unchanged
B
The original Point is modified
C
Compilation error
D
Runtime panic
27

Question 27

In a team project, should receiver types be consistent across similar methods?

A
Yes, for clarity and to avoid interface satisfaction issues
B
No, each method can choose independently
C
Only if they modify the receiver
D
It doesn't matter
28

Question 28

What is the impact of receiver choice on performance?

A
Pointer receivers avoid copying for large types, value receivers may copy
B
Pointer receivers are always slower
C
Value receivers are always slower
D
No performance difference
29

Question 29

Can methods be defined on any type?

A
Yes, on any named type defined in the same package
B
Only on structs
C
Only on built-in types
D
No, methods are not allowed
30

Question 30

What happens when you define a method on a built-in type?

A
Compilation error - cannot define methods on built-in types
B
It's allowed if in the same package
C
It's allowed using type aliases
D
Runtime error
31

Question 31

In this complex scenario, what is the output?

go
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())
}
A
Count: 6
B
Count: 5
C
Compilation error
D
Runtime panic
32

Question 32

Why is the receiver the first parameter in method syntax?

A
It's syntactic sugar for function calls with the receiver as first parameter
B
It's required for inheritance
C
It's a language requirement
D
It affects performance
33

Question 33

In a microservices architecture, you're implementing a UserService with methods like CreateUser, GetUser, UpdateUser. Which receiver types should these methods use?

A
Pointer receivers for all, since services often maintain state or are large
B
Value receivers for getters, pointer for modifiers
C
Value receivers for all, for immutability
D
No receivers, make them functions
34

Question 34

What is the key insight about method sets and interface satisfaction?

A
To satisfy an interface, a type must have all interface methods in its method set
B
Interfaces don't care about receivers
C
Only pointer types can satisfy interfaces
D
Method sets are irrelevant
35

Question 35

What is the fundamental principle behind Golang's method system?

A
Methods are functions with an implicit receiver parameter, enabling object-like behavior without classes
B
Methods are inherited from base classes
C
Methods are only for built-in types
D
Methods require virtual tables

QUIZZES IN Golang