← Quiz: Banker's Algorithm in Practice: all 15 questions
Quiz: Banker's Algorithm in Practice · Question 14 of 15
What does this print? (code)
What does this print?
def is_safe(available, alloc, need):
work, finished, order = list(available), [False] * len(alloc), []
progress = True
while progress:
progress = False
for i in range(len(alloc)):
if not finished[i] and all(n <= w for n, w in zip(need[i], work)):
work = [w + a for w, a in zip(work, alloc[i])]
finished[i] = True
order.append(f"P{i}")
progress = True
return all(finished), order
print(is_safe([1, 1], [[1, 0], [0, 1]], [[1, 2], [0, 1]]))
- A.(True, ['P0', 'P1'])
- B.(False, [])
- C.(True, ['P1', 'P0'])
- D.(False, ['P1'])
Answer & explanation
Correct answer: (True, ['P1', 'P0'])
P0 needs 2 of the second resource but only 1 is free; P1 fits, returns (0, 1), and then P0 fits.
Can you get the rest right without peeking?
Take the timed Quiz: Banker's Algorithm in Practice quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →