Haskell Language Professional Practice Exam, Exams of Technology

This exam tests proficiency in Haskell, a functional programming language. Candidates will demonstrate their ability to apply Haskell’s strong type system, lazy evaluation, and immutable data structures to build efficient and scalable software. Topics include higher-order functions, monads, concurrency, and advanced Haskell libraries. The exam requires candidates to solve problems using Haskell’s pure functional paradigms while optimizing for readability and performance.

Typology: Exams

2025/2026

Available from 12/24/2025

shilpi-jain-1
shilpi-jain-1 🇮🇳

4.2

(5)

29K documents

1 / 99

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Haskell Language Professional Practice
Exam
**Question 1.** Which of the following best describes a pure function in Haskell?
A) It may modify global variables.
B) It performs I/O directly.
C) Its output depends only on its input and has no side effects.
D) It uses mutable arrays internally.
Answer: C
Explanation: A pure function’s result is determined solely by its arguments and it does not cause
side effects such as I/O or state mutation.
**Question 2.** In Haskell, which keyword is used to declare an immutable variable?
A) var
B) let
C) mutable
D) const
Answer: B
Explanation: `let` introduces a binding that cannot be altered; Haskell variables are immutable
by default.
**Question 3.** Referential transparency allows you to:
A) Replace an expression with any value you like.
B) Replace an expression with its evaluated result without changing program behavior.
C) Change the order of sideeffectful statements arbitrarily.
D) Use mutable state safely.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63

Partial preview of the text

Download Haskell Language Professional Practice Exam and more Exams Technology in PDF only on Docsity!

Exam

Question 1. Which of the following best describes a pure function in Haskell? A) It may modify global variables. B) It performs I/O directly. C) Its output depends only on its input and has no side effects. D) It uses mutable arrays internally. Answer: C Explanation: A pure function’s result is determined solely by its arguments and it does not cause side effects such as I/O or state mutation. Question 2. In Haskell, which keyword is used to declare an immutable variable? A) var B) let C) mutable D) const Answer: B Explanation: let introduces a binding that cannot be altered; Haskell variables are immutable by default. Question 3. Referential transparency allows you to: A) Replace an expression with any value you like. B) Replace an expression with its evaluated result without changing program behavior. C) Change the order of side‑effectful statements arbitrarily. D) Use mutable state safely.

Exam

Answer: B Explanation: Because expressions are referentially transparent, they can be substituted by their values without affecting the program. Question 4. Which of the following is a static, strong type system feature of Haskell? A) Types are inferred at runtime. B) Types can change during execution. C) Types are checked at compile time and cannot be bypassed. D) Types are optional annotations. Answer: C Explanation: Haskell’s type system is static (checked at compile time) and strong (no implicit coercions). Question 5. How would you define a function add that takes two Ints and returns their sum using infix notation? A) add x y = x + y B) add = (+) C) add :: Int - > Int - > Int D) add = \x y - > x - y Answer: B Explanation: (+) is the infix addition operator; assigning it to add makes add a function that adds two numbers.

Exam

Explanation: The pattern x:xs deconstructs a non‑empty list into head (x) and tail (xs). Question 9. Which of the following statements about the Haskell layout (off‑side) rule is correct? A) Braces {} are mandatory for all function definitions. B) Indentation determines grouping of declarations when braces are omitted. C) Semicolons must separate every expression. D) The layout rule applies only to where clauses. Answer: B Explanation: Haskell uses indentation to infer block structure, allowing omission of explicit braces and semicolons. Question 10. What does the type signature map :: (a - > b) - > [a] - > [b] indicate? A) map takes a list and returns a function. B) map transforms a list of as into a list of bs using a function from a to b. C) map works only on numbers. D) map is a monadic operation. Answer: B Explanation: map receives a function (a - > b) and a list [a], producing a new list [b]. Question 11. If a function has no explicit type signature, how does GHC determine its type? A) It guesses randomly. B) It uses type inference based on usage.

Exam

C) It defaults to Int. D) It refuses to compile. Answer: B Explanation: GHC infers the most general type that satisfies all uses of the function. Question 12. In the type Either a b, what do the type variables a and b represent? A) Both are always String. B) a is the error type, b is the success type. C) a is the success type, b is the error type. D) They must be numeric. Answer: B Explanation: By convention, Left a holds an error value, while Right b holds a successful result. Question 13. Which statement about currying in Haskell is true? A) Functions take all arguments at once as a tuple. B) Every function actually takes a single argument and returns another function if more arguments are needed. C) Currying is optional and must be enabled with a pragma. D) Curried functions cannot be partially applied. Answer: B Explanation: Haskell functions are curried by default; a function of type a - > b - > c actually takes a and returns a function b - > c.

Exam

Answer: C Explanation: [] denotes the empty list pattern. Question 17. Which fold is tail‑recursive and therefore more memory‑efficient for large lists? A) foldr B) foldl C) foldl' D) foldMap Answer: C Explanation: foldl' is the strict version of foldl; it evaluates the accumulator eagerly, avoiding buildup of thunks. Question 18. What does the list comprehension [x*2 | x <- [1..5], even x] produce? A) [2,4,6,8,10] B) [4,8,12] C) [2,6,10] D) [1,2,3,4,5] Answer: B Explanation: It selects even x from 1..5 (2,4) and multiplies each by 2, yielding [4,8] plus x=5 is odd, so result [4,8]. Actually wait: even numbers are 2 and 4, *2 => 4 and 8, so answer B [4,8]. The option B shows [4,8,12] which is incorrect. Let's correct options: Provide correct answer. We'll adjust.

Exam

Revised Question 18. What does the list comprehension [x*2 | x <- [1..5], even x] produce? A) [2,4,6,8,10] B) [4,8] C) [2,6,10] D) [1,2,3,4,5] Answer: B Explanation: The generator yields numbers 1 through 5; the guard even x keeps 2 and 4; each is doubled, giving [4,8]. Question 19. Which guard syntax correctly defines a function that returns "positive" for positive integers and "non‑positive" otherwise? A) sign n | n > 0 = "positive" | otherwise = "non‑positive" B) sign n = if n > 0 then "positive" else "non‑positive" C) sign n | n > 0 - > "positive" | _ - > "non‑positive" D) sign n where n > 0 = "positive" Answer: A Explanation: Guards are written with | followed by a boolean expression and =; otherwise is a synonym for True. Question 20. Which higher‑order function removes all odd numbers from a list? A) map (mod2) xs B) filter odd xs C) filter even xs

Exam

A) All expressions are evaluated before the program starts. B) Expressions are evaluated only when their values are needed. C) The compiler forces strict evaluation by default. D) Memory is allocated statically. Answer: B Explanation: Haskell postpones computation until the result is demanded, enabling infinite data structures and potential performance gains. Question 24. Which expression creates an infinite list of natural numbers? A) [1..] B) repeat 1 C) cycle [1] D) enumFrom 1 Answer: A Explanation: [1..] uses the range syntax to generate an unbounded list starting at 1. Question 25. What is a thunk in Haskell? A) A strict data constructor. B) A placeholder for a deferred computation. C) A type of monad. D) An optimization flag. Answer: B

Exam

Explanation: A thunk is a runtime representation of a delayed expression awaiting evaluation. Question 26. Define a product type Person with fields name :: String and age :: Int using record syntax. Which declaration is correct? A) data Person = Person String Int B) data Person = Person { name :: String, age :: Int } C) type Person = (String, Int) D) newtype Person = Person String Int Answer: B Explanation: Record syntax uses curly braces to name fields, providing automatic accessor functions. Question 27. Which of the following defines a sum type Shape with constructors for a circle and a rectangle? A) data Shape = Circle Float | Rectangle Float Float B) type Shape = Circle Float | Rectangle Float Float C) newtype Shape = Circle Float | Rectangle Float Float D) data Shape = Circle { radius :: Float } & Rectangle { w :: Float, h :: Float } Answer: A Explanation: Sum types use the | symbol to separate alternative constructors. Question 28. What does the deriving clause deriving (Eq, Show) automatically provide for a data type? A) Custom serialization logic.

Exam

Explanation: Integral inherits from Real, which in turn inherits from Num; ultimately Num is the root numeric class. Question 31. How does the Maybe type help avoid null pointer errors? A) It forces the programmer to handle the Nothing case explicitly. B) It automatically converts null to a default value. C) It lazily evaluates the contained value. D) It provides mutable storage. Answer: A Explanation: Maybe a can be Just a or Nothing; pattern matching forces explicit handling of the absent case. Question 32. Which of the following expressions extracts the value from a Just constructor? A) fromJust (Just 5) B) Just 5 C) maybe 0 id (Just 5) D) Nothing 5 Answer: C Explanation: maybe default f (Just x) applies f to x; here id returns the value, yielding 5. Question 33. How can you update the age field of a Person record immutably? A) person.age = 30 B) person { age = 30 }

Exam

C) setAge person 30 D) person ++ { age = 30 } Answer: B Explanation: Record update syntax record { field = newValue } creates a new record with the modified field. Question 34. Which of the following correctly imports the Data.Map module qualified as M? A) import qualified Data.Map as M B) import Data.Map qualified as M C) import Data.Map M D) import qualified M = Data.Map Answer: A Explanation: The syntax import qualified Module as Alias creates a qualified import. Question 35. What is the purpose of the cabal file in a Haskell project? A) It defines the project’s build configuration, dependencies, and exposed modules. B) It contains the source code of the program. C) It is used only for testing. D) It replaces the GHC compiler. Answer: A Explanation: A .cabal file specifies package metadata, library/executable sections, dependencies, and build options.

Exam

Answer: A Explanation: The bang pattern ! in a data declaration makes the field strict, evaluating it to WHNF when the constructor is built. Question 39. How can you profile a Haskell program’s memory usage? A) Run ghc - prof - fprof-auto - rtsopts MyProg.hs and execute with +RTS - hc - RTS. B) Use ghc - O0. C) Add {-# PROFILE #-} pragma. D) Compile with -threaded. Answer: A Explanation: The -prof and -fprof-auto flags enable profiling; +RTS - hc - RTS tells the runtime to collect heap profiling data. Question 40. Which library provides property‑based testing in Haskell? A) HUnit B) tasty C) QuickCheck D) hspec Answer: C Explanation: QuickCheck generates random inputs to test that properties hold for many cases. Question 41. What does the putStrLn function have type signature? A) String - > IO ()

Exam

B) IO String C) String - > IO String D) IO () - > String Answer: A Explanation: putStrLn takes a String and returns an IO action that produces no result (()). Question 42. In the do notation, which operator sequences two actions, discarding the result of the first? A) >>= B) >> C) <$> D) . Answer: B Explanation: >> runs the first action, ignores its result, then runs the second. Question 43. Which of the following is a lawful Functor instance for the list type? A) fmap = (++) B) fmap = map C) fmap = foldr D) fmap = concat Answer: B Explanation: Functor [] is defined with fmap = map, applying a function to each element.

Exam

D. Defines list concatenation. Answer: A Explanation: MonadError e m defines throwError and catchError for error handling within monads. Question 47. Which of the following modules would you import to use the State monad? A) Control.Monad.State B) Data.State C) System.State D) Control.State Answer: A Explanation: The State monad is defined in Control.Monad.State. Question 48. How does the Writer monad accumulate output? A) Using a mutable buffer. B) By concatenating values with a Monoid instance via tell. C) By writing directly to a file. D) By using IORef. Answer: B Explanation: Writer w a stores a log of type w that must be a Monoid; tell appends to the log.

Exam

Question 49. Which of the following is a correct type signature for a function that reads a line from stdin and returns it as an Int? A) readInt :: IO Int B) readInt :: String - > IO Int C) readInt :: IO String D) readInt :: Int - > IO String Answer: A Explanation: The function performs I/O and yields an Int, so its type is IO Int. Question 50. What does the seq function do? A) Forces its first argument to be evaluated to weak head normal form before returning the second. B) Concatenates two lists. C) Creates a new thread. D) Delays evaluation indefinitely. Answer: A Explanation: seq a b evaluates a to WHNF, then returns b; it is used to avoid thunk buildup. Question 51. Which GHC flag enables strict data fields by default? A) -O B) -fno-warn-unused-binds C) -XStrictData D) -threaded