ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
st = []
for t in '2 1 + 3 *'.split():
    if t.isdigit():
        st.append(int(t))
    else:
        b = st.pop()
        a = st.pop()
        st.append(a + b if t == '+' else a * b)
print(st)
  1. A. [9]
  2. B. [7]
  3. C. [3, 9]
  4. D. [2, 1, 3]

Correct answer: A. [9]

This is reverse Polish evaluation. 2 and 1 are added to give 3. Pushing 3 and multiplying gives 9, the only value left on the stack.

More Python Programming questions

Exams that ask this topic