ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
x = 7
match x:
    case 1 | 2:
        r = 'small'
    case int(v) if v > 5:
        r = f'big{v}'
    case _:
        r = 'other'
print(r)
  1. A. big7
  2. B. small
  3. C. other
  4. D. 7

Correct answer: A. big7

The first pattern fails for 7. The class pattern int(v) binds v to 7, and the guard v > 5 holds, so the second branch runs and gives 'big7'.

More Python Programming questions

Exams that ask this topic