


















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Tests knowledge of functional programming concepts, including immutability, higher-order functions, recursion, type systems, and how these principles are applied in languages like Haskell, Scala, and F#.
Typology: Exams
1 / 90
This page cannot be seen from the preview
Don't miss anything!



















































































Question 1. Which property of a data structure guarantees that its value cannot be altered after creation? A) Mutability B) Immutability C) Volatility D) Transience Answer: B Explanation: Immutability means once a value is created it cannot be changed, which is essential for safe concurrency. Question 2. In a pure function, which of the following must always hold? A) It may read global variables. B) It may produce side‑effects. C) It always returns the same output for the same input. D) It depends on the current time. Answer: C Explanation: Pure functions are referentially transparent; given identical arguments they always yield identical results. Question 3. Which functional construct allows a function to be passed as an argument to another function? A) Recursion B) Higher‑order function C) Tail call D) Pattern matching Answer: B
Explanation: Higher‑order functions treat functions as first‑class values, enabling them to be passed or returned. Question 4. What does tail‑call optimization (TCO) primarily prevent? A) Memory leaks B) Stack overflow C) Race conditions D) Null pointer exceptions Answer: B Explanation: TCO rewrites tail‑recursive calls to reuse the current stack frame, avoiding stack growth. Question 5. Which of the following is a product type? A) Either<A,B> B) Option
D. fun (Int, String) tuple = (1, "a") Answer: A Explanation: The function works for any type T, illustrating generic (parametric) polymorphism. Question 10. Which type is commonly used to represent a possibly missing value without using null? A) Exception B) Result C) Option/Maybe D. Void Answer: C Explanation: Option (or Maybe) explicitly encodes presence (Some) or absence (None) of a value. Question 11. In the context of functors, which law must hold for the map operation? A) map id = id B. map (f. g) = map f. map g C. Both A and B D. None of the above Answer: C Explanation: Functor laws require identity preservation and composition preservation for map. Question 12. Which of the following best illustrates an applicative functor? A) list.map(f) B) option.flatMap(f)
C) Applicative.pure(3).ap(funct) D) Future.then(f) Answer: C Explanation: Applicatives provide pure and ap to apply a function wrapped in a context to a value wrapped in a context. Question 13. The monad law of left identity can be expressed as: A) return a >>= f == f a B) m >>= return == m C) (m >>= f) >>= g == m >>= (\x - > f x >>= g) D) None of the above Answer: A Explanation: Left identity states that wrapping a value and then binding it is equivalent to applying the function directly. Question 14. Which lambda calculus conversion replaces a bound variable with a fresh name? A) α‑conversion B) β‑reduction C) γ‑conversion D) δ‑reduction Answer: A Explanation: α‑conversion renames bound variables to avoid capture. Question 15. Eager (strict) evaluation differs from lazy evaluation in that: A) It evaluates arguments only when needed.
Question 18. Which concurrency model eliminates the need for explicit locks by using immutable messages? A) Shared‑memory threading B. Actor model C. Coroutine model D. Transactional memory Answer: B Explanation: Actors communicate via immutable messages, avoiding shared mutable state and locks. Question 19. In functional domain modeling, making an illegal state unrepresentable is achieved by: A) Using runtime assertions. B) Using exhaustive match statements. C) Encoding invariants in the type system (e.g., ADTs). D. Adding comments to the code. Answer: C Explanation: Types can express constraints so that illegal values cannot be constructed. Question 20. When integrating functional code with legacy OOP code, the adapter pattern is used to: A) Convert mutable objects into immutable ones. B. Translate OOP interfaces into functional types. C. Optimize performance of functional code. D. Automatically generate type classes. Answer: B
Explanation: An adapter wraps OOP classes, exposing a functional API that matches the rest of the codebase. Question 21. Which of the following is a typical effect‑type used to model database interactions in a pure functional language? A) IO B) Task C) ReaderT Connection D. Future Answer: C Explanation: ReaderT can carry a database connection, allowing pure code to request queries without performing side effects directly. Question 22. The Elm Architecture’s core concept “Model‑View‑Update” maps to which functional ideas? A. Model = immutable state, View = pure function, Update = pure function returning new state. B. Model = mutable object, View = side‑effecting render, Update = imperative loop. C. Model = database schema, View = SQL query, Update = transaction. D. Model = thread pool, View = scheduler, Update = lock manager. Answer: A Explanation: The architecture emphasizes immutable models, pure view rendering, and pure update functions that return a new model. Question 23. Which of the following languages enforces purity by default? A) Scala B) Haskell
Question 26. When using the Either type for error handling, which side conventionally represents success? A) Left B) Right C. Both sides can be success. D. Neither side; it only represents failure. Answer: B Explanation: By convention, Right holds the successful value, while Left holds error information. Question 27. Which of the following best describes a “persistent” data structure? A) One that is stored on disk. B) One that can be mutated in place. C) One that preserves previous versions after updates. D. One that automatically syncs across network nodes. Answer: C Explanation: Persistent (functional) structures keep old versions accessible after modifications, using immutability. Question 28. Which of the following is a correct statement about tail recursion? A) It always uses less memory than a loop. B) It can be optimized only if the recursive call is the last operation. C) It can be used only with primitive types. D. It guarantees constant time execution. Answer: B
Explanation: Tail‑call optimization applies when the recursive call is in tail position (the final action). Question 29. In a language with type classes, the Eq type class provides which capability? A) Ordering of elements. B) Equality testing. C) Serialization to JSON. D. Concurrency control. Answer: B Explanation: Eq defines methods like (==) and (/=) for testing equality. Question 30. Which of the following best explains why immutable data simplifies reasoning about parallel programs? A) It eliminates the need for synchronization primitives. B) It makes the program run faster. C) It allows variables to be shared across threads without copying. D. It enables automatic garbage collection. Answer: A Explanation: Since immutable data cannot be changed, concurrent threads cannot cause race conditions, removing the need for locks. Question 31. Which combinator transforms a list of Option<T> into an Option<List<T>> only when all elements are Some? A) sequence B) traverse C) map
B) It aggregates elements using an accumulator function. C) It filters elements based on a predicate. D. It sorts the collection. Answer: B Explanation: fold traverses a structure, applying a binary function to combine elements into a single result. Question 35. In the context of category theory, a functor must preserve which two aspects? A) Identity morphisms and composition. B. Types and values. C. Memory layout and performance. D. Concurrency and mutability. Answer: A Explanation: Functor laws require that F(id) = id and F(g ∘ f) = F(g) ∘ F(f). Question 36. Which of the following is an example of a “higher‑order” function in JavaScript? A) Math.max B) Array.prototype.map C) console.log D. new Date() Answer: B Explanation: map takes a function as an argument and returns a new array, making it higher‑order.
Question 37. Which of the following best captures the purpose of the IO monad in Haskell? A) To perform pure calculations faster. B) To represent side‑effecting operations as values. C) To enforce type safety for numeric operations. D. To manage memory allocation. Answer: B Explanation: IO a encapsulates an effectful computation that, when executed, yields a value of type a. Question 38. In a functional language, which construct is used to guarantee that a function cannot return null? A) Option type B) Int type C. String type D. Unit type Answer: A Explanation: Option forces callers to handle the None case explicitly, avoiding null. Question 39. Which of the following statements about lazy evaluation is true? A) It always reduces runtime performance. B) It can enable representation of infinite data structures. C. It forbids recursion. D. It requires explicit await keywords. Answer: B
D. Using a linked list underneath. Answer: B Explanation: Persistent vectors are usually implemented as trees; updates recreate only the path to the changed leaf, sharing the rest. Question 43. Which of the following is a typical effect of using pure functions for business logic? A) Increased difficulty in testing. B) Deterministic and repeatable tests. C. Need for extensive mocking. D. Higher runtime memory consumption. Answer: B Explanation: Pure functions always produce the same output for the same input, making them easy to test deterministically. Question 44. In a functional reactive programming (FRP) library, a “stream” is best described as: A) A mutable array. B) A time‑varying value that can be observed. C. A compiled binary file. D. A static configuration object. Answer: B Explanation: FRP streams represent values that evolve over time and can be subscribed to for updates. Question 45. Which of the following is a correct use of the ap function for an applicative functor?
A) Just (add 2) <*> Just 3 B) Just (add 2) >>= Just 3 C) Just (add 2). Just 3 D. Just (add 2) ++ Just 3 Answer: A Explanation: ap (<*>) applies a function wrapped in a context (Just (add 2)) to a value wrapped in the same context (Just 3). Question 46. Which of the following statements about “tail recursion” is false? A) It can be optimized into a loop by the compiler. B) It requires the recursive call to be the final action. C) It always eliminates the need for a stack. D. It can be expressed with the tailrec annotation in Kotlin. Answer: C Explanation: Tail recursion eliminates growing the call stack only when the compiler/runtime performs TCO; it does not inherently remove the stack. Question 47. In Haskell, which type class provides the >>= (bind) operator? A) Functor B) Applicative C) Monad D. Foldable Answer: C Explanation: >>= is defined by the Monad type class for sequencing monadic actions.
Explanation: Since immutable collections never change, multiple threads can read them concurrently without synchronization. Question 51. In a functional language, which construct is typically used to model stateful computations while preserving purity? A) Global variables B) The State monad C) Mutable arrays D. Exception throwing Answer: B Explanation: The State monad threads a state value through pure functions, encapsulating state changes. Question 52. Which of the following best illustrates “currying”? A) Converting a function of two arguments into a chain of single‑argument functions. B) Sorting a list in place. C) Using a mutable map to store intermediate results. D. Compiling code to bytecode. Answer: A Explanation: Currying transforms f(a,b) into g(a)(b), enabling partial application. Question 53. In Scala, which trait combines the capabilities of Functor and Applicative? A) Monad B) Traversable C. Seq D. Iterable
Answer: A Explanation: Monad extends Applicative, which in turn extends Functor, inheriting both map and ap. Question 54. Which of the following best describes the purpose of a “type class” in Haskell? A) To enforce runtime type checking. B) To define a set of functions that can operate over multiple types. C) To generate documentation automatically. D. To manage memory allocation. Answer: B Explanation: Type classes specify behavior (methods) that can be implemented by different types, enabling ad‑hoc polymorphism. Question 55. Which of the following is a correct statement about the foldLeft function on a list? A) It processes elements from right to left. B) It is tail‑recursive. C) It cannot be used with infinite lists. D. It modifies the original list. Answer: B Explanation: foldLeft is implemented as a tail‑recursive loop, accumulating results while traversing left‑to‑right. Question 56. In functional programming, what does “structural sharing” primarily help to achieve? A) Faster network communication.