🔀
Mixed set 1: ten questions from all phases in random order, as in the exam. Aim for 25 minutes. Every answer links back to the lesson to re-read.
Practice set
Q1MCQ1 markPhases
The statement x = 5 +; is rejected by which phase?
- (A) lexical analysis
- (B) syntax analysis
- (C) semantic analysis
- (D) code optimization
Q2NAT1 markTokens
How many tokens are in int x = 10; /* y */ x += 2;?
Numerical answer: ________
Q3MCQ2 marksFOLLOW
For S → A a | b B, A → c | ε, B → d B | ε, FOLLOW(B) is:
- (A) { d }
- (B) { $ }
- (C) { a, $ }
- (D) { d, $ }
Q4MCQ2 marksLR
For S → L = R | R, L → * R | id, R → L, which is true?
- (A) It is LR(0).
- (B) It is SLR(1) but not LALR(1).
- (C) It is LALR(1) but not SLR(1).
- (D) It is not LR(1).
Q5MCQ2 marksSDT
Translation scheme: E → E + T {print("+")} | T; T → T * F {print("*")} | F; F → id {print(id.name)}. Output for a * b + c?
- (A) a b c * +
- (B) a b * c +
- (C) + * a b c
- (D) a * b + c
Q6NAT1 markDAG
How many nodes does the DAG of a + a * (b − c) + (b − c) * d have?
Numerical answer: ________
Q7NAT2 marksBlocks
The following code has how many basic blocks?
(1) i = 1
(2) j = 1
(3) t1 = 10 * i
(4) t2 = t1 + j
(5) t3 = 8 * t2
(6) t4 = t3 - 88
(7) a[t4] = 0
(8) j = j + 1
(9) if j <= 10 goto (3)
(10) i = i + 1
(11) if i <= 10 goto (2)
(12) i = 1
(13) t5 = i - 1
(14) t6 = 88 * t5
(15) a[t6] = 1
(16) i = i + 1
(17) if i <= 10 goto (13)
Numerical answer: ________
Q8MCQ2 marksScoping
What does this print under dynamic scoping?
int x = 5;
void f() { print(x); }
void g() { int x = 10; f(); x = x + 1; print(x); }
main() { g(); f(); }
- (A) 5 11 5
- (B) 10 11 5
- (C) 10 11 10
- (D) 5 11 10
Q9NAT1 markRegisters
Straight-line code a = x + y; b = a * x; c = b + y; d = c * a, only d needed after it. Largest number of live variables at one point?
Numerical answer: ________
Q10MCQ2 marksConstants
B1: a = 3, b = 6 → B2: c = a * 2, b = b − a and B3: c = b, a = 3 → both to B4: d = c + a, e = b + 1. At the end of B4:
- (A) d = 9, e = 4
- (B) d = 9, e = NAC
- (C) d = NAC, e = NAC
- (D) d = 9, e = 7
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: (B)1 mark
Every token is valid, but the sequence does not match any grammar rule.
- (A) is wrong: x, =, 5, +, ; are all valid tokens.
- (C) is wrong: Meaning is checked only on a valid structure.
- (D) is wrong: Never reached.
Re-read: Lesson 0.1.3 — The phases of a compiler; symbol table; error handler; which phase catches which error
Q2 Answer: 91 mark
int, x, =, 10, ;, x, +=, 2, ; — the comment produces none: 9 (computed).
Re-read: Lesson 1.1.2 — Counting tokens in C code: whitespace, comments, string literals, ++ and >=
Q3 Answer: (B)2 marks
B appears only at the end of S → b B and B → d B, so FOLLOW(B) = FOLLOW(S) = { $ } (computed).
- (A) is wrong: d is in FIRST(B), not FOLLOW.
- (C) is wrong: a follows A, not B.
- (D) is wrong: d is not after B.
Re-read: Lesson 2.3.2 — FIRST and FOLLOW sets
Q4 Answer: (C)2 marks
SLR puts reduce R → L under FOLLOW(R) ∋ =, clashing with shift = (computed: one S/R conflict); LALR lookaheads avoid it.
- (A) is wrong: LR(0) has the same conflict.
- (B) is wrong: Reversed.
- (D) is wrong: CLR and LALR accept it.
Re-read: Lesson 2.4.5 — LALR(1): merging states and which conflicts merging can create
Q5 Answer: (B)2 marks
Actions at the end of each production print postfix: a b * c + (computed).
- (A) is wrong: That is a * (b + c) order.
- (C) is wrong: Prefix.
- (D) is wrong: Infix.
Re-read: Lesson 3.3 — Translation schemes with embedded actions; tracing what an SDT prints or computes
Q6 Answer: 91 mark
Leaves a, b, c, d; operators b − c (shared), a * (b − c), +, (b − c) * d, +: 9 (computed).
Re-read: Lesson 4.1 — Why an intermediate representation; syntax trees vs DAGs; counting nodes in a DAG
Q7 Answer: 62 marks
Leaders 1, 2, 3, 10, 12, 13: 6 (computed).
Re-read: Lesson 4.4 — Basic blocks: leader rules and partitioning; control flow graphs (the second meaning of “CFG”); counting blocks and edges
Q8 Answer: (B)2 marks
Called from g, f sees g’s x = 10; called from main, it sees the global 5. Computed: 10 11 5.
- (A) is wrong: That is static scoping.
- (C) is wrong: The second call of f is from main.
- (D) is wrong: Mixed up.
Re-read: Lesson 5.3 — Static vs dynamic scoping; tracing program output under each; access links and displays (brief)
Q9 Answer: 31 mark
Live sets before each statement (computed): { x, y }, { a, x, y }, { a, b, y }, { a, c }. Maximum 3.
Re-read: Lesson 6.4 — Liveness analysis: live variables at each point; what liveness is used for
Q10 Answer: (B)2 marks
c = 6 on both paths, a = 3: d = 9. b is 3 on one path and 6 on the other: NAC, so e is NAC (computed).
- (A) is wrong: b is not constant.
- (C) is wrong: c and a are constants.
- (D) is wrong: b is not 6 on every path.
Re-read: Lesson 6.3 — Constant propagation