Get a deep understanding of arrays and slices in Go programming with 30 multiple-choice questions. This chapter covers declaring and initializing arrays, slice operations, and built-in functions like append, copy, and len.
MCQs on Arrays and Slices in Go
1. Declaring and Initializing Arrays
How do you declare an array in Go? a) var arr [5]int b) arr := []int{5} c) var arr int[5] d) array[5] := int
What is the default value of an array element in Go? a) 0 for numeric types b) nil for all types c) An empty string d) It is undefined
How do you initialize an array with values in Go? a) arr := [5]{1, 2, 3, 4, 5} b) arr := [5]int{1, 2, 3, 4, 5} c) arr := {1, 2, 3, 4, 5} d) arr := []int{1, 2, 3, 4, 5}
What happens if you initialize an array without specifying its size in Go? a) Go automatically chooses the size based on the number of elements b) It will throw an error c) The size is fixed at 10 by default d) The array is uninitialized
How can you assign a value to an array element in Go? a) arr[2] = 5 b) arr{2} = 5 c) arr = 5[2] d) arr[5] = 2
Which statement correctly initializes a multidimensional array in Go? a) var arr [2][3]int b) arr := [2] [3] int{} c) var arr [2, 3] int d) arr := [3][2]int
How can you find the length of an array in Go? a) len(arr) b) arr.length() c) arr.size() d) size(arr)
What is the maximum number of elements an array can have in Go? a) Depends on the system’s memory b) 100 elements c) 1024 elements d) Go arrays have no limit
Can you change the size of an array after it has been declared in Go? a) Yes, by reinitializing it b) No, arrays have fixed sizes in Go c) Yes, by using slices d) Yes, but only for numeric types
What will the following code print: arr := [3]int{1, 2, 3}; fmt.Println(arr)? a) 1 2 3 b) [1, 2, 3] c) array[1 2 3] d) Error: incompatible types
2. Slices: Creation, Manipulation, and Internals
What is a slice in Go? a) A dynamically-sized array b) A reference to an array c) A built-in array type d) A pointer to an array
How do you create a slice in Go? a) slice := [5]int{1, 2, 3, 4, 5} b) slice := []int{1, 2, 3} c) slice := make([]int, 3) d) slice := new([]int)
Which function is used to create a slice from an existing array in Go? a) slice() b) arraySlice() c) make() d) [:]
How do you append an element to a slice in Go? a) append(slice, 5) b) slice.append(5) c) append(5, slice) d) slice.add(5)
What will happen if you append an element to a slice that has reached its capacity in Go? a) A new slice with double the capacity will be created b) The element will be added and the slice will remain the same size c) It will throw an error d) The element will overwrite the first element
How can you slice an array or slice in Go? a) arr[1:3] b) arr(1, 3) c) arr{1, 3} d) arr{1-3}
What is the capacity of a slice in Go? a) The number of elements in the slice b) The maximum number of elements that can be added to the slice c) The length of the array it references d) The number of elements that have been appended to the slice
What is the result of cap(slice) in Go? a) The total number of elements in the slice b) The number of elements the slice can hold before it needs to resize c) The length of the slice d) The number of elements added to the slice
What does the copy function do in Go? a) Copies elements from one slice into another b) Copies an entire array into a slice c) Copies the reference of one array to another d) It copies the array to a different array type
What is a key difference between arrays and slices in Go? a) Arrays are mutable, while slices are immutable b) Arrays are fixed-size, while slices are dynamically-sized c) Arrays are passed by reference, while slices are passed by value d) Slices cannot hold elements of different types
Can you modify the length of a slice in Go? a) Yes, slices are dynamically sized b) No, slices always have a fixed length c) Yes, by assigning a new value to the length property d) No, you can only modify the underlying array
Which of the following is true about the internals of slices in Go? a) Slices always refer to a new array in memory b) Slices share memory with the underlying array c) Slices are independent of the array d) Slices do not have any internal structure
What is the purpose of the len() function in the context of slices? a) It returns the size of the underlying array b) It returns the number of elements in the slice c) It checks the capacity of the slice d) It returns the index of the last element in the slice
How do you create a slice with a specific length and capacity in Go? a) slice := make([]int, 5, 10) b) slice := new([]int{5, 10}) c) slice := [5:10]int d) slice := []int{5, 10}
What will happen if you try to append an element to a nil slice in Go? a) It will throw an error b) It will create a new slice with the appended element c) The slice will be ignored d) It will append the element at index 0
How does Go handle memory management for slices? a) Slices are garbage-collected automatically b) Slices do not manage memory c) You must manually deallocate slice memory d) Slices use memory only during the program’s runtime
What does arr[:3] do in Go? a) It creates a slice from the first three elements of the array b) It creates a slice starting from the third element of the array c) It creates a slice that includes the first and third elements only d) It will throw an error
Which built-in function helps you ensure that a slice grows dynamically when adding elements in Go? a) append() b) copy() c) make() d) expand()
How can you append multiple elements to a slice in Go? a) append(slice, 1, 2, 3) b) slice.append(1, 2, 3) c) append(slice, [1, 2, 3]) d) append(slice, {1, 2, 3})
What does the following code do: slice := append([]int{}, 1, 2, 3)? a) Creates an empty slice and appends 1, 2, and 3 to it b) Creates a slice with elements 1, 2, and 3 c) Appends elements to an existing slice d) It will cause a runtime error
Answers Table
Qno
Answer (Option with Text)
1
a) var arr [5]int
2
a) 0 for numeric types
3
b) arr := [5]int{1, 2, 3, 4, 5}
4
a) Go automatically chooses the size based on the number of elements
5
a) arr[2] = 5
6
a) var arr [2][3]int
7
a) len(arr)
8
a) Depends on the system’s memory
9
b) No, arrays have fixed sizes in Go
10
b) [1, 2, 3]
11
a) A dynamically-sized array
12
b) slice := []int{1, 2, 3}
13
d) [:]
14
a) append(slice, 5)
15
a) A new slice with double the capacity will be created
16
a) arr[1:3]
17
b) The maximum number of elements that can be added to the slice
18
b) The number of elements the slice can hold before it needs to resize
19
a) Copies elements from one slice into another
20
b) Arrays are fixed-size, while slices are dynamically-sized
21
a) Yes, slices are dynamically sized
22
b) Slices share memory with the underlying array
23
b) It returns the number of elements in the slice
24
a) slice := make([]int, 5, 10)
25
b) It will create a new slice with the appended element
26
a) Slices are garbage-collected automatically
27
a) It creates a slice from the first three elements of the array
28
a) append()
29
a) append(slice, 1, 2, 3)
30
a) Creates an empty slice and appends 1, 2, and 3 to it