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

Quiz: CFG and PDA · Question 9 of 10

What does this print? (code)

What does this print?

pairs = {")": "(", "]": "[", "}": "{"}
def ok(s):
    stack = []
    for ch in s:
        if ch in "([{":
            stack.append(ch)
        elif not stack or stack.pop() != pairs[ch]:
            return False
    return not stack
print([ok(s) for s in ["([]{})", "([)]", "(("]])
  1. A.[True, True, False]
  2. B.[True, False, False]
  3. C.[False, False, False]
  4. D.[True, False, True]
Answer & explanation

Correct answer: [True, False, False]

The stack matches the nearest open bracket: ([)] closes ( while [ is on top, and (( leaves the stack non-empty.

Can you get the rest right without peeking?

Take the timed Quiz: CFG and PDA quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →