⏱️
15 questions · 23 marks · 45 minutes. Negative marking on MCQ only: −1/3 mark for a wrong 1-mark MCQ, −2/3 for a wrong 2-mark MCQ. No negative marking for MSQ or NAT.
Q1MCQ1 markAreas
Local variables of a recursive function live in:
- (A) the static area
- (B) the stack
- (C) the heap
- (D) the code area
Q2MSQ1 markAreas
Which are stored in the static area?
- (A) global variables
- (B) string literals
- (C) malloc’d blocks
- (D) static locals
Q3NAT2 marksCalls
How many calls does fib(4) make, with int fib(int n) { if (n < 2) return n; return fib(n − 1) + fib(n − 2); }?
Numerical answer: ________
Q4NAT2 marksCalls
How many calls does t(3) make, with int t(int n) { if (n <= 0) return 1; return t(n − 1) + t(n − 1) + t(n − 2); }?
Numerical answer: ________
Q5MCQ1 markLinks
The access link of an activation record points to:
- (A) the caller’s record
- (B) the record of the lexically enclosing procedure
- (C) the heap
- (D) the return address
Q6MCQ2 marksScoping
Output under static scoping?
int x = 1;
void show() { print(x); }
void change() { x = 2; }
void test() { int x = 3; change(); show(); }
main() { test(); show(); }
- (A) 2 2
- (B) 2 1
- (C) 3 1
- (D) 1 1
Q7MCQ2 marksScoping
Same program under dynamic scoping:
- (A) 2 2
- (B) 2 1
- (C) 3 1
- (D) 3 3
Q8NAT2 marksScoping
Under dynamic scoping, what is the second number printed?
int n = 5;
int f(int k) { return k * n; }
int g() { int n = 2; return f(3) + n; }
main() { int n = 10; print(g(), f(1)); }
Numerical answer: ________
Q9NAT1 markAccess links
A procedure at depth 5 reads a variable declared at depth 2. Access links followed?
Numerical answer: ________
Q10MCQ2 marksParameters
For P1 (p(i, a[i]), i = 0, a = {10, 20, 30}, body x = x + 1; y = y + 1; i = i + 1), call by name prints:
- (A) 1 10 20 30
- (B) 2 11 20 30
- (C) 1 11 20 30
- (D) 2 10 21 30
Q11NAT2 marksParameters
For int a = 1; void q(int x, int y) { x = x + 10; y = y * 2; } and q(a, a), what is a by reference?
Numerical answer: ________
Q12MCQ1 markParameters
Copy-restore is another name for:
- (A) call by value
- (B) call by reference
- (C) call by value-result
- (D) call by name
Q13MCQ1 markAllocation
Which feature makes static allocation impossible?
- (A) global constants
- (B) recursion
- (C) fixed-size arrays
- (D) integer variables
Q14MCQ1 markDangling
Which is a dangling reference?
- (A) a pointer to a freed heap block
- (B) a global variable
- (C) a heap block nobody points to
- (D) a static local
Q15MSQ2 marksRecords
Which fields are typically in an activation record?
- (A) control link
- (B) return address
- (C) the code of the procedure
- (D) local variables
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: (B)1 mark
One copy per activation record.
- (A) is wrong: One copy only.
- (C) is wrong: Not requested at run time.
- (D) is wrong: No.
Re-read: Lesson 5.1 — Storage organization: code, static, stack, heap
Q2 Answer: (A) (B) (D)1 mark
malloc uses the heap.
Re-read: Lesson 5.1 — Storage organization: code, static, stack, heap
Q3 Answer: 92 marks
9.
Re-read: Lesson 5.2 — Activation records, activation trees, the call stack; caller vs callee duties; recursion
Q4 Answer: 252 marks
c(n) = 1 + 2c(n − 1) + c(n − 2), c(0) = c(−1) = 1: 4, 10, 25.
Re-read: Lesson 5.2 — Activation records, activation trees, the call stack; caller vs callee duties; recursion
Q5 Answer: (B)1 mark
Static nesting.
- (A) is wrong: Control link.
- (C) is wrong: No.
- (D) is wrong: Saved status.
Re-read: Lesson 5.3 — Static vs dynamic scoping; tracing program output under each; access links and displays (brief)
Q6 Answer: (A)2 marks
change updates the global x; show prints the global x twice.
- (B) is wrong: Dynamic.
- (C) is wrong: show never prints test’s x statically.
- (D) is wrong: change sets x to 2.
Re-read: Lesson 5.3 — Static vs dynamic scoping; tracing program output under each; access links and displays (brief)
Q7 Answer: (B)2 marks
change updates test’s x; main’s show sees the global 1.
- (A) is wrong: Static.
- (C) is wrong: test’s x became 2.
- (D) is wrong: No.
Re-read: Lesson 5.3 — Static vs dynamic scoping; tracing program output under each; access links and displays (brief)
Q8 Answer: 102 marks
f(1) called from main uses main’s n = 10: 10.
Re-read: Lesson 5.3 — Static vs dynamic scoping; tracing program output under each; access links and displays (brief)
Q9 Answer: 31 mark
5 − 2 = 3.
Re-read: Lesson 5.3 — Static vs dynamic scoping; tracing program output under each; access links and displays (brief)
Q10 Answer: (D)2 marks
Computed: 2 10 21 30.
- (A) is wrong: Value.
- (B) is wrong: Reference.
- (C) is wrong: Value-result.
Re-read: Lesson 5.4 — Parameter passing: by value, by reference, by value-result, by name; output tracing
Q11 Answer: 222 marks
22.
Re-read: Lesson 5.4 — Parameter passing: by value, by reference, by value-result, by name; output tracing
Q12 Answer: (C)1 mark
Copy in, restore on return.
- (A) is wrong: No restore.
- (B) is wrong: No copies.
- (D) is wrong: Re-evaluation.
Re-read: Lesson 5.4 — Parameter passing: by value, by reference, by value-result, by name; output tracing
Q13 Answer: (B)1 mark
Several activations of one procedure at once.
- (A) is wrong: Static is fine.
- (C) is wrong: Static is fine.
- (D) is wrong: Static is fine.
Re-read: Lesson 5.5 — Static vs stack vs heap allocation: which language features force which
Q14 Answer: (A)1 mark
The target no longer exists.
- (B) is wrong: Lives for the whole run.
- (C) is wrong: That is garbage.
- (D) is wrong: Lives for the whole run.
Re-read: Lesson 5.5 — Static vs stack vs heap allocation: which language features force which
Q15 Answer: (A) (B) (D)2 marks
Code lives in the code area.
Re-read: Lesson 5.2 — Activation records, activation trees, the call stack; caller vs callee duties; recursion
Wrong on Q → re-read
| Question | Topic | Re-read |
|---|---|---|
| Q1 | Areas | Lesson 5.1 |
| Q2 | Areas | Lesson 5.1 |
| Q3 | Calls | Lesson 5.2 |
| Q4 | Calls | Lesson 5.2 |
| Q5 | Links | Lesson 5.3 |
| Q6 | Scoping | Lesson 5.3 |
| Q7 | Scoping | Lesson 5.3 |
| Q8 | Scoping | Lesson 5.3 |
| Q9 | Access links | Lesson 5.3 |
| Q10 | Parameters | Lesson 5.4 |
| Q11 | Parameters | Lesson 5.4 |
| Q12 | Parameters | Lesson 5.4 |
| Q13 | Allocation | Lesson 5.5 |
| Q14 | Dangling | Lesson 5.5 |
| Q15 | Records | Lesson 5.2 |