MCQs on Reflection and Generics | Go

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

  1. 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
  2. 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()
  3. 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
  4. How do you get the underlying value of an interface using reflection?
    • a) reflect.Value()
    • b) reflect.Interface()
    • c) reflect.Elem()
    • d) reflect.String()
  5. 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
  6. 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
  7. How do you change the value of a variable using reflection?
    • a) reflect.Set()
    • b) reflect.Modify()
    • c) reflect.Update()
    • d) reflect.Change()
  8. 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
  9. 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()
  10. 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

  1. Which version of Go introduced support for generics?
    • a) Go 1.5
    • b) Go 1.18
    • c) Go 1.12
    • d) Go 2.0
  2. 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
  3. Which keyword is used in Go to define a generic function?
    • a) generic
    • b) type
    • c) func
    • d) interface
  4. 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]
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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 }
  10. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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 }
  10. 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

QnoAnswer
1a) To work with runtime types dynamically
2a) reflect.TypeOf()
3a) The variable’s value
4c) reflect.Elem()
5a) Modifying the value of variables at runtime
6b) A reflect.Type object representing the struct
7a) reflect.Set()
8b) The value stored at the pointer’s address
9c) reflect.Kind()
10d) reflect.ValueOf(value).IsNil()
11b) Go 1.18
12c) A function or type that can work with any data type
13c) func
14a) func Foo[T any](t T)
15a) Any type, including concrete types and interfaces
16a) A function that takes any type of number
17a) To define the permissible types for a generic type parameter
18b) Yes, using a comma-separated list
19a) type Foo[T any] struct { value T }
20a) Yes, generics allow a function to return any type
21a) Allows functions to be type-agnostic and reusable
22a) When you need to create reusable, type-safe code
23b) Performance overhead due to code generation
24a) Yes, they can be used with any type, including interfaces
25b) By using type parameters with constraints like comparable
26a) Yes, Go generics work with arrays and slices
27c) Go generics are simpler and don’t involve type erasure
28a) By using the comparable constraint in the type parameter
29a) func Swap[T any](a, b T) { a, b = b, a }
30a) To ensure that the type passed to a generic function matches the constraint

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top