🔀
Mixed set 2: ten questions from all phases in random order, as in the exam. Aim for 22 minutes. Every answer links back to the lesson to re-read.
Practice set
Q1MCQ1 markBasics
Which statement is true?
- (A) An interpreter produces a target program that is run later.
- (B) A compiler translates the whole program before it runs.
- (C) A compiler executes the program statement by statement.
- (D) An interpreter never reports errors.
Q2NAT2 marksTokens
How many tokens are in printf("sum=%d", a+++b);?
Numerical answer: ________
Q3MCQ1 markFOLLOW
For S → a S b | ε, FOLLOW(S) is:
- (A) { b }
- (B) { $ }
- (C) { b, $ }
- (D) { a, b, $ }
Q4NAT2 marksCLR
How many states does the canonical LR(1) collection for S → A A, A → a A | b have (including I0)?
Numerical answer: ________
Q5NAT2 marksSDD
E → E # T {E.val = E.val × T.val} | T; T → T & F {T.val = T.val + F.val} | F; F → n. Value of 2 & 3 # 4 & 1 # 5?
Numerical answer: ________
Q6NAT1 markTAC
With DAG-based generation (common subexpressions shared), how many temporaries does (a + b) * (a + b) + c need?
Numerical answer: ________
Q7NAT2 marksArrays
int A[5][8] stored row-major from address 100, 4 bytes per element, indices from 0. Address of A[3][6]?
Numerical answer: ________
Q8MCQ2 marksParameters
What does this print with call by reference?
int a = 2, b = 3;
void swapadd(int x, int y) { x = x + y; y = x - y; a = a * 10; }
main() { swapadd(a, b); print(a, b); }
- (A) 20 3
- (B) 50 2
- (C) 5 2
- (D) 20 2
Q9NAT1 markStack
For f(n) = 1 if n ≤ 2 else f(n − 1) + f(n − 3), called as f(6) from main: maximum number of activation records of f on the stack at the same time?
Numerical answer: ________
Q10MCQ2 marksAvailable
B1: u = a + b, v = c * d → B2: w = a + b, c = 1 and B3: x = c * d → both to B4: y = a + b, z = c * d. Which expressions are available at the start of B4?
- (A) { a + b, c * d }
- (B) { a + b }
- (C) { c * d }
- (D) ∅
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: (B)1 mark
That is the definition from Lesson 0.1.1.
- (A) is wrong: That describes a compiler.
- (C) is wrong: That describes an interpreter.
- (D) is wrong: It reports them when reached.
Re-read: Lesson 0.1.1 — Programs, machine code and why we translate; compiler vs interpreter
Q2 Answer: 102 marks
printf ( "sum=%d" , a ++ + b ) ; → 10 (the lexer takes ++ first by longest match).
Re-read: Lesson 1.1.2 — Counting tokens in C code: whitespace, comments, string literals, ++ and >=
Q3 Answer: (C)1 mark
S is the start (so $) and appears before b (computed).
- (A) is wrong: Misses $.
- (B) is wrong: Misses b.
- (D) is wrong: a never follows S.
Re-read: Lesson 2.3.2 — FIRST and FOLLOW sets
Q4 Answer: 102 marks
Computed: 10 (versus 7 for LR(0)/LALR).
Re-read: Lesson 2.4.4 — Canonical LR(1) (CLR) items with lookaheads
Q5 Answer: 1252 marks
& binds tighter: (2 + 3) × (4 + 1) × 5 = 125 (computed on the parse tree).
Re-read: Lesson 3.1 — Syntax-directed definitions: attributes, semantic rules, synthesized vs inherited, annotated parse trees, dependency graphs and evaluation order
Q6 Answer: 31 mark
t1 = a + b; t2 = t1 * t1; t3 = t2 + c: 3 (computed; 4 without sharing).
Re-read: Lesson 4.1 — Why an intermediate representation; syntax trees vs DAGs; counting nodes in a DAG
Q7 Answer: 2202 marks
100 + (3 × 8 + 6) × 4 = 220.
Re-read: Lesson 4.2.2 — TAC for expressions and array accesses (address calculation)
Q8 Answer: (B)2 marks
x aliases a: a = 5; y aliases b: b = 5 − 3 = 2; a = 5 × 10 = 50 (computed: 50 2).
- (A) is wrong: That is call by value.
- (C) is wrong: That is value-result.
- (D) is wrong: a was 5 when multiplied.
Re-read: Lesson 5.4 — Parameter passing: by value, by reference, by value-result, by name; output tracing
Q9 Answer: 51 mark
The chain f(6) → f(5) → f(4) → f(3) → f(2): 5 (computed).
Re-read: Lesson 5.2 — Activation records, activation trees, the call stack; caller vs callee duties; recursion
Q10 Answer: (B)2 marks
c changes in B2, killing c * d on that path (computed: { a + b }).
- (A) is wrong: c * d is killed on the B2 path.
- (C) is wrong: a + b survives both paths.
- (D) is wrong: a + b is available.
Re-read: Lesson 6.5 — Global common-subexpression elimination via available expressions