MCQs on Data Types and Type System | Haskell

Explore the fundamentals of Haskell programming with a focus on its type system. Learn about basic data types like Int, Float, Bool, and Char, understand type inference and declarations, create custom data types, and utilize collections like lists and tuples efficiently.


MCQs: Data Types and Type System in Haskell

Basic Data Types: Int, Float, Bool, Char, String

  1. Which of the following is a valid type in Haskell?
    • a) String
    • b) Number
    • c) Boolean
    • d) Character
  2. What is the type of True in Haskell?
    • a) Bool
    • b) Int
    • c) Char
    • d) String
  3. Which function converts an integer to a floating-point number?
    • a) toFloat
    • b) fromIntegral
    • c) intToFloat
    • d) toInteger
  4. What is the type of a single character in Haskell?
    • a) Char
    • b) String
    • c) Text
    • d) Literal
  5. What is the default type for numeric literals in Haskell?
    • a) Int
    • b) Float
    • c) Num
    • d) Double
  6. How are String values represented in Haskell?
    • a) As a list of Char values
    • b) As a separate data type
    • c) As a tuple of characters
    • d) As a mutable array
  7. Which type would be used for logical expressions in Haskell?
    • a) Bool
    • b) Logic
    • c) Expression
    • d) Predicate
  8. What is the result type of the expression 5 + 3.0 in Haskell?
    • a) Float
    • b) Double
    • c) Int
    • d) Compilation error
  9. Which of these is not a basic type in Haskell?
    • a) Bool
    • b) Int
    • c) List
    • d) Char
  10. What is the type of the expression [True, False, True]?
    • a) [Bool]
    • b) (Bool)
    • c) [Int]
    • d) Bool List

Type Inference and Type Declarations

  1. What does Haskell use for type inference?
    • a) Hindley-Milner type system
    • b) Dynamic type checking
    • c) Pattern matching
    • d) Compile-time execution
  2. Which keyword is used for explicit type declarations in Haskell?
    • a) type
    • b) let
    • c) ::
    • d) data
  3. What will happen if the inferred type conflicts with an explicit type declaration?
    • a) Compilation error
    • b) The explicit type overrides
    • c) The inferred type is used
    • d) Runtime exception
  4. What type does the function length return?
    • a) Int
    • b) Integer
    • c) Num
    • d) a -> Int
  5. What is the inferred type of (+ 2) in Haskell?
    • a) Int -> Int
    • b) Num a => a -> a
    • c) Float -> Float
    • d) Integer -> Integer
  6. Which operator checks the type of a variable in Haskell?
    • a) :
    • b) ::
    • c) ==
    • d) <>
  7. What does the following type signature mean: f :: a -> a -> a?
    • a) f takes two arguments of the same type and returns a value of the same type.
    • b) f takes two arguments of any type and returns an Int.
    • c) f takes one argument and returns the same argument.
    • d) f only works with Int types.
  8. What is a polymorphic type in Haskell?
    • a) A type that can represent multiple types
    • b) A type that changes at runtime
    • c) A type used in recursive functions
    • d) A type that represents lists
  9. What type does Haskell infer for the expression map not [True, False]?
    • a) [Bool]
    • b) Bool
    • c) Num
    • d) [Num]
  10. How does Haskell handle type inference for function composition?
    • a) It deduces the types based on input and output types of composed functions.
    • b) It requires explicit type declarations.
    • c) It defaults to Int.
    • d) It throws a compile-time error.

Defining Custom Data Types

  1. Which keyword defines a custom data type in Haskell?
    • a) data
    • b) newtype
    • c) type
    • d) class
  2. How does newtype differ from data in Haskell?
    • a) It creates a new type with exactly one constructor and one field.
    • b) It allows multiple constructors.
    • c) It is used for polymorphic types.
    • d) It defines type aliases.
  3. What does the type keyword in Haskell do?
    • a) Defines a type alias
    • b) Creates a new type
    • c) Declares a variable’s type
    • d) Combines two types
  4. What is a constructor in Haskell?
    • a) A function that creates a value of a specific type
    • b) A function that defines a type alias
    • c) A keyword for type inference
    • d) A pattern-matching operator
  5. What is the type of Just 5 if data Maybe a = Nothing | Just a?
    • a) Maybe Int
    • b) Maybe a
    • c) Just Int
    • d) Nothing
  6. What is the purpose of deriving in custom data types?
    • a) To automatically generate instances for standard type classes like Eq, Ord, etc.
    • b) To create polymorphic types
    • c) To infer type declarations
    • d) To initialize default values
  7. How is pattern matching used with custom data types?
    • a) By deconstructing the values in function definitions
    • b) By applying default types
    • c) By overriding type classes
    • d) By specifying global types
  8. Which of the following is a valid newtype declaration?
    • a) newtype Age = Age Int
    • b) data Age = Age Int
    • c) type Age = Age Int
    • d) let Age = Age Int
  9. What does data Tree a = Leaf a | Node (Tree a) (Tree a) represent?
    • a) A binary tree data structure
    • b) A list of trees
    • c) A polymorphic list
    • d) A tuple-based tree
  10. What is the type of [('a', 1), ('b', 2)] in Haskell?
    • a) [(Char, Int)]
    • b) [(Char, Integer)]
    • c) [Char, Int]
    • d) [Char -> Int]

Haskell’s data types and type system:

QnoAnswer (Option with Text)
1a) String
2a) Bool
3b) fromIntegral
4a) Char
5c) Num
6a) As a list of Char values
7a) Bool
8d) Compilation error
9c) List
10a) [Bool]
11a) Hindley-Milner type system
12c) ::
13a) Compilation error
14d) a -> Int
15b) Num a => a -> a
16b) ::
17a) f takes two arguments of the same type
18a) A type that can represent multiple types
19a) [Bool]
20a) It deduces the types based on input and output
21a) data
22a) It creates a new type with exactly one constructor and one field
23a) Defines a type alias
24a) A function that creates a value of a specific type
25a) Maybe Int
26a) To automatically generate instances for standard type classes like Eq, Ord, etc.
27a) By deconstructing the values in function definitions
28a) newtype Age = Age Int
29a) A binary tree data structure
30a) [(Char, Int)]

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