Sample questions
Question 1Easy
In a constraint such as func Max[T int | float64](a, b T) T, what does int | float64 mean?
- T must be both int and float64
- T is a pointer to int or float64
- T can be either int or float64
- 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
- Constraints in square brackets limit T.
- The bar | means a union of types.
- 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?
- T can be any type
- T must be an interface value
- T must be a number
- 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
- Type parameters sit in square brackets.
any allows every type.- 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 }?
- Exactly the built-in type int, and no named types at all
- Any pointer to an int value, written as *int in the code
- Any type with underlying type int, named types too
- 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
- ~int means the underlying type is int.
- type Age int has underlying type int.
- 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}))
- 4.0
- 4
- 3
- 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
- s starts at the zero value.
- Adding 1.5 and then 2.5 gives 4.0.
- 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 2 3]
- [2 4 6]
- [1 4 9]
- [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
T and U are inferred as int.- Each element is squared.
- 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))
- a 1
- b 2
- ab 0
- 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
- Push adds a, then b.
- Pop returns the last item, b, and shrinks the slice.
- 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?
- []int
- int
- string
- 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
- comparable allows == and != on T.
- Slices support neither == nor != in Go.
- 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 →