Composite Types Flashcards

1
Q

What is an array in Go?

A

A fixed-size sequence of elements of the same type.

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

How do you declare an array with zero values in Go?

A

var x [3]int initializes to [0 0 0].

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

How do you create an array with specific values in Go?

A

var x = [3]int{10, 20, 30} creates [10 20 30].

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

What happens when you access an out-of-bounds index in an array?

A

It causes a panic.

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

Why are arrays rarely used directly in Go?

A

Their fixed size limits flexibility; slices are preferred.

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

What is a slice in Go?

A

A dynamic sequence built on an array, with no fixed size.

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

What is the zero value of a slice?

A

nil (no underlying array).

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

How do you add elements to a slice?

A

Use append(x, value) to grow the slice.

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

What does make([]int, 5, 10) create?

A

A slice with length 5, capacity 10, initialized to [0 0 0 0 0].

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

How do you copy a slice in Go?

A

Use copy(dest, src) to duplicate elements.

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

What does len(s) return for a slice?

A

The number of elements in the slice.

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

The number of elements in the slice.

A

The capacity of the underlying array.

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

What is a string in Go?

A

An immutable sequence of bytes, UTF-8 encoded.

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

What does len(s) return for a string?

A

The number of bytes, not runes.

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

What is a rune in Go?

A

A Unicode code point, which may span multiple bytes.

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

How do you convert a string to a rune slice?

A

Use []rune(s).

17
Q

What is a map in Go?

A

A collection associating keys with values; zero value is nil.

18
Q

How do you check if a key exists in a map?

A

Use v, ok := m["key"]; ok is true if the key exists.

19
Q

How do you delete a key from a map?

A

Use delete(m, "key").

20
Q

What is a struct in Go?

A

A type that groups related data of different types.