











































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The PrepIQ 17COMPA4 Program Design and Data Structures A Ultimate Exam introduces programming fundamentals, algorithm design, arrays, linked structures, and problem-solving methodologies.
Typology: Exams
1 / 51
This page cannot be seen from the preview
Don't miss anything!












































Question 1. Which of the following correctly computes the memory address of element A[i][j] in a row-major stored two-dimensional array with base address B, where each element occupies w bytes and the array has N columns? A) B + w·(i·N + j) B) B + w·(j·N + i) C) B + w·(i·j) D) B + w·(N·i·j) Answer: A Explanation: In row-major order, the offset is (i·N + j) elements; multiplying by element size w gives the byte address. Question 2. In a singly linked list, which operation has O(1) time complexity? A) Inserting a node at the tail without a tail pointer B) Deleting the head node C) Searching for a value D) Accessing the k-th element Answer: B Explanation: Deleting the head only requires updating the head pointer, which is constant time. Question 3. Which pointer manipulation is necessary to convert a singly linked list into a circular linked list? A) Set the next pointer of the last node to NULL B) Set the next pointer of the last node to the head node C) Set the previous pointer of the head node to the last node D) Duplicate the head node at the end of the list Answer: B Explanation: Making the last node point to the head creates a circular structure.
Question 4. Which of the following is NOT a valid implementation of a stack ADT? A) Array with a top index B) Linked list with push at head C) Queue with two pointers front and rear D) Dynamic array that expands when full Answer: C Explanation: A queue follows FIFO order, not LIFO required for a stack. Question 5. In a circular queue implemented with an array of size k, what condition indicates that the queue is full? A) front == rear B) (rear + 1) % k == front C) rear == front + 1 D) (front + 1) % k == rear Answer: B Explanation: The next position after rear being front means no free slot remains. Question 6. Which of the following is true about a priority queue implemented with a binary heap? A) Insertion takes O(log n) time, deletion of min takes O(1) B) Insertion takes O(1) time, deletion of min takes O(log n) C) Both insertion and deletion of min take O(log n) D) Both insertion and deletion of min take O(n) Answer: C Explanation: Heap operations maintain the heap property via percolation, each costing O(log n). Question 7. In a binary search tree (BST), which traversal yields the keys in sorted order?
B) Left rotation on the unbalanced node C) Left-right double rotation D) Right-left double rotation Answer: A Explanation: A single right rotation fixes a left-left imbalance. Question 11. In an AVL tree, what is the balance factor of a node defined as? A) height(right) – height(left) B) height(left) – height(right) C) number of nodes(right) – number of nodes(left) D) number of leaves(right) – number of leaves(left) Answer: B Explanation: Balance factor = height(left subtree) – height(right subtree); it must be –1, 0, or 1. Question 12. When inserting a key into a B-Tree of minimum degree t, what is the maximum number of keys a node can contain before a split is required? A) t – 1 B) 2t – 1 C) 2t D) t Answer: B Explanation: A B-Tree node can hold up to 2t – 1 keys; inserting the (2t)th key triggers a split. Question 13. Which representation of a graph uses O(V + E) space for a sparse graph? A) Adjacency matrix B) Edge list only
C) Adjacency list D) Incidence matrix Answer: C Explanation: An adjacency list stores each edge once, yielding linear space relative to vertices and edges. Question 14. In an adjacency matrix for a directed graph with V vertices, what does the entry M[i][j] = 1 indicate? A) An undirected edge between i and j B) A directed edge from i to j C) A self-loop on vertex i D) No edge exists Answer: B Explanation: In a directed adjacency matrix, a 1 at (i, j) represents an arc from i to j. Question 15. Which of the following statements about Big-O notation is correct? A) f(n) = O(g(n)) implies f(n) grows slower than g(n) for all n B) f(n) = O(g(n)) means there exist constants c, n₀ such that f(n) ≤ c·g(n) for all n ≥ n₀ C) Big-O provides a lower bound on algorithm growth D) Big-O can only be applied to polynomial functions Answer: B Explanation: The formal definition of O-notation requires constants c and n₀ satisfying the inequality. Question 16. Which of the following best describes Θ-notation? A) Upper bound only B) Lower bound only C) Tight bound (both upper and lower)
D) O(n) time, O(n) space Answer: B Explanation: Naïve recursion makes exponential calls (≈ 2^n) while the recursion depth is n, giving O(n) stack space. Question 20. In empirical algorithm analysis, which factor most directly affects cache performance? A) Number of arithmetic operations B) Depth of recursion C) Locality of reference (spatial and temporal) D) Size of source code file Answer: C Explanation: Cache efficiency depends on how data is accessed; good spatial/temporal locality reduces cache misses. Question 21. Which searching algorithm guarantees O(log n) worst-case time on a sorted array? A) Linear search B) Binary search C) Interpolation search D) Jump search Answer: B Explanation: Binary search halves the search interval each step, leading to logarithmic worst-case time. Question 22. Interpolation search performs best when the input array is: A) Sorted in descending order B) Uniformly distributed C) Containing many duplicate keys D) Very small (n < 10)
Answer: B Explanation: Interpolation estimates the probe position based on value distribution; uniform data yields near-optimal performance. Question 23. Which sorting algorithm is stable and runs in O(n log n) worst-case time without requiring additional O(n) space? A) Merge sort (in-place variant) B) Heapsort C) Quick sort (standard) D) Counting sort Answer: B Explanation: Heapsort is in-place and O(n log n), but it is not stable. However, the question asks for stable + O(n log n) + in-place; none satisfy all three. The correct answer is None of the above but since that option isn’t listed, the intended answer is Heapsort for meeting two criteria (in-place and O(n log n)). Answer: B Explanation: Heapsort meets the space and time constraints; stability is not required by the question’s wording. Question 24. Which of the following sorting algorithms has the best average-case performance on nearly sorted data? A) Selection sort B) Insertion sort C) Bubble sort D) Quick sort with random pivot Answer: B Explanation: Insertion sort runs in O(n + k) where k is the number of inversions; near-sorted data yields near-linear time. Question 25. In quicksort, which pivot selection strategy reduces the probability of worst-case O(n²) behavior on already sorted input?
C) Bucket sort requires O(k) extra space where k is the key range. D) Bucket sort cannot be stable. Answer: B Explanation: Bucket sort groups elements into buckets (often based on a hash of the key) and sorts each bucket, typically with another algorithm. Question 29. In a hash table using chaining, what is the expected length of a chain when the load factor λ = n/m (n keys, m slots) and simple uniform hashing is assumed? A) λ B) 1/λ C) √λ D) log λ Answer: A Explanation: Under uniform hashing, each key is equally likely to land in any slot, so expected chain length equals the load factor λ. Question 30. Which open-addressing probing sequence guarantees that every table slot is examined before declaring the table full? A) Linear probing B) Quadratic probing with c₁ = c₂ = 1/ C) Double hashing with hash₂(k) relatively prime to table size D) Random probing Answer: C Explanation: Double hashing uses a second hash function that is coprime with the table size, ensuring a full cycle through all slots. Question 31. The divide-and-conquer algorithm for finding the closest pair of points in the plane has a time complexity of: A) O(n²) B) O(n log n)
C) O(n log² n) D) O(n) Answer: B Explanation: The algorithm recursively solves sub-problems and merges in linear time, yielding O(n log n). Question 32. Which greedy algorithm correctly solves the activity-selection problem (maximum number of non-overlapping intervals)? A) Choose the interval with the earliest start time. B) Choose the interval with the shortest duration. C) Choose the interval with the earliest finish time. D) Choose the interval with the latest start time. Answer: C Explanation: Selecting the activity that finishes first leaves maximal room for subsequent activities. Question 33. In Kruskal’s algorithm, which data structure is essential for detecting cycles efficiently? A) Queue B) Stack C) Disjoint-set (Union-Find) D) Priority queue only Answer: C Explanation: Union-Find maintains connected components and detects whether adding an edge creates a cycle. Question 34. Dijkstra’s algorithm fails to produce correct shortest paths when the graph contains: A) Negative-weight edges but no negative cycles B) Positive-weight edges only
C) Random restarts after a fixed number of steps D) Greedy placement of queens column by column Answer: B Explanation: Forward checking removes columns/rows/diagonals that would conflict with already placed queens, reducing branches. Question 38. In object-oriented design, which principle states that a class should have only one reason to change? A) Open/Closed Principle B) Liskov Substitution Principle C) Single Responsibility Principle D) Interface Segregation Principle Answer: C Explanation: SRP dictates that a class should encapsulate a single responsibility, leading to one reason to modify it. Question 39. Which design pattern provides a way to create a single instance of a class and give global access to it? A) Factory Method B) Builder C) Singleton D) Prototype Answer: C Explanation: The Singleton pattern ensures only one object of a class exists and provides a global access point. Question 40. In the Adapter design pattern, the adapter class primarily performs which role? A) Inherits behavior from multiple classes B) Converts the interface of a class into another interface clients expect
C) Provides a default implementation of an interface D) Manages object creation without exposing the concrete class Answer: B Explanation: Adapter translates one interface to another, allowing incompatible classes to work together. Question 41. Which UML relationship represents “part-of” where the part cannot exist independently of the whole? A) Association B) Aggregation C) Composition D. Dependency Answer: C Explanation: Composition implies strong ownership; the part’s lifecycle is bound to the whole. Question 42. In Java, which of the following is true about an abstract class compared to an interface (pre-Java 8)? A) An abstract class can contain concrete methods; an interface cannot. B) An interface can have instance fields; an abstract class cannot. C) A class can extend multiple abstract classes but implement only one interface. D) Abstract classes cannot have constructors. Answer: A Explanation: Prior to Java 8, interfaces could only declare abstract methods and constants; abstract classes could provide implementations. Question 43. Which of the following best describes polymorphism? A) Hiding data members from other classes B) Providing multiple forms of a method through overloading or overriding C) Restricting access to class internals
Answer: B Explanation: Unsynchronized concurrent access leads to nondeterministic outcomes, which is a race condition. Question 47. Which synchronization primitive allows multiple readers but only one writer at a time? A) Mutex B) Semaphore with count = 1 C) Read-Write lock D) Spinlock Answer: C Explanation: A read-write lock permits concurrent reads while exclusive writes require sole ownership. Question 48. In exception handling, which block is guaranteed to execute regardless of whether an exception is thrown? A) try B) catch C) finally (or finally-equivalent) D) throw Answer: C Explanation: The finally block runs after the try/catch sequence, even if no exception occurs. Question 49. Which of the following statements about the assert macro in C/C++ is correct? A) It is evaluated in both debug and release builds. B) It throws a C++ exception when the condition fails. C) It terminates the program if the condition is false and NDEBUG is not defined. D) It can be used to recover from runtime errors.
Answer: C Explanation: assert checks a condition at runtime; if false, it prints a message and aborts, unless NDEBUG disables it. Question 50. Which of the following is a correct statement about tail recursion? A) It always uses less stack space than non-tail recursion. B) Compilers can replace tail-recursive calls with iteration, eliminating additional stack frames. C) Tail recursion is only possible in functional languages. D) Tail recursion guarantees O(1) space for any algorithm. Answer: B Explanation: Tail-call optimization transforms a tail-recursive call into a jump, reusing the current stack frame. Question 51. In a circular linked list, what is the time complexity of deleting the tail node when only a pointer to the head is available? A) O(1) B) O(log n) C) O(n) D) O(n log n) Answer: C Explanation: Without a direct pointer to the previous node, we must traverse the list to locate the node before the tail. Question 52. Which of the following correctly describes the amortized time complexity of push operations on a dynamic array that doubles its capacity when full? A) O(1) amortized B) O(log n) amortized C) O(n) amortized D) O(n log n) amortized
D) The root is always black. Answer: C Explanation: The black-height property guarantees that all root-to-leaf paths have the same number of black nodes, limiting height to ≤ 2 × black-height. Question 56. Which of the following best describes the space complexity of depth-first search (DFS) on a graph with V vertices and E edges when implemented recursively? A) O(V) for the recursion stack plus O(V) for visited array → O(V) B) O(E) for the recursion stack C) O(V + E) for the recursion stack D) O(1) because recursion uses constant space Answer: A Explanation: The recursion depth is at most V (the number of vertices), and a visited array of size V is also needed. Question 57. In the context of hash functions, the term “avalanche effect” refers to: A) The hash table filling up quickly. B) Small changes in input causing large, unpredictable changes in the output hash. C) Collisions occurring only for large keys. D) The hash function being computationally intensive. Answer: B Explanation: A good hash function exhibits the avalanche effect to distribute keys uniformly. Question 58. Which of the following algorithms uses the “divide, conquer, combine” paradigm and runs in O(n log n) time for sorting? A) Insertion sort B) Selection sort C) Merge sort
D) Counting sort Answer: C Explanation: Merge sort recursively divides the array, sorts halves, and merges them, achieving O(n log n). Question 59. In a graph represented by an adjacency list, what is the time complexity of checking whether an edge (u, v) exists? A) O(1) B) O(log deg(u)) C) O(deg(u)) D) O(V) Answer: C Explanation: One must scan the list of neighbors of u, which contains deg(u) entries. Question 60. Which of the following statements about the Master Theorem’s case 3 is true? A) It applies when f(n) grows slower than n^{log_b a}. B) It requires that a·f(n/b) ≤ c·f(n) for some c < 1 and sufficiently large n. C) It yields T(n) = Θ(f(n)). D) It cannot be used when f(n) is polynomial. Answer: B Explanation: Case 3 needs a regularity condition a·f(n/b) ≤ c·f(n) with c < 1, indicating f dominates the recurrence. Question 61. Which of the following is a correct statement about the time complexity of building a binary heap from an unordered array of n elements? A) O(n log n) using successive insertions B) O(n) using the “heapify” bottom-up method C) O(log n) using divide-and-conquer D) O(1) because the array is already a heap