1. How do you declare a slice in Go?
Answer:
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?
Answer:
Explanation:
The zero value of a slice in Go is 'nil'.
3. How do you create a slice with preallocated underlying array in Go?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
A slice in Go is a reference to an underlying array.
14. How do you remove an element from a slice in Go?
Answer:
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?
Answer:
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.