← Quiz: Page Replacement Numericals: all 15 questions
Quiz: Page Replacement Numericals · Question 14 of 15
What does this print? (code)
What does this print?
def faults(refs, frames, policy):
mem, count = [], 0
for i, page in enumerate(refs):
if page in mem:
if policy == "LRU":
mem.remove(page)
mem.append(page)
continue
count += 1
if len(mem) == frames:
if policy == "OPT":
future = refs[i + 1:]
victim = max(mem, key=lambda p: future.index(p) if p in future else len(future))
mem.remove(victim)
else:
mem.pop(0)
mem.append(page)
return count
refs = [1, 2, 3, 4, 1, 2]
print(faults(refs, 3, "FIFO"), faults(refs, 3, "OPT"))
- A.4 4
- B.6 6
- C.6 4
- D.5 4
Answer & explanation
Correct answer: 6 4
FIFO evicts 1 for 4, then 2 for 1, then 3 for 2 — six faults. OPT evicts 3 (never used again), so 1 and 2 hit — four faults.
Can you get the rest right without peeking?
Take the timed Quiz: Page Replacement Numericals quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →