ProgrammingHardCognizant GenC · TCS NQT
What is printed?
What is printed?
class Q:
def __init__(self):
self.i, self.o = [], []
def push(self, x):
self.i.append(x)
def pop(self):
if not self.o:
while self.i:
self.o.append(self.i.pop())
return self.o.pop()
q = Q()
for x in [1, 2, 3]:
q.push(x)
a = q.pop()
q.push(4)
print(a, q.pop(), q.pop(), q.pop())- A. 1 2 3 4
- B. 1 3 2 4
- C. 3 1 2 4
- D. 1 2 4 3
Correct answer: A. 1 2 3 4
The input stack moves to the output stack only when the output is empty, which gives first-in first-out order. After the first pop returns 1, the remaining pops return 2, 3 and 4.
More Python Programming questions
- What is printed? python print([y for x in [[1, 2], [3], []] for y in x if y % 2])
- What is printed? python out = [] for i in range(3): try: if i == 1: continue out.append(i) finally: out.append('f') print(out)
- What is printed? python print(int('0x1f', 16), int('1f', 16), int('0o17', 8))
- What is printed? python print(divmod(-9, 4), -9 % 4, round(-9 / 4))
- What is printed? python print(len(str(float(1016))), str(1016 1.0), len(str(1016)))
- What is printed? python print(0.1 + 0.7 == 0.8, 0.8 - 0.7, 0.3 / 0.1, int(0.3 / 0.1))