Haskell Online Quiz

Welcome to our Haskell Online Quiz, specifically designed to evaluate and enhance your understanding of Haskell, a purely functional programming language known for its high level of abstraction and expressive syntax. This quiz spans basic to advanced concepts, including types, functions, laziness, and monads, ideal for both newcomers and seasoned programmers. Test your Haskell knowledge through carefully crafted questions and discover insights into this elegant and powerful language.

1. What is Haskell primarily known for?

a) Object-oriented programming
b) Imperative programming
c) Functional programming
d) Procedural programming

2. How do you define a function in Haskell to calculate the sum of two integers?

sumTwo :: Int -> Int -> Int
sumTwo x y = x + y
a) sumTwo :: Int -> Int -> Int\nsumTwo x y = x + y
b) function sumTwo(x, y) { return x + y }
c) def sumTwo(x: Int, y: Int): Int = x + y
d) int sumTwo(int x, int y) { return x + y; }

3. What is the output of the following Haskell code?

triple x = x * 3
main = print (triple 10)
a) 30
b) 10
c) 3
d) Error

4. What is the type of the following Haskell function?

f :: [Int] -> Int
f = foldr (+) 0
a) [Int] -> Int
b) Int -> [Int]
c) Int -> Int
d) [Int] -> [Int]

5. How do you create a list of integers from 1 to 10 in Haskell?

a) [1..10]
b) range(1, 10)
c) list(1, 10)
d) {1..10}

6. What does the map function do in Haskell?

result = map (*2) [1, 2, 3]
a) Doubles each element in the list
b) Filters elements from the list
c) Reduces the list to a single value
d) Repeats each element in the list twice

7. What is Haskell's type system characteristic?

a) Unchecked
b) Dynamic
c) Static
d) Weak

8. How do you define an infinite list of natural numbers in Haskell?

naturals = [1..]
a) naturals = [1..]
b) naturals = [1..Infinity]
c) naturals = repeat [1..]
d) naturals = cycle [1..100]

9. What does the following Haskell code snippet return?

f x y = if x > y then x else y
f 5 3
a) 5
b) 3
c) True
d) False

10. How do you declare a new type in Haskell for a binary tree?

data Tree a = Leaf a | Node (Tree a) (Tree a)
a) data Tree a = Leaf a | Node (Tree a) (Tree a)
b) type Tree a = Leaf a | Node (Tree a) (Tree a)
c) create Tree a = Leaf | Node (Tree a) (Tree a)
d) struct Tree { Leaf a; Node (Tree a) (Tree a); }

11. What function is used in Haskell to apply a function to a Maybe type only if it is Just?

a) justApply
b) maybe
c) applyJust
d) fmap

12. How do you concatenate lists in Haskell?

a) ++
b) concat
c) +=
d) append

13. What does the filter function do in Haskell?

result = filter even [1, 2, 3, 4]
a) Returns a list of all even numbers from the list
b) Counts the number of even numbers in the list
c) Finds the first even number in the list
d) Removes all even numbers from the list

14. What is the output of the following Haskell code?

let pairs = [(1, 'a'), (2, 'b'), (3, 'c')]
lookup 2 pairs
a) 'b'
b) (2, 'b')
c) 2
d) None

15. How do you perform integer division in Haskell?

a) /
b) //
c) div
d) %

16. What does the reverse function do in Haskell?

reverse [1, 2, 3, 4]
a) [4, 3, 2, 1]
b) [1, 2, 3, 4]
c) Error
d) None

17. How do you declare a function that takes an integer and returns a function from integer to integer in Haskell?

a) Int -> Int -> Int
b) Int -> (Int -> Int)
c) (Int, Int) -> Int
d) Int -> Int

18. What is the role of the let keyword in Haskell?

a) To define variables globally
b) To import modules
c) To define variables within a local scope
d) To perform input/output operations

19. How do you create a new type synonym in Haskell?

type StringList = [String]
a) type StringList = [String]
b) newtype StringList = [String]
c) create StringList = [String]
d) data StringList = [String]

20. What is the use of the do keyword in Haskell?

a) To define a new module
b) To initiate a loop
c) To sequence actions
d) To create data structures