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
Which of the following is a valid type in Haskell?
a) String
b) Number
c) Boolean
d) Character
What is the type of True in Haskell?
a) Bool
b) Int
c) Char
d) String
Which function converts an integer to a floating-point number?
a) toFloat
b) fromIntegral
c) intToFloat
d) toInteger
What is the type of a single character in Haskell?
a) Char
b) String
c) Text
d) Literal
What is the default type for numeric literals in Haskell?
a) Int
b) Float
c) Num
d) Double
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
Which type would be used for logical expressions in Haskell?
a) Bool
b) Logic
c) Expression
d) Predicate
What is the result type of the expression 5 + 3.0 in Haskell?
a) Float
b) Double
c) Int
d) Compilation error
Which of these is not a basic type in Haskell?
a) Bool
b) Int
c) List
d) Char
What is the type of the expression [True, False, True]?
a) [Bool]
b) (Bool)
c) [Int]
d) Bool List
Type Inference and Type Declarations
What does Haskell use for type inference?
a) Hindley-Milner type system
b) Dynamic type checking
c) Pattern matching
d) Compile-time execution
Which keyword is used for explicit type declarations in Haskell?
a) type
b) let
c) ::
d) data
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
What type does the function length return?
a) Int
b) Integer
c) Num
d) a -> Int
What is the inferred type of (+ 2) in Haskell?
a) Int -> Int
b) Num a => a -> a
c) Float -> Float
d) Integer -> Integer
Which operator checks the type of a variable in Haskell?
a) :
b) ::
c) ==
d) <>
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.
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
What type does Haskell infer for the expression map not [True, False]?
a) [Bool]
b) Bool
c) Num
d) [Num]
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
Which keyword defines a custom data type in Haskell?
a) data
b) newtype
c) type
d) class
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.
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
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
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
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
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
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
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
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:
Qno
Answer (Option with Text)
1
a) String
2
a) Bool
3
b) fromIntegral
4
a) Char
5
c) Num
6
a) As a list of Char values
7
a) Bool
8
d) Compilation error
9
c) List
10
a) [Bool]
11
a) Hindley-Milner type system
12
c) ::
13
a) Compilation error
14
d) a -> Int
15
b) Num a => a -> a
16
b) ::
17
a) f takes two arguments of the same type
18
a) A type that can represent multiple types
19
a) [Bool]
20
a) It deduces the types based on input and output
21
a) data
22
a) It creates a new type with exactly one constructor and one field
23
a) Defines a type alias
24
a) A function that creates a value of a specific type
25
a) Maybe Int
26
a) To automatically generate instances for standard type classes like Eq, Ord, etc.
27
a) By deconstructing the values in function definitions