Arrays & Slices Questions with Answers

19 practice questions on Arrays & Slices (Go) with answers and step-by-step solutions. Try 10 samples below, then practise all 19 free in the AptiNest app.

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)
}
  1. 4 [0 0 0 1]
  2. 5 [1 0 0 0 0]
  3. 1 [1]
  4. 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

  1. Array a has length 5, so len(a) = 5.
  2. All elements are zero unless set, so a = [0 0 0 0 1].
  3. fmt.Println prints len(a) and array: 5 [0 0 0 0 1].
  4. 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)
}
  1. true 0 []
  2. Runtime panic
  3. false 0 [7]
  4. 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

  1. A nil slice has length 0 and equals nil: true 0.
  2. append on a nil slice works.
  3. 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

What does append return?

  1. Nothing; it modifies the original slice header in place, like a method with a pointer receiver
  2. A boolean telling whether it grew
  3. The updated slice, which may point to a new array
  4. 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

  1. Append adds elements to a slice and may need a larger backing array.
  2. When reallocation occurs, a new array is created and a new slice header points to it.
  3. The function returns this new slice value.
  4. 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))
  1. false 0
  2. true 1
  3. Compile error
  4. 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

  1. var s []int gives the zero value nil.
  2. 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))
}
  1. 2 2
  2. 3 5
  3. 2 6
  4. 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

  1. S has 6 elements, so its length and capacity are both 6.
  2. s[1:3] holds elements 1 and 2, so its length is 2.
  3. Its capacity runs from index 1 to the end: 6 − 1 = 5.
  4. 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)
  1. 3 [1 2 3]
  2. 2 [1 2]
  3. 2 [1 2 3]
  4. 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

  1. dst has length 2.
  2. Source has 3 elements.
  3. 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])
  1. 9 9
  2. 1 1
  3. 1 9
  4. 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

  1. a is an array of 3.
  2. Assignment copies the array.
  3. 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)
}
  1. [10 20 30]
  2. [1 2 3]
  3. [0 0 0]
  4. 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

  1. S is a slice with values [1 2 3].
  2. Range creates a copy of each element, so v is a copy.
  3. V *= 10 changes the copy, not the original slice.
  4. fmt.Println(s) prints the original slice [1 2 3].
  5. 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?

  1. Go never frees arrays
  2. It still references the whole underlying array, so the GC cannot free it
  3. Slices copy the full array when created, so both the slice and the array use memory
  4. 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

  1. A slice holds a reference to the original array.
  2. The garbage collector cannot free the array while the slice references it.
  3. So the whole array stays in memory even if the slice is small.
  4. This prevents the GC from freeing the underlying array.
  5. 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. [1 2 3]
  2. [1 2 3 99]
  3. [1 2 99]
  4. [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

  1. b has len 2 and cap 3.
  2. append has room, so it reuses the array.
  3. 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 →

Related topics

See all Go topics · All topics