Bird
Raised Fist0
Goprogramming~20 mins

Slice creation in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Go Slice Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of slice length and capacity
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    s := make([]int, 3, 5)
    fmt.Println(len(s), cap(s))
}
A5 3
B3 5
C0 5
D3 3
Attempts:
2 left
💡 Hint
Remember: len is the number of elements, cap is the total allocated space.
❓ Predict Output
intermediate
2:00remaining
Slice literal with capacity
What will this program print?
Go
package main
import "fmt"
func main() {
    s := []int{1, 2, 3, 4}
    fmt.Println(len(s), cap(s))
}
A3 4
B4 8
C4 4
D4 0
Attempts:
2 left
💡 Hint
Slice literals have capacity equal to their length.
❓ Predict Output
advanced
2:00remaining
Appending beyond capacity
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    s := make([]int, 2, 3)
    s[0], s[1] = 10, 20
    s = append(s, 30)
    s = append(s, 40)
    fmt.Println(len(s), cap(s))
}
A4 6
B4 3
C4 4
D3 3
Attempts:
2 left
💡 Hint
Appending beyond capacity causes the slice to grow, usually doubling capacity.
❓ Predict Output
advanced
2:00remaining
Slice zero value behavior
What happens when you run this program?
Go
package main
import "fmt"
func main() {
    var s []int
    fmt.Println(len(s), cap(s))
    s = append(s, 1)
    fmt.Println(len(s), cap(s))
}
A
0 1
1 1
B
0 0
1 0
C
1 1
1 1
D
0 0
1 1
Attempts:
2 left
💡 Hint
A nil slice has zero length and capacity but can be appended to.
❓ Predict Output
expert
2:00remaining
Slice header and underlying array
What will this program print?
Go
package main
import "fmt"
func main() {
    arr := [4]int{1, 2, 3, 4}
    s := arr[1:3]
    s = append(s, 5)
    fmt.Println(arr)
    fmt.Println(s)
}
A
[1 2 3 5]
[2 3 5]
B
[1 2 3 4]
[2 3 5]
C
[1 2 3 4]
[2 3 4 5]
D
[1 2 3 4]
[2 3 4]
Attempts:
2 left
💡 Hint
Appending within capacity modifies the shared underlying array.

Practice

(1/5)
1. What is a slice in Go programming?
easy
A. A flexible, dynamically-sized list of elements.
B. A fixed-size array that cannot change length.
C. A function that slices strings.
D. A type of map with keys and values.

Solution

  1. Step 1: Understand slice definition

    A slice in Go is a data structure that holds a list of elements and can grow or shrink in size.
  2. Step 2: Compare with other types

    Unlike arrays, slices are dynamic and flexible, unlike maps or functions.
  3. Final Answer:

    A flexible, dynamically-sized list of elements. -> Option A
  4. Quick Check:

    Slice = dynamic list [OK]
Hint: Remember: slices can grow, arrays cannot [OK]
Common Mistakes:
  • Confusing slices with fixed-size arrays
  • Thinking slices are functions
  • Mixing slices with maps
2. Which of the following is the correct way to create an empty slice of integers in Go?
easy
A. var s []int
B. s := []int{}
C. s := make([]int, 0)
D. All of the above

Solution

  1. Step 1: Review slice creation methods

    In Go, you can create an empty slice by declaring a nil slice, using a slice literal with no elements, or using make with length zero.
  2. Step 2: Confirm all options are valid

    All three options create an empty slice of integers correctly.
  3. Final Answer:

    All of the above -> Option D
  4. Quick Check:

    Empty slice creation = D [OK]
Hint: Empty slices can be made in multiple valid ways [OK]
Common Mistakes:
  • Using make with negative length
  • Confusing array and slice syntax
  • Forgetting to initialize slice before use
3. What will be the output of this Go code?
package main
import "fmt"
func main() {
  s := []int{1, 2, 3}
  s = append(s, 4)
  fmt.Println(s)
}
medium
A. [1 2 3]
B. [1 2 3 4]
C. [4]
D. Compilation error

Solution

  1. Step 1: Understand initial slice and append

    The slice s starts with elements 1, 2, 3. The append function adds 4 to the end.
  2. Step 2: Print the updated slice

    After append, s contains [1 2 3 4], which is printed.
  3. Final Answer:

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

    append adds element to slice [OK]
Hint: append adds elements to the end of a slice [OK]
Common Mistakes:
  • Expecting append to replace elements
  • Thinking append returns a new slice without assignment
  • Confusing slice with array syntax
4. Identify the error in this Go code snippet:
package main
func main() {
  s := make([]int, 3)
  s[3] = 10
}
medium
A. Slice s is nil
B. Syntax error in make function
C. Index out of range error at s[3]
D. No error, code runs fine

Solution

  1. Step 1: Analyze slice length and indexing

    make([]int, 3) creates a slice with length 3, valid indices are 0, 1, 2.
  2. Step 2: Check index used in assignment

    Accessing s[3] is out of range and causes a runtime panic.
  3. Final Answer:

    Index out of range error at s[3] -> Option C
  4. Quick Check:

    Slice indices start at 0, max index = length-1 [OK]
Hint: Slice indices go from 0 to length-1 only [OK]
Common Mistakes:
  • Using index equal to slice length
  • Confusing slice length and capacity
  • Assuming make creates larger slice than specified
5. Given the code below, what will be the length and capacity of slice s after execution?
package main
import "fmt"
func main() {
  s := make([]int, 2, 5)
  s = append(s, 3, 4, 5)
  fmt.Println(len(s), cap(s))
}
hard
A. 5 5
B. 5 10
C. 7 10
D. 3 5

Solution

  1. Step 1: Understand make parameters

    make([]int, 2, 5) creates a slice with length 2 and capacity 5.
  2. Step 2: Append three elements

    Appending 3 elements increases length from 2 to 5, capacity remains 5 as it is enough.
  3. Final Answer:

    5 5 -> Option A
  4. Quick Check:

    Length grows with append, capacity stays if enough [OK]
Hint: Capacity grows only if append exceeds current capacity [OK]
Common Mistakes:
  • Assuming capacity doubles automatically
  • Confusing length and capacity
  • Forgetting append increases length
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