Stacks, Queues and Linked Lists · Programming, Data Structures and Algorithms
3 questions · asked in 2 of 3 years (2024–2026)
MCQ 1 · NAT 21-mark 2 · 2-mark 1
2024Questions per year2026
Questions 1–3 of 3
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)Answer: 24
At the start, the values are:
Push 0 onto the stack.
Stack (Bottom - Top): 0
As flag = 0, the loop adds 1 to x and pushes it onto the stack. It does this again and again until x = 8.
The stack is now:
Bottom - 0, 1, 2, 3, 4, 5, 6, 7, 8 - Top
When x = 8, the second if statement sets:
The if statements are independent of each other. So the third if statement also runs in this same iteration.
The stack operations go as follows:
After the last iteration the stack is empty, so the while loop ends.
The final value of sum is 24.
Correct Answer: 24
Push 0 onto the stack.
Stack (Bottom - Top): 0
As flag = 0, the loop adds 1 to x and pushes it onto the stack. It does this again and again until x = 8.
The stack is now:
Bottom - 0, 1, 2, 3, 4, 5, 6, 7, 8 - Top
When x = 8, the second if statement sets:
The if statements are independent of each other. So the third if statement also runs in this same iteration.
The stack operations go as follows:
| Iteration | Value popped (x) | x is Odd? | Additional Pop() | sum |
|---|---|---|---|---|
| 1 | 8 | No | — | 8 |
| 2 | 7 | Yes | 6 | 15 |
| 3 | 5 | Yes | 4 | 20 |
| 4 | 3 | Yes | 2 | 23 |
| 5 | 1 | Yes | 0 | 24 |
After the last iteration the stack is empty, so the while loop ends.
The final value of sum is 24.
Correct Answer: 24
Match the items in Column 1 with the items in Column 2 in the following table:
Answer: (A)
Match each property with its data structure:
(p) First In First Out: a queue removes the oldest element first, so (p) goes with (ii) Queues.
(q) Lookup operation: a hash table finds a key directly from its hash value, in constant time on average, so (q) goes with (iii) Hash Tables.
(r) Last In First Out: a stack removes the newest element first, so (r) goes with (i) Stacks.
So the matching is (p) - (ii), (q) - (iii), (r) - (i).
Answer: (A)
(p) First In First Out: a queue removes the oldest element first, so (p) goes with (ii) Queues.
(q) Lookup operation: a hash table finds a key directly from its hash value, in constant time on average, so (q) goes with (iii) Hash Tables.
(r) Last In First Out: a stack removes the newest element first, so (r) goes with (i) Stacks.
So the matching is (p) - (ii), (q) - (iii), (r) - (i).
Answer: (A)
The fundamental operations in a double-ended queue D are:
insertFirst(e) - Insert a new element at the beginning of .
insertLast(e) - Insert a new element at the end of D .
removeFirst()-Remove and return the first element of D.
removeLast () - Remove and return the last element of D.
In an empty double-ended queue, the following operations are performed:
insertFirst (10)
insertLast (32)
removeFirst ()
insertLast (28)
insertLast(17)
removeFirst ()
removeLast ()
The value of is _________ .
insertFirst(e) - Insert a new element at the beginning of .
insertLast(e) - Insert a new element at the end of D .
removeFirst()-Remove and return the first element of D.
removeLast () - Remove and return the last element of D.
In an empty double-ended queue, the following operations are performed:
insertFirst (10)
insertLast (32)
removeFirst ()
insertLast (28)
insertLast(17)
removeFirst ()
removeLast ()
The value of is _________ .
Answer: 17
Follow each operation, writing the deque from first to last:
1. insertFirst(10): D = [10]
2. insertLast(32): D = [10, 32]
3. removeFirst(): removes and returns 10, so a = 10. Now D = [32]
4. insertLast(28): D = [32, 28]
5. insertLast(17): D = [32, 28, 17]
6. removeFirst(): removes and returns 32, so a = 32. Now D = [28, 17]
7. removeLast(): removes and returns 17, so a = 17. Now D = [28]
The last assignment gives the final value of a.
Answer: 17
1. insertFirst(10): D = [10]
2. insertLast(32): D = [10, 32]
3. removeFirst(): removes and returns 10, so a = 10. Now D = [32]
4. insertLast(28): D = [32, 28]
5. insertLast(17): D = [32, 28, 17]
6. removeFirst(): removes and returns 32, so a = 32. Now D = [28, 17]
7. removeLast(): removes and returns 17, so a = 17. Now D = [28]
The last assignment gives the final value of a.
Answer: 17
Keep practising
Topics in Programming, Data Structures and Algorithms
Programming in Python
Stacks, Queues and Linked ListsYou are here
TreesHash TablesLinear and Binary SearchSorting and Divide and ConquerGraph Theory and Graph Algorithms