Data Structures Beginner Quiz
30 questions on Data Structures.
Question 1: Which data structure uses the LIFO (Last In First Out) principle?
- A. Queue
- B. Stack β (correct answer)
- C. Array
- D. Graph
Explanation: A stack follows the Last In First Out (LIFO) principle, where the last element added is the first one to be removed (like a stack of plates).
Question 2: Which data structure uses the FIFO (First In First Out) principle?
- A. Stack
- B. Queue β (correct answer)
- C. Tree
- D. Hash Table
Explanation: A queue follows the First In First Out (FIFO) principle, where the first element added is the first one to be removed (like a line at a grocery store).
Question 3: What is a Data Structure?
- A. A programming language
- B. A specialized format for organizing, processing, retrieving, and storing data β (correct answer)
- C. A type of computer memory
- D. A graphical user interface
Explanation: Data structures define how data is organized in memory so it can be accessed and modified efficiently.
Question 4: Which data structure stores elements in a contiguous block of memory?
- A. Linked List
- B. Graph
- C. Tree
- D. Array β (correct answer)
Explanation: Arrays allocate a continuous block of memory for their elements, allowing O(1) random access via an index.
Question 5: Which data structure consists of nodes where each node contains data and a reference (or link) to the next node?
- A. Array
- B. Stack
- C. Linked List β (correct answer)
- D. Hash Table
Explanation: A linked list is a linear collection of data elements where order is not given by physical placement in memory, but rather each element points to the next.
Question 6: What is the primary advantage of a Linked List over an Array?
- A. Faster random access
- B. Dynamic size and efficient insertion/deletion without shifting elements β (correct answer)
- C. Less memory usage
- D. Contiguous memory allocation
Explanation: Unlike arrays, linked lists do not require a contiguous block of memory and can easily grow or shrink, allowing O(1) insertions if the node location is known.
Question 7: Which operation adds an element to the top of a Stack?
- A. pop()
- B. push() β (correct answer)
- C. enqueue()
- D. dequeue()
Explanation: The push() operation adds an item to the top of a stack.
Question 8: Which operation removes an element from the front of a Queue?
- A. push()
- B. pop()
- C. dequeue() β (correct answer)
- D. enqueue()
Explanation: The dequeue() operation removes and returns the front item from a queue.
Question 9: Which data structure maps keys to values for highly efficient lookups?
- A. Queue
- B. Linked List
- C. Hash Table (Dictionary) β (correct answer)
- D. Stack
Explanation: Hash tables use a hash function to compute an index into an array of buckets, from which the desired value can be found in average O(1) time.
Question 10: What problem occurs in a Hash Table when two keys hash to the same index?
- A. Memory leak
- B. Collision β (correct answer)
- C. Stack overflow
- D. Segmentation fault
Explanation: A collision occurs when a hash function maps two different keys to the same bucket. It is commonly resolved using chaining or open addressing.
Question 11: Which non-linear data structure simulates a hierarchical tree structure with a root value and subtrees of children?
- A. Linked List
- B. Queue
- C. Tree β (correct answer)
- D. Stack
Explanation: A tree represents hierarchical data, consisting of nodes connected by edges, starting from a single root node.
Question 12: In a Tree data structure, what do you call a node that has no children?
- A. Root
- B. Branch
- C. Leaf β (correct answer)
- D. Edge
Explanation: A leaf node is a terminal node that does not have any child nodes.
Question 13: Which data structure is essentially a set of vertices (nodes) and edges connecting them, used to represent networks like maps or social networks?
- A. Graph β (correct answer)
- B. Stack
- C. Array
- D. Queue
Explanation: Graphs are used to model pairwise relations between objects, making them ideal for representing networks, paths, and relationships.
Question 14: What is the difference between a directed graph and an undirected graph?
- A. Directed graphs have weights, undirected do not
- B. Undirected graph edges have no directional arrows (two-way), while directed graph edges go one-way β (correct answer)
- C. Directed graphs cannot have cycles
- D. Undirected graphs can only have two nodes
Explanation: In a directed graph, edges have a specific direction (A -> B). In an undirected graph, relationships are bidirectional (A <-> B).
Question 15: Which data structure is typically used to implement an "Undo" feature in software applications?
- A. Queue
- B. Array
- C. Stack β (correct answer)
- D. Hash Table
Explanation: An Undo feature requires reversing the most recent action first, perfectly matching the LIFO (Last In First Out) property of a stack.
Question 16: In an Array, what is the time complexity to access an element by its index?
- A. O(1) β (correct answer)
- B. O(n)
- C. O(log n)
- D. O(n^2)
Explanation: Because arrays store elements contiguously, the memory address of any element can be calculated instantly using math, resulting in constant O(1) access time.
Question 17: Why is inserting an element at the beginning of a standard Array generally an O(n) operation?
- A. Because the memory must be erased first
- B. Because all existing elements must be shifted one position to the right to make room β (correct answer)
- C. Because arrays are linked
- D. Because you must sort the array first
Explanation: Arrays are contiguous. Adding a new item at index 0 requires moving every other element, which takes time proportional to the array size (n).
Question 18: Which data structure is best suited for implementing a Breadth-First Search (BFS) algorithm?
- A. Stack
- B. Queue β (correct answer)
- C. Hash Table
- D. Array
Explanation: BFS explores nodes layer by layer. A Queue ensures that the nodes discovered first are explored first (FIFO).
Question 19: Which data structure is best suited for implementing a Depth-First Search (DFS) algorithm?
- A. Stack β (correct answer)
- B. Queue
- C. Hash Table
- D. Linked List
Explanation: DFS dives as deep as possible before backtracking. A Stack (or call stack via recursion) perfectly handles this LIFO exploration.
Question 20: What is a Doubly Linked List?
- A. A list with two types of data
- B. A list where each node contains pointers to both the next node AND the previous node β (correct answer)
- C. A list that requires double the memory for data
- D. Two linked lists connected together
Explanation: A doubly linked list allows traversal in both directions (forward and backward) because each node holds references to its predecessor and successor.
Question 21: If a Hash Table has a bad hash function that constantly causes collisions, what happens to its lookup time complexity?
- A. It stays O(1)
- B. It improves to O(log n)
- C. It degrades to O(n) in the worst case β (correct answer)
- D. It crashes
Explanation: If all items hash to the exact same bucket, they form a single linked list. Searching that list requires checking every item, which is O(n).
Question 22: What defines a Binary Search Tree (BST)?
- A. Every node has exactly two children
- B. The left child's value is less than the parent, and the right child's value is greater than the parent β (correct answer)
- C. It is completely balanced
- D. The root node is always 0
Explanation: This specific ordering property makes BSTs excellent for searching, as it halves the search space at each step (O(log n) time average).
Question 23: Which structure is heavily used by operating systems for process scheduling (like CPU task management)?
- A. Stack
- B. Tree
- C. Priority Queue β (correct answer)
- D. Hash Table
Explanation: A priority queue (often implemented with a Heap) ensures that high-priority tasks are executed before lower-priority tasks, regardless of their arrival time.
Question 24: In a Singly Linked List, if you want to delete the last node, what is the time complexity? (Assuming you only have a pointer to the head).
- A. O(1)
- B. O(log n)
- C. O(n) β (correct answer)
- D. O(n^2)
Explanation: To delete the last node, you must update the "next" pointer of the *second-to-last* node. To find the second-to-last node, you must traverse the entire list from the head, which takes O(n) time.
Question 25: What is a Circular Queue?
- A. A queue where elements move in a circle physically
- B. A queue where the last position is connected back to the first position to make efficient use of array space β (correct answer)
- C. A queue that never ends
- D. A queue that holds pi values
Explanation: In a normal array-based queue, dequeuing leaves empty spaces at the front. A circular queue wraps around, reusing those empty spaces efficiently.
Question 26: What is the worst-case search time complexity of a perfectly balanced Binary Search Tree?
- A. O(1)
- B. O(n)
- C. O(log n) β (correct answer)
- D. O(n^2)
Explanation: Because the tree is perfectly balanced, every step down the tree eliminates half of the remaining nodes, resulting in logarithmic time complexity.
Question 27: What happens if you insert elements into a standard Binary Search Tree in sorted order (e.g., 1, 2, 3, 4, 5)?
- A. It balances itself
- B. It throws an error
- C. It degenerates into a Linked List, making search time O(n) β (correct answer)
- D. It becomes a graph
Explanation: Because each element is greater than the last, they are all inserted as right children. The tree becomes a straight line, losing its O(log n) search efficiency.
Question 28: Which data structure is typically used to implement a LRU (Least Recently Used) Cache efficiently?
- A. Array and Stack
- B. Hash Table and Doubly Linked List β (correct answer)
- C. Two Queues
- D. Binary Tree
Explanation: The Hash Table provides O(1) lookup, while the Doubly Linked List allows O(1) removal of the oldest item and O(1) insertion to mark items as recently used.
Question 29: In Graph theory, what does an "Adjacency Matrix" represent?
- A. A mathematical formula
- B. A 2D array where rows and columns represent vertices, and the value indicates if an edge exists between them β (correct answer)
- C. A list of linked lists
- D. A sorting algorithm
Explanation: An adjacency matrix is a fast (O(1) edge lookup) but memory-heavy (O(V^2)) way to represent graph connections.
Question 30: What is the "Stack Overflow" error in programming?
- A. When an array exceeds its maximum size
- B. When memory used by the call stack exceeds the allocated limit, usually due to infinite or excessively deep recursion β (correct answer)
- C. When a queue is empty
- D. When a linked list loop is detected
Explanation: Every function call pushes a frame onto the system's Call Stack. If a recursive function lacks a base case, it will call itself infinitely until the stack memory is full, crashing the program.