Data Structures & Algorithms MCQs — Programming
3 practice MCQs
The correct answer is marked on every question, with its full explanation.
ProgrammingData Structures & Algorithms PPSC FPSC NTSID: NWF-035
In a stack data structure, which element is removed first?
- AThe last element added. ✓ Correct answer
- BThe first element added.
- CThe middle element.
- DA random element.
✓ Correct answer: The last element added.
Full explanation
In a stack, the element removed first is the last element added. A stack works on the last-in, first-out (LIFO) rule, so the element pushed last is popped first. Why the others are wrong: The first element added — that is the rule of a queue (FIFO), not a stack. The middle element — a stack does not remove the middle element first. A random element — a stack does not remove a random element.
ProgrammingData Structures & Algorithms PPSC FPSC NTSID: PROG-003
Which data structure works on the first-in, first-out rule?
- AStack
- BTree
- CQueue ✓ Correct answer
- DGraph
✓ Correct answer: Queue
Full explanation
The data structure that works on the first-in, first-out rule is the queue. Why the others are wrong: Stack — a stack removes the last element added, not the first. Tree — a tree stores data in branches, not in first-in, first-out order. Graph — a graph stores connected nodes, not in first-in, first-out order.
ProgrammingData Structures & Algorithms PPSC FPSC NTSID: PROG-008
Which sorting method repeatedly swaps neighbouring elements that are in the wrong order?
- ABubble sort ✓ Correct answer
- BMerge sort
- CQuick sort
- DInsertion sort
✓ Correct answer: Bubble sort
Full explanation
Bubble sort. Bubble sort works by going through the list again and again, swapping each pair of neighbouring elements that are in the wrong order. Why the others are wrong: 'Merge sort' — merge sort splits the list into halves and merges them in order, it does not swap neighbours. 'Quick sort' — quick sort picks a pivot and splits the list around it, it does not swap neighbours step by step. 'Insertion sort' — insertion sort picks each element and inserts it into its correct place, it does not swap neighbouring pairs.
Practise these MCQs interactively →
← Programming MCQs