Go Slices MCQ

1. How do you declare a slice in Go?

a) var mySlice []int
b) mySlice := []int{}
c) []int mySlice
d) Both a and b

Answer:

d) Both a and b

Explanation:

A slice in Go can be declared either with 'var mySlice []int' for a nil slice, or 'mySlice := []int{}' for an empty slice.

2. What is the zero value of a slice in Go?

a) An empty array
b) nil
c) A slice with one zero-valued element
d) An uninitialized array

Answer:

b) nil

Explanation:

The zero value of a slice in Go is 'nil'.

3. How do you create a slice with preallocated underlying array in Go?

a) make([]int, length, capacity)
b) new([]int, length, capacity)
c) []int{length, capacity}
d) var []int{length, capacity}

Answer:

a) make([]int, length, capacity)

Explanation:

The 'make' function is used to create a slice with a specified length and capacity. The syntax is make([]Type, length, capacity).

4. What is the result of len() function when applied to a nil slice in Go?

a) 0
b) 1
c) nil
d) An error

Answer:

a) 0

Explanation:

The len() function returns 0 when applied to a nil slice in Go.

5. How do you append elements to a slice in Go?

a) append(mySlice, element)
b) mySlice.append(element)
c) mySlice += element
d) mySlice++ element

Answer:

a) append(mySlice, element)

Explanation:

The append function is used to add elements to a slice in Go. The syntax is append(slice, element).

6. Can you change the length of a slice after its declaration in Go?

a) Yes, by using append function
b) Yes, by reassigning with a new length
c) No, slices have a fixed length
d) Yes, by using resize function

Answer:

a) Yes, by using append function

Explanation:

The length of a slice in Go can be changed dynamically by appending elements to it using the append function.

7. What does the 'cap' function return when applied to a slice in Go?

a) The length of the slice
b) The capacity of the slice
c) The total size of the slice in bytes
d) The number of non-nil elements in the slice

Answer:

b) The capacity of the slice

Explanation:

The 'cap' function in Go returns the capacity of the slice, which is the total number of elements the slice can hold without reallocating.

8. How do you create a new slice by slicing an existing array 'arr' from index 2 to 5 in Go?

a) arr[2:5]
b) arr.slice(2, 5)
c) newSlice := arr[2:5]
d) Both a and c

Answer:

d) Both a and c

Explanation:

A new slice from an existing array can be created by specifying the start and end indices. The syntax is array[startIndex:endIndex].

9. What happens when you modify an element in a slice in Go?

a) It modifies the corresponding element in its underlying array.
b) It creates a new array.
c) It leaves the underlying array unchanged.
d) It raises an error.

Answer:

a) It modifies the corresponding element in its underlying array.

Explanation:

Slices in Go are references to an underlying array, so modifying an element in the slice also modifies the corresponding element in the array.

10. What is the result of slicing a slice beyond its capacity in Go?

a) A new slice with increased capacity is returned.
b) It results in a compile-time error.
c) It results in a runtime panic.
d) A nil slice is returned.

Answer:

c) It results in a runtime panic.

Explanation:

Slicing a slice beyond its capacity causes a runtime panic in Go.

11. Can you directly compare two slices in Go using the '==' operator?

a) Yes
b) No
c) Only if they have the same length
d) Only if they have the same capacity

Answer:

b) No

Explanation:

In Go, slices cannot be compared directly using the '==' operator. You must compare the individual elements.

12. How do you copy elements from one slice to another in Go?

a) Using the '=' operator
b) Using the copy() function
c) Slices cannot be copied
d) Using the clone() method

Answer:

b) Using the copy() function

Explanation:

The copy() function is used in Go to copy elements from one slice to another.

13. What is the underlying type of a slice in Go?

a) Array
b) Struct
c) Pointer
d) Map

Answer:

a) Array

Explanation:

A slice in Go is a reference to an underlying array.

14. How do you remove an element from a slice in Go?

a) Using the remove() function
b) Slices do not support element removal
c) By slicing out the element
d) By setting the element to nil

Answer:

c) By slicing out the element

Explanation:

To remove an element from a slice, you can slice out the element and then append the remaining parts.

15. How is a new underlying array created in a slice in Go?

a) When the slice length exceeds its capacity
b) By using the new() function
c) Automatically when appending elements
d) Both a and c

Answer:

d) Both a and c

Explanation:

A new underlying array for a slice is created when appending elements causes the slice length to exceed its capacity, or when explicitly using functions like append.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top