Skip to main contentSkip to solution

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

✅ Correct Option: 1

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.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question