Generics Questions with Answers

7 practice questions on Generics (Go) with answers and step-by-step solutions. Try 7 samples below, then practise all 7 free in the AptiNest app.

Sample questions

Question 1Easy

In a constraint such as func Max[T int | float64](a, b T) T, what does int | float64 mean?

  1. T must be both int and float64
  2. T is a pointer to int or float64
  3. T can be either int or float64
  4. T is ignored at compile time

Answer: C. T can be either int or float64

Explanation

The bar lists a type set in a constraint. Any one of the listed types can be used as T, but no type outside the set.

Step-by-step solution

  1. Constraints in square brackets limit T.
  2. The bar | means a union of types.
  3. So T may be int or float64.

Why the other options are wrong

  • A. One type cannot be both; the bar lists the allowed types.
  • B. The bar builds a type set, not a pointer type.
  • D. Type parameters are checked at compile time, not skipped.

Tip: A union constraint lets you use operators, such as +, that all listed types support.

Question 2Easy

In func Print[T any](v T), what does the constraint any mean?

  1. T can be any type
  2. T must be an interface value
  3. T must be a number
  4. T must be a pointer

Answer: A. T can be any type

Explanation

The constraint any places no restriction, so every type can be used as T.

Step-by-step solution

  1. Type parameters sit in square brackets.
  2. any allows every type.
  3. Operations are limited to what all types support.

Why the other options are wrong

  • B. T can be any concrete type too.
  • C. Numbers need a numeric constraint.
  • D. Pointers are only one possible choice.

Question 3Medium

What does ~int mean in a Go constraint such as interface{ ~int | ~float64 }?

  1. Exactly the built-in type int, and no named types at all
  2. Any pointer to an int value, written as *int in the code
  3. Any type with underlying type int, named types too
  4. Any integer type, such as int8 and uint

Answer: C. Any type with underlying type int, named types too

Explanation

The tilde makes the constraint match any type whose underlying type is int. So a named type such as type Age int also satisfies it.

Step-by-step solution

  1. ~int means the underlying type is int.
  2. type Age int has underlying type int.
  3. So Age satisfies ~int.

Why the other options are wrong

  • A. Without the tilde, only int itself matches; the tilde widens the match.
  • B. A pointer type is written with *, not with ~.
  • D. ~int matches types whose underlying type is exactly int; int8 and uint are different types.

Tip: Use ~ when named types, such as type Celsius float64, should also pass the constraint.

Question 4Medium

What is printed?

type Number interface{ ~int | ~float64 }

func Sum[T Number](xs []T) T {
    var s T
    for _, x := range xs {
        s += x
    }
    return s
}

fmt.Println(Sum([]float64{1.5, 2.5}))
  1. 4.0
  2. 4
  3. 3
  4. Compile error

Answer: B. 4

Explanation

Sum starts at the zero value of T and adds each element. 1.5 + 2.5 = 4.0, and fmt.Println prints it as 4.

Step-by-step solution

  1. s starts at the zero value.
  2. Adding 1.5 and then 2.5 gives 4.0.
  3. fmt.Println prints 4.

Why the other options are wrong

  • A. fmt.Println prints the float64 value 4 as 4, not 4.0.
  • C. The total is 1.5 + 2.5, not the sum of the integer parts 1 and 2.
  • D. float64 matches ~float64 in Number, so the call compiles.

Tip: Printing a float64 with the default format drops the trailing .0.

Question 5Medium

What is printed?

func Map[T, U any](s []T, f func(T) U) []U {
    out := make([]U, 0, len(s))
    for _, v := range s {
        out = append(out, f(v))
    }
    return out
}

fmt.Println(Map([]int{1, 2, 3}, func(n int) int { return n * n }))
  1. [1 2 3]
  2. [2 4 6]
  3. [1 4 9]
  4. [3 6 9]

Answer: C. [1 4 9]

Explanation

The function applies f to each element, so n*n gives 1, 4, 9. Types T and U are inferred as int.

Step-by-step solution

  1. T and U are inferred as int.
  2. Each element is squared.
  3. The result slice prints as [1 4 9].

Why the other options are wrong

  • A. Those are the originals; f squares them.
  • B. Doubling is not what f does.
  • D. The square of 3 is 9, not 3 times 3 per item.

Question 6Medium

What is printed?

type Stack[T any] struct{ items []T }

func (s *Stack[T]) Push(v T) { s.items = append(s.items, v) }

func (s *Stack[T]) Pop() T {
    n := len(s.items)
    v := s.items[n-1]
    s.items = s.items[:n-1]
    return v
}

s := &Stack[string]{}
s.Push("a")
s.Push("b")
v := s.Pop()
fmt.Println(v, len(s.items))
  1. a 1
  2. b 2
  3. ab 0
  4. b 1

Answer: D. b 1

Explanation

Pop removes the last item pushed (b) and returns it, which leaves one item. So the output is b 1.

Step-by-step solution

  1. Push adds a, then b.
  2. Pop returns the last item, b, and shrinks the slice.
  3. One item, a, remains, so len is 1.

Why the other options are wrong

  • A. Pop takes the last item pushed, which is b, not the first.
  • B. Pop removes the item, so one value is left, not two.
  • C. Pop returns one value, and one item is still in the stack.

Tip: A stack is last in, first out.

Question 7Hard

For func Eq[T comparable](a, b T) bool, which type cannot be used as the type argument T?

  1. []int
  2. int
  3. string
  4. struct{ X int }

Answer: A. []int

Explanation

comparable allows == and !=. Slices do not support == in Go, so []int is rejected at compile time. Maps and functions are rejected the same way.

Step-by-step solution

  1. comparable allows == and != on T.
  2. Slices support neither == nor != in Go.
  3. So []int does not satisfy the constraint.

Why the other options are wrong

  • B. int is comparable, so Eq[int] compiles.
  • C. string is comparable and works with ==.
  • D. A struct whose fields are all comparable is itself comparable.

Tip: If you need a slice as a key, convert it to a comparable form such as a string first.

Practise all 7 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