Bird
Raised Fist0
Goprogramming~20 mins

Why slices are used in Go - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Slice Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of slice capacity and length
What is the output of this Go program that uses slices?
Go
package main
import "fmt"
func main() {
  arr := [5]int{10, 20, 30, 40, 50}
  s := arr[1:4]
  fmt.Println(len(s), cap(s))
}
A4 3
B3 4
C4 4
D3 3
Attempts:
2 left
💡 Hint
Remember that length is the number of elements in the slice, capacity is from the start of the slice to the end of the underlying array.
🧠 Conceptual
intermediate
2:00remaining
Why slices are preferred over arrays in Go
Which reason best explains why slices are used instead of arrays in Go?
ASlices provide dynamic size and can grow or shrink, arrays have fixed size.
BSlices cannot be passed to functions, arrays can.
CSlices are stored on the stack, arrays are stored on the heap.
DSlices do not reference underlying data, arrays do.
Attempts:
2 left
💡 Hint
Think about flexibility in size when working with collections.
🔧 Debug
advanced
2:00remaining
Identify the runtime error with slices
What error will this Go code produce when run?
Go
package main
import "fmt"
func main() {
  s := []int{1, 2, 3}
  fmt.Println(s[3])
}
APrints 0
BCompilation error: index out of range
CIndex out of range runtime panic
DNo error, prints 3
Attempts:
2 left
💡 Hint
Check the valid indices for a slice of length 3.
❓ Predict Output
advanced
2:00remaining
Effect of appending on slice capacity
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
  s := make([]int, 2, 4)
  s[0], s[1] = 10, 20
  s = append(s, 30, 40, 50)
  fmt.Println(len(s), cap(s))
}
A5 10
B5 4
C5 5
D5 8
Attempts:
2 left
💡 Hint
Appending beyond capacity doubles the capacity.
🧠 Conceptual
expert
3:00remaining
Why slices improve memory efficiency in Go
Which statement best explains how slices improve memory efficiency compared to arrays?
ASlices share the underlying array, avoiding copying data when passed to functions.
BSlices always allocate new memory on append, preventing memory leaks.
CSlices store data inline, reducing pointer overhead compared to arrays.
DSlices automatically compress data to save memory.
Attempts:
2 left
💡 Hint
Think about how slices reference data and how that affects copying.

Practice

(1/5)
1.

Why are slices preferred over arrays in Go?

easy
A. Because arrays cannot store multiple values
B. Because slices use less memory than arrays
C. Because slices can change size dynamically while arrays have fixed size
D. Because slices are slower but more secure

Solution

  1. Step 1: Understand array size behavior

    Arrays in Go have a fixed size defined at creation and cannot grow or shrink.
  2. Step 2: Understand slice flexibility

    Slices are built on arrays but can dynamically change size, making them more flexible for varying data.
  3. Final Answer:

    Because slices can change size dynamically while arrays have fixed size -> Option C
  4. Quick Check:

    Slice size flexibility = A [OK]
Hint: Remember: slices grow, arrays don't [OK]
Common Mistakes:
  • Thinking arrays can resize dynamically
  • Confusing memory usage between slices and arrays
  • Believing slices are slower than arrays
2.

Which of the following is the correct way to declare a slice of integers in Go?

var s _____
easy
A. []int
B. [5]int
C. int[]
D. slice int

Solution

  1. Step 1: Recall slice syntax

    In Go, slices are declared with square brackets and no size: []type.
  2. Step 2: Identify correct option

    []int uses []int which is the correct syntax for a slice of integers.
  3. Final Answer:

    []int -> Option A
  4. Quick Check:

    Slice declaration = []int [OK]
Hint: Slices use []type without size [OK]
Common Mistakes:
  • Using fixed size like [5]int for slices
  • Writing type after brackets like int[]
  • Using invalid keywords like 'slice int'
3.

What will be the output of this Go code?

arr := [3]int{1, 2, 3}
slice := arr[:2]
slice = append(slice, 4)
fmt.Println(arr)
medium
A. [1 2 4]
B. [1 2 3 4]
C. [1 2 3]
D. Runtime error

Solution

  1. Step 1: Understand slice and array relation

    The slice references the first two elements of arr: [1, 2], with length 2 and capacity 3.
  2. Step 2: Append behavior on slice

    Appending 4 to slice: since current length 2 < capacity 3, it appends in place to the underlying array, setting arr[2] = 4. Thus, arr becomes [1 2 4].
  3. Final Answer:

    [1 2 4] -> Option A
  4. Quick Check:

    Append within capacity modifies underlying array = B [OK]
Hint: Append within capacity modifies shared array [OK]
Common Mistakes:
  • Thinking append always reallocates a new array
  • Expecting arr to grow to 4 elements
  • Thinking append causes runtime error
4.

Identify the error in this Go code snippet:

var s []int
s[0] = 10
fmt.Println(s)
medium
A. Syntax error: slice declaration is incorrect
B. Slice s is nil and has no elements, so s[0] assignment causes runtime panic
C. No error, code prints [10]
D. Compile error: cannot assign to slice element

Solution

  1. Step 1: Check slice initialization

    Variable s is declared but not initialized; it is nil with length 0.
  2. Step 2: Understand assignment to s[0]

    Assigning to s[0] causes runtime panic because no elements exist yet.
  3. Final Answer:

    Slice s is nil and has no elements, so s[0] assignment causes runtime panic -> Option B
  4. Quick Check:

    Assigning to nil slice index = runtime panic [OK]
Hint: Initialize slice before assigning elements [OK]
Common Mistakes:
  • Assuming slice auto-expands on index assignment
  • Thinking code compiles with no error
  • Confusing compile error with runtime panic
5.

You want to collect an unknown number of user scores efficiently. Which approach best uses slices?

1. Use a fixed-size array
2. Use a slice and append scores as they come
3. Use a map with indexes
4. Use a string to store scores
hard
A. Use a fixed-size array
B. Use a string to store scores
C. Use a map with indexes
D. Use a slice and append scores as they come

Solution

  1. Step 1: Consider data size flexibility

    User scores count is unknown, so fixed-size array is not suitable.
  2. Step 2: Evaluate slice usage

    Slices can grow dynamically and efficiently handle unknown data sizes by appending.
  3. Step 3: Exclude other options

    Maps add overhead and strings are not suitable for numeric data storage.
  4. Final Answer:

    Use a slice and append scores as they come -> Option D
  5. Quick Check:

    Dynamic data collection = slice append [OK]
Hint: Use slices to grow lists dynamically [OK]
Common Mistakes:
  • Choosing fixed arrays for unknown sizes
  • Using strings for numeric data
  • Overcomplicating with maps when slices suffice
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard