MCQ drill bank
50 questions: 21 Poole–Mackworth (ch 3–5), 29 DPV and data structures. Concepts behind them are on topic 5.
PM ch 3: search
1. Which search strategy selects from the frontier the path that was added first, and therefore finds a path with the fewest arcs?
- depth-first search
- lowest-cost-first search
- greedy best-first search
- breadth-first search
Answer
(D) BFS uses a FIFO queue, so paths come off in order of number of arcs.2. A heuristic function h is admissible if, for every node n,
- h(n) is at most the cost of the cheapest path from n to a goal
- h(n) is at least the cost of the cheapest path from n to a goal
- h(n) = 0 only at goal nodes
- h(n) can be computed in constant time
Answer
(A) Admissible means it never overestimates the true remaining cost.3. You need a search that is guaranteed to find a fewest-arc path but uses memory only linear in the path length. Which one?
- breadth-first search
- depth-first search
- A* search
- iterative deepening
Answer
(D) Iterative deepening runs depth-bounded DFS with bounds 1, 2, 3, …: DFS memory, BFS guarantee. BFS needs exponential memory; plain DFS has no guarantee.4. A* search with h(n) = 0 for every node behaves exactly like
- lowest-cost-first search
- depth-first search
- breadth-first search
- greedy best-first search
Answer
(A) A* orders by cost(p) + h(p) = cost(p), which is lowest-cost-first.5. A* is run with multiple-path pruning (an explored set: a path to an already-reached node is discarded). Which condition on h guarantees it still returns a least-cost path?
- h overestimates by at most 1
- h is any non-negative function
- no condition is needed
- h is consistent: \(h(n) \le \text{cost}(n, n') + h(n')\)
Answer
(D) PM Prop. 3.2: with a consistent heuristic, multiple-path pruning never prevents A* from finding an optimal solution.6. Depth-first branch and bound has already found a solution of cost 10. The current path p has cost(p) = 6 and h(p) = 5, with h admissible. What does it do with p?
- Prune p, since cost(p) + h(p) = 11 ≥ 10, so no path through p can beat 10
- Expand p, since 6 < 10
- Record p as the new best solution
- Restart the search with bound 11
Answer
(A) An admissible h makes 11 a lower bound on any solution through p, which already exceeds the bound 10.PM ch 4: constraints
7. A CSP has 6 variables, each with domain {0, 1, 2}. In the worst case, how many possible worlds does generate-and-test check?
- 18
- 216
- 729
- 64
Answer
(C) \(3^6 = 729\). (216 = \(6^3\) swaps base and exponent; 18 adds instead of multiplying.)8. dom(A) = dom(B) = {1, 2, 3, 4} and the only constraint is \(A \le B - 2\). After arc consistency, the domains are
- A = {1, 2, 3}, B = {2, 3, 4}
- A = {1, 2}, B = {1, 2, 3, 4}
- unchanged
- A = {1, 2}, B = {3, 4}
Answer
(D) A = 3, 4 need B ≥ 5 (none); B = 1, 2 need A ≤ 0 (none). Both arcs get pruned.9. Which of these constraints is binary?
- X ≠ Y
- X > 5
- X + Y = Z
- alldifferent(Q1, …, Q8)
Answer
(A) Scope of size 2. (B) is unary, (C) ternary, (D) 8-ary.10. Variables X, Y, Z each have domain {red, green}, with constraints X ≠ Y, Y ≠ Z, X ≠ Z. GAC deletes nothing. What is true?
- The CSP has exactly three solutions
- The CSP has a solution, because every domain is non-empty after GAC
- GAC was run incorrectly
- The CSP has no solution, but GAC can't detect it because each arc on its own is consistent
Answer
(D) Three mutually different values from two colours is impossible; arc consistency checks one constraint at a time, so splitting is needed to see it.11. During backtracking, after choosing a variable, which value ordering is recommended for finding a first solution quickly?
- The least constraining value: the one ruling out the fewest values in neighbours' domains
- The value that appears in the most constraints
- The value that rules out the most values in neighbours' domains
- Always the smallest value
Answer
(A) Values: "succeed first" (least constraining). Variables: "fail first" (most constrained).12. Simulated annealing accepts a move that worsens h by Δ with probability \(e^{-\Delta/T}\). As the temperature T approaches 0, the algorithm behaves like
- a random walk
- random restart
- breadth-first search
- greedy descent: only improving (or equal) moves are taken
Answer
(D) \(e^{-\Delta/T} \to 0\) for Δ > 0, so worse moves are essentially never accepted. High T is the random-walk end.13. A local-search algorithm solves 50% of runs within k steps (independently each run) and almost never later. You restart it every k steps. What is the probability it has solved the problem within 3k steps?
- 50%
- 75%
- 87.5%
- 100%
Answer
(C) Three independent tries fail with probability \(0.5^3 = 0.125\), so success is \(1 - 0.125 = 0.875\).PM ch 5: propositions and inference
14. Over the atoms p, q, r, how many models does \(p \wedge (q \vee r)\) have?
- 1
- 3
- 4
- 7
Answer
(B) p must be true (4 rows); q ∨ r is true in 3 of those 4.15. Which proposition is valid (true in every interpretation)?
- \((p \to q) \vee (q \to p)\)
- \(p \to q\)
- \(p \wedge \neg p\)
- \(p \vee q\)
Answer
(A) If q is T then p → q is T; if q is F then q → p is T (F → anything is T). (B), (D) are only satisfiable; (C) is unsatisfiable.16. Which entailment holds?
- {p → q, q} ⊨ p
- {p ∨ q} ⊨ p
- {p → q} ⊨ q → p
- {p → q, ¬q} ⊨ ¬p
Answer
(D) The only model of the KB has q = F and so p = F (modus tollens). (A) is affirming the consequent: p = F, q = T is a counterexample.17. KB: p ← q ∧ r. q ← s. r ← q. s. t ← u. u ← t. Which set does bottom-up (forward chaining) derive?
- {s, q, r, p}
- {s}
- {s, q, r, p, t, u}
- {p, q, r, s, t}
Answer
(A) s, then q, then r, then p. t and u each need the other first, so neither fires: they are false in the minimal model.18. Clauses: \((\neg a \vee b)\), \((\neg b \vee \neg c)\), \((c \vee d)\). DPLL sets a = true. What does unit propagation derive?
- b = true only
- b = true, c = true, d = false
- b = true, c = false, d = true
- nothing; it must split again
Answer
(C) First clause becomes unit: b. Then the second forces ¬c. Then the third forces d.19. You want to use a SAT solver (such as DPLL) to decide whether KB ⊨ g. You should check whether
- KB ∪ {g} is satisfiable
- g is valid
- KB is unsatisfiable
- KB ∪ {¬g} is unsatisfiable
Answer
(D) KB ⊨ g iff there is no model of KB where g is false, i.e. KB ∪ {¬g} has no model.20. Which of these is a Horn clause but not a definite clause?
- false ← a ∧ b
- a ← b ∧ c
- a.
- a ∨ b ← c
Answer
(A) An integrity constraint has no atom in the head. Horn = definite clauses + integrity constraints. (D) has two head atoms, so it is not Horn.21. The minimal conflicts are {ok_a, ok_b} and {ok_b, ok_c}. Which is a minimal diagnosis?
- {ok_a}
- {ok_a, ok_b}
- {ok_c}
- {ok_b}
Answer
(D) ok_b hits both conflicts. (A) and (C) miss one conflict; (B) hits both but contains {ok_b}, so it is not minimal. (The other minimal diagnosis is {ok_a, ok_c}.)DFS and graph decomposition (DPV ch 3)
22. After DFS on a directed graph, for which type of edge (u, v) is post(u) < post(v)?
- tree edge
- back edge
- forward edge
- cross edge
Answer
(B) v is an ancestor of u, so v finishes after u. Tree, forward and cross edges all have post(u) > post(v).23. After DFS on a DAG, the vertex with the highest post number is guaranteed to be
- a source
- a sink
- the vertex with the highest pre number
- on every path
Answer
(A) In a DAG every edge (w, v) has post(w) > post(v), so nothing can point into the max-post vertex.24. DFS on an undirected graph classifies every edge as
- tree or cross
- tree, back, forward or cross
- tree only
- tree or back
Answer
(D) Whichever endpoint is explored first reaches the other before finishing, so one is an ancestor of the other: no cross edges.25. DFS on a graph with V vertices stored as an adjacency matrix takes
- \(O(V + E)\)
- \(O(V^2)\)
- \(O(E \log V)\)
- \(O(VE)\)
Answer
(B) Listing one vertex's neighbours scans a whole row, O(V), for each of V vertices.Shortest paths (DPV ch 4)
26. Dijkstra's algorithm is BFS with the FIFO queue replaced by
- a stack
- a hash table
- a priority queue keyed by tentative distance
- a list of edges sorted by length
Answer
(C) deletemin returns the closest unfinalized vertex ("alarm clocks").27. Dijkstra implemented with an unsorted array as the priority queue runs in
- \(O(V + E)\)
- \(O((V + E)\log V)\)
- \(O(V^2)\)
- \(O(VE)\)
Answer
(C) V deletemins at O(V) each plus E decreasekeys at O(1) each: \(O(V^2 + E) = O(V^2)\).28. All edge lengths are positive. You multiply every edge length by 3. What happens to the shortest paths?
- They change, favouring paths with fewer edges
- They stay the same: every path's length triples, so the order of paths is preserved
- They change, favouring paths with more edges
- Dijkstra no longer applies
Answer
(B) Scaling preserves comparisons. Contrast: adding a constant penalizes paths with more edges (DPV 4.8).29. The running time of Bellman–Ford is
- \(O(V + E)\)
- \(O((V + E)\log V)\)
- \(O(VE)\)
- \(O(V^2 E)\)
Answer
(C) V − 1 rounds, each updating all E edges.30. A weighted DAG has some negative edges. Which single-source shortest-path method is correct and fastest?
- Dijkstra
- Bellman–Ford, \(O(VE)\)
- BFS
- Linearize by DFS, then one pass of updates in topological order, \(O(V + E)\)
Answer
(D) Every path's edges get updated in order; negative edges are fine, negative cycles can't exist. (B) is correct but slower; (A) can be wrong.31. To detect currency arbitrage, where r(u, v) is the exchange rate from u to v, you should
- set \(\ell(u,v) = -\log r(u,v)\) and look for a negative cycle with Bellman–Ford
- set \(\ell(u,v) = r(u,v)\) and run Dijkstra
- set \(\ell(u,v) = 1/r(u,v)\) and run BFS
- set \(\ell(u,v) = \log r(u,v)\) and run Dijkstra
Answer
(A) A product of rates > 1 becomes a sum of \(-\log r\) < 0. Rates above 1 give negative edges, so not Dijkstra.Heaps and priority queues
32. A binary heap is stored in an array at positions 1, …, n. The children of the node at position i are at
- i + 1 and i + 2
- 2i − 1 and 2i
- 2i and 2i + 1
- \(\lfloor i/2 \rfloor\)
Answer
(C) (D) is the parent.33. Building a binary min-heap from n given keys (Build-Min-Heap, sifting down from the bottom) takes
- O(1)
- O(log n)
- Θ(n)
- Θ(n log n)
Answer
(C) Most nodes are near the bottom and sift down only a few levels. (n separate inserts would be O(n log n); heap sort is O(n log n) because of the n deletemins.)Hash tables
34. With the division method \(h(k) = k \bmod m\), why should m not be a power of 2?
- Division by a power of 2 is slow
- The table would be too large
- It makes open addressing loop forever
- h(k) is then just the lowest bits of k, so keys that share their low bits all collide
Answer
(D) Good choice: a prime not too close to a power of 2.35. Multiplication method \(h(k) = \lfloor m\,(kA \bmod 1) \rfloor\) with m = 16, A = 3/8, k = 11. What is h(k)?
- 1
- 2
- 4
- 6
Answer
(B) kA = 33/8 = 4.125; fractional part 0.125; 16 × 0.125 = 2; floor 2.36. Open addressing with linear probing, m = 7, \(h(k, i) = (k \bmod 7 + i) \bmod 7\), table initially empty. Insert 10, 17, 24 in that order. Which slot does 24 land in?
- 3
- 4
- 5
- 6
Answer
(C) All three hash to 3. 10 → 3; 17 → 3 taken, 4; 24 → 3, 4 taken, 5. A growing run like this is primary clustering.37. A finite family \(\mathcal{H}\) of hash functions into {0, …, m − 1} is universal if, for every pair of distinct keys k ≠ l,
- at most \(|\mathcal{H}|/m\) functions in \(\mathcal{H}\) map k and l to the same slot
- no function in \(\mathcal{H}\) maps k and l to the same slot
- exactly half of the functions collide them
- \(\mathcal{H}\) contains exactly m functions
Answer
(A) So a randomly chosen h collides k and l with probability at most 1/m, whatever keys an adversary picks.Union-find and ADTs
38. With union by rank (no path compression), a find on a structure with n elements takes
- O(1)
- O(log n)
- O(n)
- \(O(\log^* n)\)
Answer
(B) A root of rank k has at least \(2^k\) nodes, so rank (= height) ≤ log n. \(\log^* n\) needs path compression too (amortized).39. Kruskal's algorithm runs on a connected graph with n vertices and m edges. How many successful union operations does it perform?
- m
- n
- m − n + 1
- n − 1
Answer
(D) One union per edge added to the tree, and a spanning tree has n − 1 edges. (m − n + 1 is the number of rejected edges.)40. A sorted set (SSet) holds {3, 8, 15, 20}. What does find(10) return?
- null
- 15
- 8
- 10
Answer
(B) SSet find is successor search: the smallest y ≥ x.41. In terms of deque operations, a FIFO queue uses
- add-last and remove-last
- add-first and remove-first
- add-last and remove-first
- remove-first only
Answer
(C) (A) and (B) both give a stack (LIFO).Greedy algorithms (DPV ch 5)
42. Fractional knapsack with capacity W = 5. Items (weight, value): (2, 10), (3, 9), (4, 16). What is the maximum total value?
- 19
- 21
- 22
- 25
Answer
(C) Values per unit are 5, 3, 4. Take all of item 1 (value 10, 3 units left), then 3/4 of item 3 (value 12): 22.43. Huffman coding on a text of 8 characters with frequencies a: 4, b: 2, c: 1, d: 1. How many bits does the encoded text take?
- 12
- 14
- 16
- 18
Answer
(B) Merge c+d = 2, then 2+b = 4, then 4+a = 8. Depths a 1, b 2, c 3, d 3: 4 + 4 + 3 + 3 = 14 (= 2 + 4 + 8, the internal nodes). A fixed 2-bit code needs 16.44. Horn formula: \((\Rightarrow x)\), \((x \Rightarrow y)\), \((y \wedge z \Rightarrow w)\), \((\bar x \vee \bar w)\). What does the greedy Horn-satisfiability algorithm return?
- all variables true
- x = T, y = T, z = F, w = F
- "not satisfiable"
- x = T, y = F, z = F, w = F
Answer
(B) Start all false. Forced: x, then y. z stays false, so w is never forced. Negative clause \(\bar x \vee \bar w\) holds since w = F.45. Set cover on n elements has an optimal cover with k sets. The greedy algorithm (always take the set covering the most uncovered elements) uses at most
- k sets
- 2k sets
- n/k sets
- \(k \ln n\) sets
Answer
(D) Uncovered count shrinks by a factor \((1 - 1/k)\) each step, so after \(k \ln n\) steps fewer than 1 remains.46. A connected undirected graph has distinct edge weights. Edge e is the heaviest edge on some cycle. Then
- e is in no MST
- e is in every MST
- e is in the MST only if it is the lightest edge at one of its endpoints
- nothing can be concluded
Answer
(A) Cycle property: if an MST used e, swapping e for a lighter edge of that cycle crossing the same cut gives a lighter spanning tree.Dynamic programming (DPV ch 6)
47. What is the length of the longest increasing subsequence of 3, 1, 4, 1, 5, 9, 2, 6?
- 3
- 4
- 5
- 6
Answer
(B) e.g. 3, 4, 5, 9 or 1, 4, 5, 6. L values: 1, 1, 2, 1, 3, 4, 2, 4; the max is 4.48. Matrices A (10 × 20), B (20 × 5), C (5 × 30). What is the minimum number of scalar multiplications to compute ABC?
- 1,500
- 2,500
- 6,000
- 9,000
Answer
(B) (AB)C: 10·20·5 + 10·5·30 = 1,000 + 1,500 = 2,500. A(BC): 20·5·30 + 10·20·30 = 3,000 + 6,000 = 9,000.49. Weighted interval scheduling with intervals sorted by finish time; \(p(s_j)\) is the index of the first interval finishing after \(s_j\). Which recurrence is correct?
- \(\text{OPT}(j) = w_j + \text{OPT}(j-1)\)
- \(\text{OPT}(j) = \max\{\text{OPT}(j-1),\ w_j + \text{OPT}(j-1)\}\)
- \(\text{OPT}(j) = \max\{\text{OPT}(j-1),\ w_j + \text{OPT}(p(s_j) - 1)\}\)
- \(\text{OPT}(j) = \max\{\text{OPT}(j-1),\ w_j + \text{OPT}(p(s_j))\}\)
Answer
(C) Skip interval j, or take it and keep only intervals before \(p(s_j)\). (D) is off by one: interval \(p(s_j)\) itself overlaps j.50. With unit costs for insert, delete and substitute, what is the edit distance between abc and yabd?
- 1
- 2
- 3
- 4