Sample questions
Question 1Easy
What is the output?
package main
import "fmt"
func main() {
var a [5]int
a[4] = 1
fmt.Println(len(a), a)
}
- 4 [0 0 0 1]
- 5 [1 0 0 0 0]
- 1 [1]
- 5 [0 0 0 0 1]
Answer: D. 5 [0 0 0 0 1]
Explanation
Arrays are zero-initialised and have fixed length 5.
Step-by-step solution
- Array a has length 5, so len(a) = 5.
- All elements are zero unless set, so a = [0 0 0 0 1].
- fmt.Println prints len(a) and array: 5 [0 0 0 0 1].
- Final output is 5 [0 0 0 0 1].
Why the other options are wrong
- A. The 1 is set at index 4, the last of five slots, so it prints at the end, not at index 3.
- B. The 1 goes at index 4, not index 0, so the first element stays 0.
- C. len(a) is 5, not 1; the array has length 5 whether or not its elements are set.
Tip: Remember arrays are zero-filled unless values are assigned.
Question 2Easy
What is the output?
package main
import "fmt"
func main() {
var s []int
fmt.Print(s == nil, " ", len(s), " ")
s = append(s, 7)
fmt.Println(s)
}
- true 0 []
- Runtime panic
- false 0 [7]
- true 0 [7]
Answer: D. true 0 [7]
Explanation
A nil slice has length 0, and append works on it by allocating a new array.
Step-by-step solution
- A nil slice has length 0 and equals nil: true 0.
- append on a nil slice works.
- The slice becomes [7].
Why the other options are wrong
- A. After append it is [7].
- B. Appending to nil is safe.
- C. A nil slice equals nil.
Tip: nil slice is usable.
Question 3Easy
- Nothing; it modifies the original slice header in place, like a method with a pointer receiver
- A boolean telling whether it grew
- The updated slice, which may point to a new array
- The new length
Answer: C. The updated slice, which may point to a new array
Explanation
You must use the result: s = append(s, x), because a reallocation produces a new header.
Step-by-step solution
- Append adds elements to a slice and may need a larger backing array.
- When reallocation occurs, a new array is created and a new slice header points to it.
- The function returns this new slice value.
- The updated slice, which may point to a new array.
Why the other options are wrong
- A. append does not update the slice in place; you must use its return value.
- B. append returns a slice, not a boolean; the growth is not reported as true or false.
- D. append returns the slice, not its length; use len on the result instead.
Tip: Write s = append(s, x) to keep the correct slice.
Question 4Easy
What is printed?
var s []int
fmt.Println(s == nil, len(s))
- false 0
- true 1
- Compile error
- true 0
Answer: D. true 0
Explanation
A declared but unassigned slice is nil with length 0. You can still call len and append on it.
Step-by-step solution
var s []int gives the zero value nil.- A nil slice has length 0.
Why the other options are wrong
- A. The zero value of a slice is nil.
- B. A nil slice has no elements.
- C. Comparing a slice with nil is allowed.
Question 5Medium
What is the output?
package main
import "fmt"
func main() {
s := []int{0, 1, 2, 3, 4, 5}
t := s[1:3]
fmt.Println(len(t), cap(t))
}
- 2 2
- 3 5
- 2 6
- 2 5
Answer: D. 2 5
Explanation
t has elements 1 and 2 (len 2); capacity runs from index 1 to the end of the array: 6 − 1 = 5.
Step-by-step solution
- S has 6 elements, so its length and capacity are both 6.
- s[1:3] holds elements 1 and 2, so its length is 2.
- Its capacity runs from index 1 to the end: 6 − 1 = 5.
- The output is 2 5, which is option D.
Why the other options are wrong
- A. The capacity is 5, not 2; capacity counts from the start index to the end of the array.
- B. The length is 2, not 3.
- C. The capacity is 5, not 6, because the slice starts at index 1.
Tip: For s[i:j], the length is j − i and the capacity is cap(s) − i.
Question 6Medium
What is printed?
dst := make([]int, 2)
n := copy(dst, []int{1, 2, 3})
fmt.Println(n, dst)
- 3 [1 2 3]
- 2 [1 2]
- 2 [1 2 3]
- 3 [1 2]
Answer: B. 2 [1 2]
Explanation
copy copies as many elements as the smaller of the two lengths and returns that count: 2.
Step-by-step solution
dst has length 2.- Source has 3 elements.
- Only 2 are copied and 2 is returned.
Why the other options are wrong
- A.
copy does not grow dst. - C. dst cannot hold three elements.
- D. The return value is the number copied, 2.
Question 7Medium
What is printed?
a := [3]int{1, 2, 3}
b := a
b[0] = 9
fmt.Println(a[0], b[0])
- 9 9
- 1 1
- 1 9
- 9 1
Answer: C. 1 9
Explanation
Arrays are values in Go, so b := a copies all elements. Changing b does not touch a.
Step-by-step solution
a is an array of 3.- Assignment copies the array.
- Only b[0] changes.
Why the other options are wrong
- A. Slices would share data, arrays do not.
- B. b[0] was set to 9.
- D. a keeps its own 1.
Question 8Medium
What is the output?
package main
import "fmt"
func main() {
s := []int{1, 2, 3}
for _, v := range s {
v *= 10
}
fmt.Println(s)
}
- [10 20 30]
- [1 2 3]
- [0 0 0]
- Compile error
Answer: B. [1 2 3]
Explanation
range gives a copy of each element; to modify, assign via s[i].
Step-by-step solution
- S is a slice with values [1 2 3].
- Range creates a copy of each element, so v is a copy.
- V *= 10 changes the copy, not the original slice.
- fmt.Println(s) prints the original slice [1 2 3].
- Final answer: [1 2 3]
Why the other options are wrong
- A. Changing v only changes the copy in the loop, so the slice keeps its original values 1, 2, 3.
- C. Nothing sets the elements to zero; v begins as a copy of each value, never zero.
- D. Compile error is wrong; assigning to the range variable v is legal Go, so the code compiles.
Question 9Hard
Why can a small slice keep a huge array alive in memory?
- Go never frees arrays
- It still references the whole underlying array, so the GC cannot free it
- Slices copy the full array when created, so both the slice and the array use memory
- Slices are stored on the stack forever
Answer: B. It still references the whole underlying array, so the GC cannot free it
Explanation
Copy the needed elements into a new slice if you keep a tiny part of a large buffer.
Step-by-step solution
- A slice holds a reference to the original array.
- The garbage collector cannot free the array while the slice references it.
- So the whole array stays in memory even if the slice is small.
- This prevents the GC from freeing the underlying array.
- Final answer: It still references the whole underlying array, so the GC cannot free it.
Why the other options are wrong
- A. Go does free arrays; the garbage collector reclaims them once no reference is left.
- C. Creating a slice does not copy the array; it only references the existing array.
- D. Storage on the stack is not the reason; the whole array stays alive because the slice references it.
Tip: Remember slices reference arrays, not copy them.
Question 10Hard
What is printed?
a := []int{1, 2, 3}
b := a[:2]
b = append(b, 99)
fmt.Println(a)
- [1 2 3]
- [1 2 3 99]
- [1 2 99]
- [99 2 3]
Answer: C. [1 2 99]
Explanation
b shares a's array and has capacity 3. Appending fits in that capacity, so the write lands in a[2].
Step-by-step solution
b has len 2 and cap 3.append has room, so it reuses the array.- a[2] becomes 99.
Why the other options are wrong
- A. The shared array is changed by the append.
- B.
a has a fixed length of 3. - D. The append goes to index 2, not 0.
Tip: Use the three-index slice a[:2:2] to force a copy.
Practise all 19 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →