Reflection and generics in Go offer powerful tools for working with dynamic types and writing more flexible, reusable code. In this chapter, we will explore the reflect package, generics, and their limitations.
Chapter 2: Reflection and Generics in Go – MCQs
1. Using the reflect package for dynamic types
What is the purpose of the reflect package in Go?
a) To work with runtime types dynamically
b) To convert one type to another
c) To optimize code performance
d) To define interfaces dynamically
Which of the following functions in the reflect package returns the type of a value?
a) reflect.TypeOf()
b) reflect.GetType()
c) reflect.Type()
d) reflect.Kind()
What does reflect.ValueOf() return?
a) The variable’s value
b) The type of the variable
c) The address of the variable
d) The function signature of the variable
How do you get the underlying value of an interface using reflection?
a) reflect.Value()
b) reflect.Interface()
c) reflect.Elem()
d) reflect.String()
Which of the following does reflect.Value provide methods for?
a) Modifying the value of variables at runtime
b) Comparing types at compile time
c) Converting types between different languages
d) Determining the length of data structures
What is the result of calling reflect.TypeOf() on a struct in Go?
a) A string containing the name of the struct
b) A reflect.Type object representing the struct
c) A pointer to the struct
d) A copy of the struct’s fields
How do you change the value of a variable using reflection?
a) reflect.Set()
b) reflect.Modify()
c) reflect.Update()
d) reflect.Change()
Which of the following will reflect.ValueOf() return for a pointer type?
a) A reflect.Value object representing the pointer
b) The value stored at the pointer’s address
c) The type of the pointer
d) A reflect.Type object
Which method can be used to obtain the kind of a value in reflection?
a) reflect.KindOf()
b) reflect.TypeOf()
c) reflect.Kind()
d) reflect.Value()
How can you check if a value is nil in reflection?
a) reflect.IsNil()
b) reflect.Nil()
c) reflect.TypeOf(value).Kind() == reflect.Ptr
d) reflect.ValueOf(value).IsNil()
2. Understanding and Implementing Generics
Which version of Go introduced support for generics?
a) Go 1.5
b) Go 1.18
c) Go 1.12
d) Go 2.0
What is a generic type in Go?
a) A type that can hold any value
b) A type that supports multiple methods
c) A function or type that can work with any data type
d) A type that is unique to each function
Which keyword is used in Go to define a generic function?
a) generic
b) type
c) func
d) interface
Which of the following is the correct syntax to define a generic type in Go?
a) func Foo[T any](t T)
b) func Foo<T>(t T)
c) func Foo[T any, U any](t T)
d) type Foo[T]
What does the any type constraint in Go’s generics signify?
a) Any type, including concrete types and interfaces
b) Only string or integer types
c) A constraint for non-primitive types
d) A type that can be modified during runtime
Which of the following would be a valid use case for generics in Go?
a) A function that takes any type of number
b) A function that works only with int and float64 types
c) A function that uses reflection to determine types
d) A function that supports only specific interfaces
What is the constraint keyword used for in Go generics?
a) To define the permissible types for a generic type parameter
b) To restrict the number of function parameters
c) To declare that a function returns a value
d) To specify the size of the type
Can you use multiple constraints with generics in Go?
a) Yes, using an & operator
b) Yes, using a comma-separated list
c) No, Go supports only one constraint per generic type
d) No, constraints are not supported in Go generics
How do you declare a generic type for a struct in Go?
a) type Foo[T any] struct { value T }
b) struct Foo[T] { value T }
c) struct Foo[T any] { value T }
d) type Foo struct { value T any }
Can Go generics be used with functions that return values of different types?
a) Yes, generics allow a function to return any type
b) No, a function can only return one predefined type
c) Yes, but only if the function signature is interface{}
d) No, generics work only with data structures
3. Use cases and limitations of generics
Which of the following is a key benefit of using generics in Go?
a) Allows functions to be type-agnostic and reusable
b) Improves performance for large datasets
c) Increases readability and simplicity of code
d) Automatically generates code at compile time
In what situation would you use generics in Go?
a) When you need to create reusable, type-safe code
b) When you need dynamic typing
c) When you are optimizing for speed
d) When using interfaces only
What is a limitation of generics in Go?
a) Lack of support for generic methods on structs
b) Performance overhead due to code generation
c) Go does not support multiple generic constraints
d) No support for arrays or slices with generics
Can generics in Go be used with interface types?
a) Yes, they can be used with any type, including interfaces
b) No, generics only work with concrete types
c) Yes, but only with specific interfaces like io.Reader
d) No, generics are incompatible with interfaces
How do you restrict the types a generic function accepts in Go?
a) By using interface{} as the type constraint
b) By using type parameters with constraints like comparable
c) By declaring explicit types in the function signature
d) By using reflection to determine the type
Can you use Go generics with arrays?
a) Yes, Go generics work with arrays and slices
b) No, Go generics do not work with arrays
c) Yes, but only for fixed-length arrays
d) Yes, but only with one-dimensional arrays
What is the primary difference between Go’s generics and those in other languages like Java?
a) Go generics are more type-safe
b) Go generics require manual type specification
c) Go generics are simpler and don’t involve type erasure
d) Go generics use reflection for runtime type checking
How do you ensure that a function accepts only types that are comparable in Go?
a) By using the comparable constraint in the type parameter
b) By using any as the constraint
c) By explicitly declaring the types in the function signature
d) By using the reflect package
How would you implement a generic function to swap two values in Go?
a) func Swap[T any](a, b T) { a, b = b, a }
b) func Swap[T](a, b T) { return b, a }
c) func Swap(a, b interface{}) { a, b = b, a }
d) func Swap[T](a, b T) { a = b; b = a }
How are generic constraints used in Go?
a) To ensure that the type passed to a generic function matches the constraint
b) To restrict the use of interfaces with generic functions
c) To allow any type to be passed to a function
d) To make sure a type is registered in the runtime
Answers Table
Qno
Answer
1
a) To work with runtime types dynamically
2
a) reflect.TypeOf()
3
a) The variable’s value
4
c) reflect.Elem()
5
a) Modifying the value of variables at runtime
6
b) A reflect.Type object representing the struct
7
a) reflect.Set()
8
b) The value stored at the pointer’s address
9
c) reflect.Kind()
10
d) reflect.ValueOf(value).IsNil()
11
b) Go 1.18
12
c) A function or type that can work with any data type
13
c) func
14
a) func Foo[T any](t T)
15
a) Any type, including concrete types and interfaces
16
a) A function that takes any type of number
17
a) To define the permissible types for a generic type parameter
18
b) Yes, using a comma-separated list
19
a) type Foo[T any] struct { value T }
20
a) Yes, generics allow a function to return any type
21
a) Allows functions to be type-agnostic and reusable
22
a) When you need to create reusable, type-safe code
23
b) Performance overhead due to code generation
24
a) Yes, they can be used with any type, including interfaces
25
b) By using type parameters with constraints like comparable
26
a) Yes, Go generics work with arrays and slices
27
c) Go generics are simpler and don’t involve type erasure
28
a) By using the comparable constraint in the type parameter
29
a) func Swap[T any](a, b T) { a, b = b, a }
30
a) To ensure that the type passed to a generic function matches the constraint