DSA Online Test - Quiz - MCQ Questions

Data structures and algorithms are fundamental concepts in computer science that enable efficient data organization, manipulation, and problem-solving. Understanding these concepts is crucial for developing efficient and scalable software solutions.

This quiz tests your data structures and algorithms knowledge through 35 multiple-choice questions (MCQs). It will help you assess your understanding of various data structures and sorting and searching algorithms.

1. What is an array?

a) A collection of items stored at contiguous memory locations
b) A collection of key-value pairs
c) A blueprint for creating objects
d) A type of function in programming

2. Which of the following is a characteristic of a stack data structure?

a) FIFO (First In, First Out)
b) LIFO (Last In, First Out)
c) Ordered by a key
d) None of the above

3. What does the 'Big O' notation describe?

a) The maximum space used by an algorithm
b) The minimum time needed by an algorithm
c) The upper bound of the running time of an algorithm
d) The bugs present in an algorithm

4. What is a linked list?

a) A data structure that stores elements in a linear fashion
b) A data structure consisting of a collection of elements called nodes
c) A data structure where each element points to the next
d) Both b) and c) are correct

5. Which algorithm is typically used for sorting small datasets?

a) Merge Sort
b) Bubble Sort
c) Quick Sort
d) Radix Sort

6. What is a binary search tree?

a) A tree data structure in which each node has no more than two children
b) A tree data structure where each child node is smaller than the parent node
c) A tree data structure where the left child is less than the parent node, and the right child is more than the parent node
d) A non-linear data structure that maximizes data search speed

7. What is a queue used for in computing?

a) To manage the running tasks on a computer
b) To store data for later processing in the order elements were added
c) To reverse the order of elements
d) To perform high-speed computations

8. What does a depth-first search (DFS) algorithm do?

a) Visits the root node first, then the leaf nodes
b) Visits all the nodes of a graph or tree data structure by exploring as deep as possible along each branch before backtracking
c) Searches element by element starting from the last node
d) Searches only the left subtree of a graph

9. What is recursion in the context of programming?

a) A method of simplifying a problem by breaking it down into smaller problems
b) A function that calls itself
c) A type of loop that repeats until a condition is met
d) A programming language designed for developing web applications

10. What does an in-order traversal of a binary tree involve?

a) Visiting the root node first, then the left and right subtrees
b) Visiting the left subtree, the root node, and then the right subtree
c) Visiting the right subtree, the root node, and then the left subtree
d) Visiting the left and right subtrees before visiting the root node

11. What is the time complexity of accessing an element in an array?

a) O(1)
b) O(n)
c) O(log n)
d) O(n^2)

12. Which data structure would be most efficient for implementing a queue?

a) Array
b) Linked List
c) Hash Table
d) Binary Tree

13. What does the following code do?

int i = 0;
while (i < 100) {
    i += 5;
}
a) Counts from 0 to 95 in increments of 5
b) Creates an infinite loop
c) Counts from 0 to 100 in increments of 5
d) None of the above

14. What is the worst-case time complexity of bubble sort?

a) O(n)
b) O(n log n)
c) O(n^2)
d) O(log n)

15. What is the result of a pre-order traversal of a binary search tree?

a) Sorted order of the values
b) Reverse sorted order of the values
c) Root node visited before its children
d) Children visited before the root node

16. Which algorithm is not a divide-and-conquer algorithm?

a) Quick sort
b) Merge sort
c) Insertion sort
d) Binary search

17. What is the space complexity of a recursive factorial function?

a) O(1)
b) O(n)
c) O(log n)
d) O(n^2)

18. How does a hash table achieve an average time complexity of O(1) for lookups?

a) By using an array-based implementation
b) Through linked lists
c) By using a good hash function to distribute keys
d) By sorting the data once inserted

19. What data structure is primarily used to implement the undo functionality in text editors?

a) Queue
b) Stack
c) Priority queue
d) Set

20. What does the following algorithm calculate?

function gcd(a, b) {
    while (b != 0) {
        t = b;
        b = a % b;
        a = t;
    }
    return a;
}
a) The least common multiple of a and b
b) The highest common factor (gcd) of a and b
c) The summation of a and b
d) The product of a and b

21. What is a binary heap?

a) A type of binary tree that is completely filled except possibly for the bottom level
b) A balanced binary tree
c) A binary tree that is only filled on the right
d) A graph with no cycles

22. What are the characteristics of a graph data structure?

a) Only nodes
b) Only edges
c) Nodes connected by edges
d) Unordered list of elements

23. What is the time complexity of inserting an element at the end of a singly linked list, assuming you have a pointer to the tail?

a) O(1)
b) O(n)
c) O(log n)
d) O(n^2)

24. Which of the following is not a linear data structure?

a) Array
b) Linked list
c) Queue
d) Tree

25. What does the following code snippet do in an array arr?

for (int i = 0; i < n; i++) {
    arr[i] = i;
}
a) Initializes the array with zeros
b) Initializes the array with indices
c) Checks if all elements are zero
d) Prints each element in the array

26. Which sorting algorithm is considered stable?

a) Quick sort
b) Heap sort
c) Merge sort
d) Selection sort

27. What property does a balanced binary search tree have?

a) All leaf nodes are at the same level
b) The difference between heights of left and right subtrees cannot be more than one
c) All nodes have either 0 or 2 children
d) The tree cannot have more than three levels

28. How is a new element added to a stack?

a) At the beginning
b) At the middle
c) At the end
d) After sorting

29. What is depth-first search primarily used for in graphs?

a) Finding the shortest path
b) Traversing all nodes
c) Checking connectivity
d) All of the above

30. What does this code calculate?

int fib(int n) {
    if (n <= 1) return n;
    return fib(n-1) + fib(n-2);
}
a) Factorial of n
b) n-th Fibonacci number
c) Sum of first n natural numbers
d) n-th prime number

31. What data structure is best for implementing the back functionality in a web browser?

a) Queue
b) Stack
c) Array
d) Linked List

32. What is the output of this code when searching for an element in a binary search tree?

bool search(TreeNode* root, int key) {
    if (root == NULL) return false;
    if (root->val == key) return true;
    if (root->val < key) return search(root->right, key);
    return search(root->left, key);
}
a) Returns true if key is found, false otherwise
b) Returns the value of the key
c) Always returns true
d) Outputs the path to the key

33. What is a characteristic feature of a queue?

a) FIFO (First In, First Out)
b) LIFO (Last In, First Out)
c) Priority order
d) Random access

34. What is true about the complexity of binary search algorithms?

a) O(n)
b) O(log n)
c) O(n^2)
d) O(1)

35. Which data structure represents a collection of key-value pairs?

a) Queue
b) Stack
c) Tree
d) Hash Table

Comments