Question
What will be the output of the following code snippet
that demonstrates stack usage? stack = [] stack.append(1) stack.append(2) stack.append(3) print(stack.pop()) print(stack.pop())Solution
A stack operates on a Last-In-First-Out (LIFO) principle. In the given code snippet, the append() method pushes the elements onto the stack in the order 1, 2, and 3. The pop() method removes elements in reverse order. The first pop() will remove 3, and the second pop() will remove 2, hence the output is 3, 2. Option A (1, 2) is incorrect because 1 was the first element pushed and remains at the bottom until all others are popped. Option C (2, 3) is incorrect because elements are popped in reverse order, making 3 the first element to be removed. Option D (1, 3) is incorrect as it does not follow the LIFO rule of the stack. Option E (3, 1) is incorrect because the second pop removes 2, not 1.
Which state in India witnessed the launch of the 24th Center of Entrepreneurship, "FinGlobe," by STPI?
By the World Book of Records, United Kingdom which tunnel in India has been recognised as the Longest Highway tunnel?
On which occasion did the Punjab government launch the 'Mukhya Mantri Tirth Yatra Yojna'?
What is the coastal cargo target set under the Coastal Shipping Bill, 2025 to be achieved by 2030?
The_________Β and Airbus signed an MoU to facilitate increased collaboration in developing education programmes focusing more on research to foster tal...
What is the key feature of Uberβs new Moto Women service launched in Bengaluru?
When is the first phase of Bima Sugam, the digital insurance platform, now expected to be rolled out?
Who won the SAFF U-19 Championship 2025?
At the BBC Indian Sportswoman of the Year Awards 2025, which Indian cricketer was named BBC Indian Sportswoman of the Year?
How many films from Poland will be showcased at the 31st Kolkata International Film Festival 2025?