GATE DA 2025 Q64: Stack pseudocode: value of sum
- Programming, Data Structures and Algorithms
- 2 marks
- NAT
- No negative marking
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 sumThe 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
Source: official GATE 2025 DA question paper and final answer key, organised by IIT Roorkee. Question paper · Answer key
More Programming, Data Structures and Algorithms PYQs
- 2025 · Q18Hashing with linear probing: h(x) = 3x mod 10
- 2025 · Q23Python lists: which call gives A = [1, 2, 3, 4, 5, 6]?
- 2025 · Q27When does binary search run in O(log n)?
- 2025 · Q29Insertion sort with exactly two swaps: possible values of x
- 2025 · Q47Python sets in a while loop: which set ends up with "this"?
- 2025 · Q58Shortest paths: which edges cannot be in G?