🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: NFA and Subset Construction: all 10 questions

Quiz: NFA and Subset Construction · Question 8 of 10

What does this print? (code)

What does this print?

eps = {"A": {"B"}, "B": {"C"}, "C": set(), "D": {"A"}}
def closure(q):
    seen, todo = {q}, [q]
    while todo:
        for r in eps[todo.pop()]:
            if r not in seen:
                seen.add(r)
                todo.append(r)
    return sorted(seen)
print(closure("B"), closure("D"))
  1. A.['B'] ['D']
  2. B.['B', 'C'] ['A', 'D']
  3. C.['B', 'C'] ['A', 'B', 'C', 'D']
  4. D.['A', 'B', 'C'] ['A', 'B', 'C', 'D']
Answer & explanation

Correct answer: ['B', 'C'] ['A', 'B', 'C', 'D']

ε-closure includes the state itself and everything reachable by ε-moves only.

Can you get the rest right without peeking?

Take the timed Quiz: NFA and Subset Construction quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →