COS 326 Functional Programming ExamSprint Handbook, Exams of Technology

This handbook introduces functional programming paradigms emphasizing immutability, higher-order functions, recursion, lambda calculus concepts, and type systems. It contrasts functional and imperative approaches while teaching how functional techniques improve reliability and modularity. The exam-focused format includes syntax examples, evaluation strategies, and conceptual exercises to reinforce understanding of declarative computation.

Typology: Exams

2025/2026

Available from 03/03/2026

shilpi-jain-2
shilpi-jain-2 🇮🇳

1

(1)

25K documents

1 / 86

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COS 326 Functional Programming
ExamSprint Handbook
Question 1. **In OCaml, which construct is used to bind a name to a value that cannot be
changed later?**
A) var
B) mutable
C) let
D) assign
Answer: C
Explanation: The `let` keyword creates an immutable binding; once bound, the value cannot be
reassigned.
Question 2. **What is the result of evaluating the OCaml expression `3 + 4 * 2`?**
A) 14
B) 11
C) 10
D) 7
Answer: B
Explanation: Multiplication has higher precedence, so `4 * 2 = 8`; then `3 + 8 = 11`.
Question 3. **Which of the following OCaml types represents a single Unicode character?**
A) int
B) char
C) string
D) byte
Answer: B
Explanation: The `char` type stores a single Unicode character; `string` stores a sequence of
characters.
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

Partial preview of the text

Download COS 326 Functional Programming ExamSprint Handbook and more Exams Technology in PDF only on Docsity!

ExamSprint Handbook

Question 1. In OCaml, which construct is used to bind a name to a value that cannot be changed later? A) var B) mutable C) let D) assign Answer: C Explanation: The let keyword creates an immutable binding; once bound, the value cannot be reassigned. Question 2. What is the result of evaluating the OCaml expression 3 + 4 * 2? A) 14 B) 11 C) 10 D) 7 Answer: B Explanation: Multiplication has higher precedence, so 4 * 2 = 8; then 3 + 8 = 11. Question 3. Which of the following OCaml types represents a single Unicode character? A) int B) char C) string D) byte Answer: B Explanation: The char type stores a single Unicode character; string stores a sequence of characters.

ExamSprint Handbook

Question 4. What does the expression let rec fact n = if n = 0 then 1 else n * fact (n-1) define? A) An iterative factorial function B) A tail‑recursive factorial function C) A non‑recursive factorial function D) A recursive factorial function that is not tail‑recursive Answer: D Explanation: The function calls itself after a multiplication, so the recursive call is not in tail position. Question 5. Which OCaml keyword introduces an anonymous function? A) function B) lambda C) fun D) proc Answer: C Explanation: fun creates a lambda expression, e.g., fun x - > x + 1. Question 6. Consider the type 'a option. Which value has type int option? A) Some 5 B) None C) Both A and B D) Neither A nor B Answer: C

ExamSprint Handbook

Explanation: The empty list literal [] matches only the empty list. Question 10. In a pattern match, what does the guard when x > 0 accomplish? A) It binds x to a value greater than zero B) It filters the matched case to only succeed when the condition holds C) It creates a new variant constructor D) It raises a runtime exception if the condition fails Answer: B Explanation: Guards are boolean expressions that must be true for the clause to be selected. Question 11. Which of the following expressions has type ('a - > 'b) - > 'a list - > 'b list? A) List.map B) List.fold_left C) List.filter D) List.concat Answer: A Explanation: List.map takes a function 'a - > 'b and a list of 'a, returning a list of 'b. Question 12. What is the effect of using List.fold_left versus List.fold_right on a list [1;2;3] with the function (+) and initial accumulator 0? A) Both produce 6 B) fold_left yields 6, fold_right yields 0 C) fold_left yields 0, fold_right yields 6 D) They produce different results only for non‑commutative functions Answer: A

ExamSprint Handbook

Explanation: For addition (associative and commutative), both folds give the same sum 6. Question 13. Which of the following definitions is tail‑recursive? A) let rec sum n = if n = 0 then 0 else n + sum (n-1) B) let rec sum n acc = if n = 0 then acc else sum (n-1) (acc + n) C) let rec sum n = List.fold_left (+) 0 (List.init n (fun i - > i+1)) D) let rec sum n = match n with 0 - > 0 | _ - > n + sum (n-1) Answer: B Explanation: The recursive call sum (n-1) (acc + n) is the last operation; thus the function is tail‑recursive. Question 14. What is the time complexity of List.rev implemented via a naïve recursive algorithm that appends each head to the reversed tail? A) O(n) B) O(n log n) C) O(n²) D) O(1) Answer: C Explanation: Each recursive step performs an @ (append) which is O(k); summing over n gives O(n²). Question 15. Which OCaml construct is used to hide the concrete implementation of a type from users of a module? A) open B) include C) Abstract type definition in the signature (.mli)

ExamSprint Handbook

C) compute_heavy () D) lazy (compute_heavy ()) Answer: B Explanation: A thunk is a parameterless function that delays computation; fun () - > … matches that. Question 19. What does the lazy keyword in OCaml produce? A) A mutable reference B) A value that is computed immediately C) A value of type 'a Lazy.t that is evaluated on first demand D) An exception handler Answer: C Explanation: lazy expr creates a lazy value; forcing it evaluates the expression once. Question 20. In OCaml, which operator is used to concatenate two lists? A) @ B) ^ C) ++ D) :: Answer: A Explanation: The @ operator concatenates two lists. Question 21. What is the most general type inferred for the function let id x = x? A) 'a - > 'a B) int - > int

ExamSprint Handbook

C) bool - > bool D) string - > string Answer: A Explanation: Since id returns exactly what it receives, the compiler infers a polymorphic type 'a - > 'a. Question 22. Which of the following is a correct definition of a binary tree ADT in OCaml? A) type 'a tree = Leaf of 'a | Node of 'a tree * 'a tree B) type 'a tree = Empty | Node of 'a * 'a tree * 'a tree C) type 'a tree = Node of 'a * 'a tree D) type 'a tree = Leaf of 'a tree Answer: B Explanation: A typical binary tree has an Empty case and a Node containing a value and two subtrees. Question 23. What does the pattern Some x match? A) Any integer B) An optional value that is present, binding its contents to x C) A list containing x D) A function that returns x Answer: B Explanation: Some x matches the Some constructor of the 'a option type and extracts the inner value. Question 24. Which of the following expressions will cause a compile‑time type error? A) let f (g:int->int) = g 3

ExamSprint Handbook

Question 27. Which of the following is true about OCaml’s ref type? A) It provides immutable storage B) It can be used to create mutable variables C) It is a compile‑time only construct D) It forces values to be evaluated lazily Answer: B Explanation: ref creates a mutable cell; its contents can be changed with :=. Question 28. What does the expression List.fold_left (fun acc x - > acc + x) 0 [1;2;3] evaluate to? A) 0 B) 1 C) 5 D) 6 Answer: D Explanation: The fold adds each element to the accumulator starting from 0, resulting in 6. Question 29. In OCaml, which of the following best describes parametric polymorphism? A) Using type variables that can be instantiated with any concrete type B) Overloading functions based on argument types C) Allowing runtime type casts D) Defining functions that only work on integers Answer: A Explanation: Parametric polymorphism uses type parameters like 'a to write generic code.

ExamSprint Handbook

Question 30. Which of these is a correct way to define a curried addition function that takes two integers? A) let add = fun x y - > x + y B) let add x y = x + y C) let add = fun x - > fun y - > x + y D) All of the above Answer: D Explanation: All three syntaxes produce a curried function int - > int - > int. Question 31. What is the result of evaluating List.map (fun x - > x * x) [1;2;3]? A) [1;4;9] B) [1;2;3] C) [2;4;6] D) [0;1;4] Answer: A Explanation: Each element is squared, producing [1;4;9]. Question 32. Which of the following expressions demonstrates partial application? A) let inc = (+) 1 B) let inc x = x + 1 C) let inc = fun x - > x + 1 D) All of the above Answer: A Explanation: (+ ) 1 fixes the first argument of (+), yielding a function that adds 1 to its argument.

ExamSprint Handbook

Question 36. Which of the following expressions will raise a Match_failure exception at runtime? A) match Some 3 with None - > 0 | Some x - > x B) match [] with [] - > 0 | _::t - > List.length t C) match [1;2] with [x] - > x | _ - > 0 D) match true with false - > 0 | true - > 1 Answer: C Explanation: The pattern [x] matches a singleton list; [1;2] does not, so the match fails and raises an exception. Question 37. In a module signature, what does the keyword val introduce? A) A type definition B) An exception definition C) A value (function or constant) that the module must provide D) A mutable reference Answer: C Explanation: val declares a value that the implementing structure must define. Question 38. What is the asymptotic space complexity of a naïve recursive implementation of the Fibonacci function fib n = fib (n-1) + fib (n-2)? A) O(1) B) O(log n) C) O(n) D) O(2ⁿ) Answer: D

ExamSprint Handbook

Explanation: The call tree grows exponentially; the maximum depth is n, but the total number of calls is O(2ⁿ), leading to exponential stack usage in the naïve version. Question 39. Which of the following best describes an algebraic data type (ADT) in OCaml? A) A type defined by an external library B) A type built from sum (variant) and product (record/tuple) constructors C) A mutable data structure D) A primitive integer type Answer: B Explanation: ADTs combine sum and product types to model complex data. Question 40. What does the expression List.filter (fun x - > x mod 2 = 0) [1;2;3;4] return? A) [1;3] B) [2;4] C) [1;2;3;4] D) [] Answer: B Explanation: It keeps only even numbers, yielding [2;4]. Question 41. Which of the following is true about OCaml’s let rec binding? A) It can only be used for functions, not values B) It allows the defined name to be used within its own definition C) It creates a mutable variable D) It forces the definition to be evaluated lazily Answer: B

ExamSprint Handbook

Answer: B Explanation: lazy creates a lazy value; the expression is not evaluated until forced. Question 45. Which of the following functions is guaranteed to be total (i.e., defined for all inputs) based on its type alone? A) fun x - > List.hd x B) fun x - > List.tl x C) fun x - > x D) fun x - > List.nth x 0 Answer: C Explanation: The identity function works for any value of any type; the others can raise exceptions on empty lists. Question 46. What does the [@@unboxed] attribute request from the compiler? A) To store a single‑field record without the wrapper B) To inline the function C) To make the value immutable D) To enable tail‑call optimization Answer: A Explanation: [@@unboxed] asks the compiler to represent a one‑field record as the field itself. Question 47. In OCaml, which of the following patterns matches any value but does not bind it? A) _ B) x C) ?

ExamSprint Handbook

D) *

Answer: A Explanation: The underscore _ is a wildcard pattern that matches anything without binding. Question 48. What is the result of List.fold_right (^) ["a";"b";"c"] ""? A) "abc" B) "cba" C) "" D) "ab" Answer: A Explanation: fold_right concatenates from the rightmost element, producing "a" ^ ("b" ^ ("c" ^ "")) = "abc". Question 49. Which of the following statements about OCaml’s garbage collector is correct? A) It only collects cyclic data structures B) It uses a generational, stop‑the‑world approach C) It requires manual invocation by the programmer D) It cannot reclaim memory used by immutable values Answer: B Explanation: OCaml’s GC is generational and pauses the program briefly to collect. Question 50. What does the expression let open List in map (fun x - > x*2) [1;2] do? A) Opens the List module locally and applies map to double each element B) Raises a syntax error

ExamSprint Handbook

B) It reuses the current stack frame for tail‑recursive calls, preventing stack growth C) It converts loops into recursion D) It caches function results automatically Answer: B Explanation: TCO allows tail‑recursive functions to run in constant stack space. Question 54. What is the type of the expression fun f - > fun x - > f (f x)? A) ('a - > 'a) - > 'a - > 'a B) ('a - > 'b) - > 'a - > 'b C) ('a - > 'b) - > ('b - > 'c) - > 'a - > 'c D) ('a - > 'a) - > ('a - > 'a) - > 'a - > 'a Answer: A Explanation: The inner f is applied twice to x; therefore f must map 'a to 'a, yielding type ('a - > 'a) - > 'a - > 'a. Question 55. Which of the following is a correct way to raise a custom exception MyError? A) raise MyError B) raise (MyError) C) raise (MyError ()) if defined as exception MyError of unit D) All of the above, depending on how MyError is declared Answer: D Explanation: The exact syntax depends on whether MyError carries a payload. Question 56. What does the [@@inline never] attribute enforce?

ExamSprint Handbook

A) The compiler must never inline the function B) The function must always be inlined C) The function is lazily evaluated D) The function is tail‑recursive Answer: A Explanation: [@@inline never] tells the compiler not to inline the annotated function. Question 57. Which of the following expressions creates an infinite stream of natural numbers using Seq.unfold? A) Seq.unfold (fun n - > Some (n, n+1)) 0 B) Seq.unfold (fun n - > None) 0 C) Seq.unfold (fun n - > Some (n, n-1)) 0 D) Seq.unfold (fun _ - > Some (0, 0)) () Answer: A Explanation: Starting from 0, each step returns the current number and the next state n+1. Question 58. What is the result of List.mem 3 [1;2;3;4]? A) false B) true C) 3 D) Raises an exception Answer: B Explanation: List.mem checks for membership; 3 is present, so it returns true.