MCQs on Arrays and Slices | Go

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

  1. 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
  2. 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
  3. 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}
  4. 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
  5. 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
  6. 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
  7. How can you find the length of an array in Go?
    a) len(arr)
    b) arr.length()
    c) arr.size()
    d) size(arr)
  8. 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
  9. 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
  10. 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

  1. 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
  2. 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)
  3. Which function is used to create a slice from an existing array in Go?
    a) slice()
    b) arraySlice()
    c) make()
    d) [:]
  4. 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)
  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
  6. 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}
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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}
  15. 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
  16. 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
  17. 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
  18. 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()
  19. 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})
  20. 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

QnoAnswer (Option with Text)
1a) var arr [5]int
2a) 0 for numeric types
3b) arr := [5]int{1, 2, 3, 4, 5}
4a) Go automatically chooses the size based on the number of elements
5a) arr[2] = 5
6a) var arr [2][3]int
7a) len(arr)
8a) Depends on the system’s memory
9b) No, arrays have fixed sizes in Go
10b) [1, 2, 3]
11a) A dynamically-sized array
12b) slice := []int{1, 2, 3}
13d) [:]
14a) append(slice, 5)
15a) A new slice with double the capacity will be created
16a) arr[1:3]
17b) The maximum number of elements that can be added to the slice
18b) The number of elements the slice can hold before it needs to resize
19a) Copies elements from one slice into another
20b) Arrays are fixed-size, while slices are dynamically-sized
21a) Yes, slices are dynamically sized
22b) Slices share memory with the underlying array
23b) It returns the number of elements in the slice
24a) slice := make([]int, 5, 10)
25b) It will create a new slice with the appended element
26a) Slices are garbage-collected automatically
27a) It creates a slice from the first three elements of the array
28a) append()
29a) append(slice, 1, 2, 3)
30a) Creates an empty slice and appends 1, 2, and 3 to it

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top