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)- A. [9]
- B. [7]
- C. [3, 9]
- 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
- What is printed? python a = [-2, 1, -3, 4, -1, 2, 1, -5, 4] best = cur = a[0] for x in a[1:]: cur = max(x, cur + x) best = max(best, cur)…
- What is printed? python print(sum(bin(i).count('1') for i in range(8)))
- What is printed? python seen = {} for i, x in enumerate([2, 7, 11, 15]): if 9 - x in seen: print((seen[9 - x], i)) break seen[x] = i
- What is printed? python print(len(bin(10 6)) - 2, len(bin(2 20)) - 2)
- What is printed? python print(max(range(10), key=lambda x: x % 4), min([], default=7))
- What is printed? python print(3 -1, 7 // 0.5, 2 0.5 2 0.5 == 2)