πŸ“’ Too many exams? Don’t know which one suits you best? Book Your Free Expert πŸ‘‰ call Now!

  • google app store apple app store
  • βœ–

      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())
      A 1, 2 Correct Answer Incorrect Answer
      B 3, 2 Correct Answer Incorrect Answer
      C 2, 3 Correct Answer Incorrect Answer
      D 1, 3 Correct Answer Incorrect Answer
      E 3, 1 Correct Answer Incorrect Answer

      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.

      Practice Next

      Relevant for Exams:

      ask-question