MCQs on Generics and Parametric Polymorphism | Scala

Dive into the world of generics and parametric polymorphism in Scala. Understand type parameters, bounds, variance annotations, and higher-kinded types with these 30 carefully crafted MCQs for Scala enthusiasts.


Generics and Parametric Polymorphism in Scala

1. Type Parameters and Bounds

  1. What is a type parameter in Scala?
    • A) A specific type that is fixed at compile time
    • B) A placeholder for a type that is determined at runtime
    • C) A placeholder for a type that is passed as a parameter to a class or method
    • D) A type used for inheritance only
  2. How do you specify a type parameter in Scala?
    • A) class ClassName[T]
    • B) def methodName[T](param: T)
    • C) class ClassName(type T)
    • D) def methodName(param: T)
  3. What is the purpose of type bounds in Scala?
    • A) To specify the types that a parameter must be a subtype of
    • B) To declare a fixed type for a parameter
    • C) To enable the use of abstract types
    • D) To restrict a function’s return type
  4. Which of the following represents a type bound that restricts a type to be a subclass of a specific class?
    • A) class MyClass[A <: SomeClass]
    • B) class MyClass[A >: SomeClass]
    • C) class MyClass[A <: Any]
    • D) class MyClass[A :> SomeClass]
  5. What does the >: operator represent in Scala’s type bounds?
    • A) It restricts the type to be a subtype of a given type
    • B) It restricts the type to be a supertype of a given type
    • C) It specifies an abstract type
    • D) It limits the return type of a function
  6. Which of the following is the correct syntax for defining a type parameter with an upper bound in Scala?
    • A) class MyClass[A <: Number]
    • B) class MyClass[A >: Number]
    • C) class MyClass[A :> Number]
    • D) class MyClass[A =: Number]
  7. What does the lower bound (>:) constraint imply for a type parameter?
    • A) The type parameter must be a subtype of the specified type
    • B) The type parameter must be a supertype of the specified type
    • C) The type parameter must be an exact match of the specified type
    • D) The type parameter must be a different type than the specified one
  8. What is the purpose of using a + sign before a type parameter in a Scala class or method?
    • A) It indicates a contravariant type
    • B) It indicates a covariant type
    • C) It specifies a lower bound for the type
    • D) It specifies an upper bound for the type
  9. How would you specify a method with a type parameter that can accept any type that is a subclass of Animal?
    • A) def method[T <: Animal](param: T)
    • B) def method[T >: Animal](param: T)
    • C) def method[T <: Any](param: T)
    • D) def method[T](param: T)
  10. What does the <: operator represent in Scala’s type bounds?
    • A) It restricts the type to be a supertype of a given type
    • B) It restricts the type to be a subtype of a given type
    • C) It allows any type to be passed in
    • D) It indicates an abstract type

2. Variance Annotations: Covariant, Contravariant, Invariant

  1. What does the term “variance” mean in Scala generics?
    • A) How types behave with respect to subtype relationships
    • B) The speed of type inference
    • C) The ability to accept any type
    • D) How methods handle different types
  2. What is a covariant type in Scala?
    • A) A type that preserves subtype relationships for its type parameter
    • B) A type that only accepts supertypes of its parameter
    • C) A type that only accepts subtypes of its parameter
    • D) A type that has no restriction on the parameter type
  3. How is covariance declared in Scala?
    • A) class MyClass[+A]
    • B) class MyClass[-A]
    • C) class MyClass[A]
    • D) class MyClass[A <: B]
  4. What is a contravariant type in Scala?
    • A) A type that accepts only the exact type of its parameter
    • B) A type that accepts supertypes of its parameter
    • C) A type that accepts subtypes of its parameter
    • D) A type that preserves subtype relationships for its parameter
  5. How is contravariance declared in Scala?
    • A) class MyClass[+A]
    • B) class MyClass[A]
    • C) class MyClass[-A]
    • D) class MyClass[A <: B]
  6. What does invariance mean in Scala?
    • A) The type parameter can accept only exactly the specified type
    • B) The type parameter can accept both subtypes and supertypes of the specified type
    • C) The type parameter can be freely substituted with other types
    • D) The type parameter is immutable
  7. Which of the following is true about covariance in Scala?
    • A) It allows a class to accept both subtypes and supertypes of a specified type
    • B) It allows the class to accept only subtypes of a specified type
    • C) It makes the class type invariant
    • D) It allows only exact matches of the type parameter
  8. What is the key benefit of using covariance in Scala?
    • A) It allows subtypes to be substituted where the class expects the type parameter
    • B) It ensures that only superclasses of a type are accepted
    • C) It enforces invariance in the type system
    • D) It makes the type system more flexible
  9. What is the effect of using contravariant type parameters in a method or class?
    • A) It allows the method or class to accept subtypes of the specified type
    • B) It allows the method or class to accept superclasses of the specified type
    • C) It allows the method or class to accept any type
    • D) It limits the method or class to accept exactly the specified type
  10. When would you typically use invariance in Scala?
    • A) When the class should only accept subtypes of the specified type
    • B) When the class should accept exactly the specified type without flexibility
    • C) When working with collections that do not change
    • D) When working with functional methods only

3. Higher-Kinded Types Basics

  1. What is a higher-kinded type in Scala?
    • A) A type that takes another type as a parameter
    • B) A type that is defined within a class
    • C) A function that returns another function
    • D) A type that can accept other types as parameters
  2. What does the F[_] syntax represent in higher-kinded types?
    • A) A type constructor that takes a type parameter
    • B) A specific type in a higher-order function
    • C) A class with an upper bound
    • D) A generic type for functions
  3. What is the role of higher-kinded types in functional programming?
    • A) To create more flexible and reusable abstractions
    • B) To limit the number of allowed types
    • C) To create types that are fixed at compile time
    • D) To restrict types to a specific set
  4. Which of the following is an example of a higher-kinded type in Scala?
    • A) trait Functor[F[_]] { def map[A, B](fa: F[A], f: A => B): F[B] }
    • B) class Functor[F[A]] { def map[A, B](fa: F[A], f: A => B): F[B] }
    • C) def fun(x: F[_]): Unit
    • D) class Functor[A] { def map(fa: A, f: A => B): A }
  5. Which of the following is NOT a benefit of using higher-kinded types?
    • A) Improved flexibility and abstraction
    • B) Ability to define generic container types
    • C) Better handling of mutability in types
    • D) Better handling of generic function types
  6. How can higher-kinded types be useful in working with containers in Scala?
    • A) By allowing the container to be parameterized by different types
    • B) By making containers mutable
    • C) By restricting containers to a single type
    • D) By simplifying the function signature
  7. Which of the following is an example of a higher-kinded type used in functional programming?
    • A) Option[_]
    • B) List[_]
    • C) Map[K, V]
    • D) All of the above
  8. What does the F[_] notation mean in Scala when defining higher-kinded types?
    • A) It refers to a type constructor that takes a single type parameter
    • B) It is a type alias for higher-order functions
    • C) It represents a function that takes two parameters
    • D) It refers to a specific type that can be substituted
  9. What is the purpose of higher-kinded types in the context of functional programming?
    • A) To make code more generic and reusable
    • B) To reduce the use of recursion
    • C) To prevent type inference
    • D) To enforce stricter type safety
  10. Which of the following best explains the use of higher-kinded types in Scala?
    • A) Higher-kinded types are used to define abstract types that take type constructors as parameters
    • B) Higher-kinded types restrict types to specific, pre-defined types
    • C) Higher-kinded types are used to define mutable collections
    • D) Higher-kinded types are used to simplify function signatures

Answer Key

QnoAnswer (Option with the text)
1C) A placeholder for a type that is passed as a parameter to a class or method
2A) class ClassName[T]
3A) To specify the types that a parameter must be a subtype of
4A) class MyClass[A <: SomeClass]
5B) It restricts the type to be a supertype of a given type
6A) class MyClass[A <: Number]
7B) The type parameter must be a supertype of the specified type
8B) It indicates a covariant type
9A) def method[T <: Animal](param: T)
10B) It restricts the type to be a subtype of a given type
11A) How types behave with respect to subtype relationships
12A) A type that preserves subtype relationships for its type parameter
13A) class MyClass[+A]
14B) A type that accepts supertypes of its parameter
15C) class MyClass[-A]
16A) The type parameter can accept only exactly the specified type
17B) It allows the class to accept only subtypes of a specified type
18A) It allows subtypes to be substituted where the class expects the type parameter
19B) It allows the method or class to accept superclasses of the specified type
20B) When the class should accept exactly the specified type without flexibility
21A) A type that takes another type as a parameter
22A) A type constructor that takes a type parameter
23A) To create more flexible and reusable abstractions
24A) trait Functor[F[_]] { def map[A, B](fa: F[A], f: A => B): F[B] }
25C) Better handling of mutability in types
26A) By allowing the container to be parameterized by different types
27D) All of the above
28A) It refers to a type constructor that takes a single type parameter
29A) To make code more generic and reusable
30A) Higher-kinded types are used to define abstract types that take type constructors as parameters

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