Types, Methods, and Interfaces Flashcards

1
Q

What are concrete types in Go?

A

Types that use primitive or compound types, specifying how data is stored and providing method implementations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the scope of types declared within a block in Go?

A

Types declared within a block are scoped to that block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between concrete and abstract types in Go?

A

Concrete types specify how data is stored, while abstract types specify what a type should do.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you define a method for a user-defined type in Go?

A

Define it at the package level with a receiver specification, e.g., func (p Person) String() string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you invoke a method on a user-defined type in Go?

A

Example:
p := Person{"Fred", "Fredson", 52}
output := p.String()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between pointer and value receivers in Go?

A

Pointer receivers modify the receiver, while value receivers do not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the best practice for method receivers in Go?

A

Use pointer receivers consistently for all methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does Go automatically do for pointer and value receivers?

A

Go automatically takes the address for pointer receivers and dereferences pointers for value receivers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens when you call a method with a value receiver on a nil instance in Go?

A

It panics.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can methods be assigned to variables in Go?

A

Yes, methods can be assigned to variables, like closures, to access the type’s instance fields.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When should you use methods instead of functions in Go?

A

Use methods when the logic depends on values initialized at startup (e.g., structs).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does Go avoid inheritance?

A

Types based on another type have the same underlying type but no hierarchy; type conversion is required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of the iota keyword in Go?

A

It is used for defining enumerations with a limited set of values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is composition via embedding in Go?

A

Go allows embedding types in structs to reuse methods and fields, but it is not inheritance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does Go’s interface system work?

A

Interfaces define a method set that concrete types must implement; they are type-safe and follow duck typing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly