Skip to content
GATEiit

GATE DA 2025 Q64: Stack pseudocode: value of sum

Question

Consider the following pseudocode.

Create empty stack S
Set x=0, flag=0, sum=0
Push x onto S
while (S is not empty){
  if (flag equals 0){
    Set x = x+1
    Push x onto S}
  if (x equals 8):
    Set flag=1
  if (flag equals 1){
    x = Pop(S)
    if (x is odd):
      Pop(S)
    Set sum = sum + x}
}
Output sum

The value of sum output by a program executing the above pseudocode is ____ (Answer in integer)

Try it: numerical answer: type the value

Numerical answer. No negative marking.
Show answer and explanation

Answer: 24 (official GATE 2025 final answer key)

Concept used: Simulating stack push and pop operations.

First step: While flag is 0, the loop pushes 1, 2, …, 8 on top of 0. Once x reaches 8, flag becomes 1 and the loop starts popping: track which popped values are added to sum.

Full step-by-step solution

Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.

Share this question

WhatsAppDownload image

Source: official GATE 2025 DA question paper and final answer key, organised by IIT Roorkee. Question paper · Answer key