Consider the given function named 'display' to show the contents of the stack:
def display(glassStack)
Arrange the following code statements in correct sequence to complete the above function.
A. print("Current elements in the stack are: ")
B. print(glassStack[i])
C. for i in range(x-1,-1,-1):
D. x=len(glassStack)
Consider the given function named 'display' to show the contents of the stack:
def display(glassStack)
Arrange the following code statements in correct sequence to complete the above function.
A. print("Current elements in the stack are: ")
B. print(glassStack[i])
C. for i in range(x-1,-1,-1):
D. x=len(glassStack)
Solution
The loop in C uses x, so D must come first. The heading in A is printed once before the loop starts, and B is the loop body, so it comes right after C. The loop runs from the top of the stack (index x-1) down to index 0, so the top element is displayed first.
D. x=len(glassStack)
A. print("Current elements in the stack are: ")
C. for i in range(x-1,-1,-1):
B. print(glassStack[i])
Option 2: D comes after the loop, so x is undefined when C runs.
Related questions:
2025: 16 May Shift 1
2025: 15 May Shift 2
2026: 26 May Shift 1
2026: 23 May Shift 2
2025: 22 May Shift 1