Docsity

PROGRAMMING FUNDAMENTALS & DATA STRUCTURES testbank, Exams of Database Programming

Toggle sidebar
bg1
CHAPTER 1: PROGRAMMING FUNDAMENTALS & DATA STRUCTURES (Q1–Q40)
Multiple-Choice Questions
Q1. Which data structure follows Last-In-First-Out (LIFO) ordering?
A) Queue
B) Stack
C) Linked list
D) Hash table
Answer: B. A stack is LIFO (push/pop at one end). A queue is FIFO; a linked list and hash table
have no inherent LIFO order.
Q2. Which data structure is best for implementing a breadth-first search (BFS) frontier?
A) Stack
B) Queue
C) Priority queue
D) Binary search tree
Answer: B. BFS uses a FIFO queue to explore level by level. A stack gives DFS; a priority queue
gives best-first/Dijkstra-style search.
Q3. The average time complexity of lookup in a hash table with good hashing is:
A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)
Answer: A. With good hashing and low load factor, average lookup is O(1). Worst case (all
collisions) degrades to O(n).
Q4. In a singly linked list, which operation is O(1) at the head?
A) Insertion at head
B) Access by index
C) Search for a value
D) Insertion at tail without a tail pointer
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download PROGRAMMING FUNDAMENTALS & DATA STRUCTURES testbank and more Exams Database Programming in PDF only on Docsity!

CHAPTER 1: PROGRAMMING FUNDAMENTALS & DATA STRUCTURES (Q1–Q40)

Multiple-Choice Questions Q1. Which data structure follows Last-In-First-Out (LIFO) ordering? A) Queue B) Stack C) Linked list D) Hash table Answer: B. A stack is LIFO (push/pop at one end). A queue is FIFO; a linked list and hash table have no inherent LIFO order. Q2. Which data structure is best for implementing a breadth-first search (BFS) frontier? A) Stack B) Queue C) Priority queue D) Binary search tree Answer: B. BFS uses a FIFO queue to explore level by level. A stack gives DFS; a priority queue gives best-first/Dijkstra-style search. Q3. The average time complexity of lookup in a hash table with good hashing is: A) O(1) B) O(log n) C) O(n) D) O(n log n) Answer: A. With good hashing and low load factor, average lookup is O(1). Worst case (all collisions) degrades to O(n). Q4. In a singly linked list, which operation is O(1) at the head? A) Insertion at head B) Access by index C) Search for a value D) Insertion at tail without a tail pointer

Answer: A. Head insertion is O(1). Indexed access and search are O(n); tail insertion without a tail pointer is O(n). Q5. Which traversal of a binary search tree yields sorted ascending order? A) Preorder B) Inorder C) Postorder D) Level-order Answer: B. Inorder (left, root, right) yields sorted order for a BST. The others do not. Q6. The worst-case time complexity of search in an unbalanced binary search tree is: A) O(1) B) O(log n) C) O(n) D) O(n²) Answer: C. A degenerate (linked-list-like) BST has O(n) search. Balanced trees give O(log n). Q7. A min-heap guarantees which property? A) The root is the maximum element B) Each parent is ≤ its children C) Elements are fully sorted D) All leaves are at the same depth Answer: B. A min-heap maintains the heap property (parent ≤ children). It is not fully sorted and need not be complete in a strict level sense. Q8. Which sorting algorithm has O(n log n) worst-case time and is stable? A) Quicksort B) Merge sort C) Heapsort D) Selection sort Answer: B. Merge sort is O(n log n) worst case and stable. Quicksort is O(n²) worst case; heapsort is not stable.

Q13. In Big-O notation, O(2n + 5) simplifies to: A) O(n) B) O(2n) C) O(n + 5) D) O(5) Answer: A. Constants and lower-order terms are dropped, leaving O(n). Q14. Which is the correct order of growth from slowest to fastest for large n? A) O(1) < O(log n) < O(n) < O(n log n) < O(n²) B) O(n) < O(1) < O(log n) < O(n²) < O(n log n) C) O(log n) < O(1) < O(n) < O(n²) < O(n log n) D) O(1) < O(n) < O(log n) < O(n²) < O(n log n) Answer: A. Standard growth order: constant < logarithmic < linear < linearithmic < quadratic. Q15. Binary search requires the input array to be: A) Randomly ordered B) Sorted C) Reversed D) Of even length Answer: B. Binary search halves the search space based on ordered comparisons, requiring a sorted array. Q16. What is the time complexity of binary search on a sorted array of n elements? A) O(1) B) O(log n) C) O(n) D) O(n log n) Answer: B. Each step halves the search interval, giving O(log n). Q17. Which of the following best describes a graph's adjacency matrix space complexity for V vertices?

A) O(V)

B) O(V²)

C) O(E)

D) O(log V) Answer: B. An adjacency matrix uses V × V entries, i.e., O(V²) space regardless of edges. Q18. Dijkstra's algorithm requires edge weights to be: A) Negative B) Non-negative C) Equal D) Imaginary Answer: B. Dijkstra assumes non-negative weights; negative edges require Bellman–Ford. Q19. Which algorithm finds shortest paths from a source with possibly negative edges (but no negative cycles)? A) Dijkstra B) Bellman–Ford C) BFS D) Prim Answer: B. Bellman–Ford handles negative edges and detects negative cycles. BFS only works for unweighted graphs; Prim builds MSTs. Q20. The height of a balanced binary tree with n nodes is approximately: A) n B) log₂ n C) n² D) √n Answer: B. Balanced trees have height ~log₂ n, enabling O(log n) operations. Short-Answer Questions Q21. Define a stack and give one real-world application. Answer: A LIFO structure supporting push/pop at one end. Applications: undo functionality, function call stack, expression evaluation.

Q29. Define a spanning tree. Answer: A subgraph that connects all vertices with no cycles; a minimum spanning tree has minimal total edge weight. Q30. Give two differences between a stack and a queue. Answer: Stack is LIFO with one access end; queue is FIFO with two ends (enqueue at rear, dequeue at front). Structured Questions Q31. Trace merge sort on the array [5, 2, 9, 1, 6]. Show splits and merges. Model answer:

  • Split: [5, 2] [9, 1, 6]
  • Further splits: [5] [2]; [9] [1, 6] → [9] [1] [6]
  • Merge: [2, 5]; [1, 6] and [9]; then [1, 6, 9]
  • Final merge: [1, 2, 5, 6, 9] Q32. Describe how a hash table with chaining handles collisions. Model answer:
  • Each bucket stores a linked list (or similar) of entries.
  • On collision, the new entry is appended to the bucket's chain.
  • Lookup hashes the key, then searches that bucket's chain.
  • Performance degrades with long chains; good hashing and load-factor management mitigate this. Q33. Compare Dijkstra's and Bellman–Ford algorithms. Model answer:
  • Dijkstra: non-negative weights, O((V+E) log V) with a binary heap, uses a greedy priority queue.
  • Bellman–Ford: allows negative edges (not negative cycles), O(VE), detects negative cycles. Scenario-Based Questions Q34. A web server must handle many incoming requests fairly in arrival order. Which data structure should be used? Answer: A FIFO queue (or deque). It processes requests in arrival order, ensuring fairness. Q35. A compiler needs to check whether parentheses in an expression are balanced. Which data structure is appropriate, and why? Answer: A stack. Push opening brackets, pop and match on closing brackets; a mismatch or leftover stack indicates imbalance. Q36. A navigation app must compute the shortest route where all road distances are positive. Which algorithm is appropriate, and what is its complexity? Answer: Dijkstra's algorithm with a min-heap, O((V+E) log V). Non-negative weights satisfy its requirement. Calculation Questions Q37. Compute the value of T(n) = 3n² + 4n + 7 as Big-O. Working: Drop lower-order terms and constants; dominant term is n². O(n²). Q38. Convert the binary number 10110₂ to decimal. Working: 1×2⁴ + 0×2³ + 1×2² + 1×2¹ + 0×2⁰ = 16 + 4 + 2 = 22. Q39. Convert the decimal number 156 to hexadecimal. Working: 156 ÷ 16 = 9 remainder 12 (C); 9 ÷ 16 = 0 remainder 9. Result = 9C₁₆. Q40. A balanced binary search tree has 1,023 nodes. What is its height (levels) approximately? Working: height ≈ log₂(1,023) ≈ 10 levels (0-indexed: 10 edges, 11 levels). ≈10 levels.

C) Tight bound (both upper and lower) D) Worst-case only Answer: C. Θ describes a function bounded both above and below asymptotically. Q44. Which sorting algorithm is generally fastest on average for random data? A) Bubble sort B) Quicksort C) Insertion sort D) Selection sort Answer: B. Quicksort has excellent average-case performance (O(n log n)) and low constant factors. Bubble, insertion, and selection sorts are O(n²) average. Q45. Which algorithm is best suited for finding the shortest path in an unweighted graph? A) Dijkstra B) BFS C) Bellman–Ford D) Floyd–Warshall Answer: B. BFS finds shortest paths in unweighted graphs by levels. Dijkstra and Bellman–Ford handle weighted graphs; Floyd–Warshall computes all-pairs paths. Q46. The recurrence T(n) = 2T(n/2) + O(n) solves to: A) O(n) B) O(n log n) C) O(n²) D) O(log n) Answer: B. By the master theorem (case 2), T(n) = O(n log n), as in merge sort. Q47. The recurrence T(n) = T(n−1) + O(1) solves to: A) O(1) B) O(log n) C) O(n) D) O(n²)

Answer: C. Each step decreases n by 1, yielding O(n) total work. Q48. Which algorithm design strategy does dynamic programming rely on? A) Divide and conquer only B) Overlapping subproblems and optimal substructure C) Greedy choice only D) Randomization only Answer: B. DP exploits overlapping subproblems and optimal substructure with memoization/tabulation. Q49. Which problem is a classic example of dynamic programming? A) Binary search B) 0/1 knapsack C) Linear search D) Bubble sort Answer: B. 0/1 knapsack has overlapping subproblems and optimal substructure, suited to DP. Q50. A greedy algorithm is appropriate when: A) A locally optimal choice leads to a global optimum B) The problem has overlapping subproblems C) The problem is undecidable D) All inputs are sorted Answer: A. Greedy algorithms work when a local optimum extends to a global optimum (e.g., activity selection, Huffman coding). Q51. Which graph algorithm builds a minimum spanning tree? A) Dijkstra B) Kruskal C) BFS D) Bellman–Ford Answer: B. Kruskal (and Prim) build MSTs. Dijkstra finds shortest paths; BFS is traversal.

C) If the ground is not wet, it did not rain D) It rains and the ground is not wet Answer: C. The contrapositive swaps and negates: ¬Q → ¬P. A is the converse; B is the inverse. Q57. De Morgan's law states: A) ¬(P ∧ Q) ≡ ¬P ∨ ¬Q B) ¬(P ∧ Q) ≡ ¬P ∧ ¬Q C) P → Q ≡ Q → P D) P ∨ Q ≡ P ∧ Q Answer: A. De Morgan: ¬(P ∧ Q) ≡ ¬P ∨ ¬Q and ¬(P ∨ Q) ≡ ¬P ∧ ¬Q. Q58. A set with n elements has how many subsets? A) n B) n² C) 2ⁿ D) n! Answer: C. Each element is in or out, giving 2ⁿ subsets. Q59. The number of permutations of 5 distinct items is: A) 5 B) 25 C) 120 D) 720 Answer: C. 5! = 120. 720 is 6!. Q60. The number of combinations of 5 items taken 2 at a time is: A) 10 B) 20 C) 5 D) 25 Answer: A. C(5,2) = 5!/(2!·3!) = 10.

Q61. Which of the following is a tautology? A) P ∧ ¬P B) P ∨ ¬P C) P → ¬P D) P ↔ ¬P Answer: B. P ∨ ¬P is always true (law of excluded middle). P ∧ ¬P is a contradiction. Q62. The Euclidean algorithm computes: A) The least common multiple B) The greatest common divisor C) A prime factorization D) The square root Answer: B. Euclidean algorithm computes GCD via repeated remainders. Q63. Modular arithmetic computes 17 mod 5 as: A) 2 B) 3 C) 1 D) 0 Answer: A. 17 ÷ 5 = 3 remainder 2, so 17 mod 5 = 2. Q64. A relation that is reflexive, symmetric, and transitive is: A) Partial order B) Equivalence relation C) Function D) Injection Answer: B. Reflexive + symmetric + transitive defines an equivalence relation. Q65. The pigeonhole principle states: A) If n items are placed in m containers with n > m, at least one container has ≥2 items B) Every set has a maximum

Q70. Which sorting algorithm is typically used as a subroutine in radix sort? A) Quicksort B) Counting sort C) Merge sort D) Heap sort Answer: B. Radix sort uses counting sort as a stable subroutine per digit. Q71. The complexity of multiplying two n × n matrices using the naive algorithm is: A) O(n) B) O(n²) C) O(n³) D) O(n log n) Answer: C. Naive matrix multiplication is O(n³). Strassen's improves the exponent. Q72. Which algorithm finds all-pairs shortest paths? A) Dijkstra B) Bellman–Ford C) Floyd–Warshall D) Kruskal Answer: C. Floyd–Warshall computes all-pairs shortest paths in O(n³). The others compute single-source or MST. Q73. The master theorem applies to recurrences of the form: A) T(n) = T(n−1) + O(1) B) T(n) = aT(n/b) + f(n) C) T(n) = T(n−1) + T(n−2) D) T(n) = O(1) Answer: B. Master theorem applies to divide-and-conquer recurrences T(n) = aT(n/b) + f(n). Q74. Which problem is decidable? A) Halting problem

B) Determining whether a program prints "hello" C) Determining whether a string is a valid integer D) Determining whether any program terminates on all inputs Answer: C. Parsing an integer is decidable. Halting and universal termination are undecidable; printing "hello" reduces to halting. Q75. A stack is used internally by: A) Iterative loops B) Function calls/recursion C) Hash tables D) Arrays Answer: B. The call stack tracks function calls and returns. Q76. Which sorting algorithm is in-place and O(n log n) in the worst case? A) Merge sort B) Heapsort C) Quicksort D) Counting sort Answer: B. Heapsort is in-place and O(n log n) worst case (not stable). Merge sort needs extra space; quicksort is O(n²) worst case. Q77. The average-case time for successful search in a balanced BST is: A) O(1) B) O(log n) C) O(n) D) O(n²) Answer: B. Balanced BSTs have O(log n) search. Q78. Which graph has an Eulerian circuit? A) Every vertex has even degree and the graph is connected B) Every vertex has odd degree

Q84. What is the halting problem, and what does its undecidability imply? Answer: Given a program and input, no algorithm can decide in general whether the program halts. It implies limits on program analysis and verification. Q85. Define a tautology and give an example. Answer: A formula true under every truth assignment; e.g., P ∨ ¬P. Q86. State the pigeonhole principle. Answer: If n items are placed into m containers with n > m, at least one container holds at least two items. Q87. What is amortized analysis? Give one example. Answer: Averaging the cost of operations over a sequence; e.g., dynamic array append is amortized O(1) despite occasional O(n) resizing. Q88. Differentiate an Eulerian circuit from a Hamiltonian cycle. Answer: Eulerian circuit visits every edge exactly once; Hamiltonian cycle visits every vertex exactly once. Q89. Define P, NP, and NP-complete. Answer: P — solvable in polynomial time; NP — verifiable in polynomial time; NP-complete — in NP and as hard as any NP problem under polynomial reductions. Q90. What is a recurrence relation, and what does it describe? Answer: An equation expressing T(n) in terms of smaller inputs; it describes the cost of a recursive algorithm. Structured Questions Q91. Using the master theorem, solve T(n) = 4T(n/2) + n. Model answer:

  • a = 4, b = 2, f(n) = n.
  • Compare f(n) with n^(log_b a) = n².
  • f(n) = O(n^(2−ε)) for ε = 1, so case 1 applies.
  • T(n) = Θ(n²). Q92. Explain how Dijkstra's algorithm works step by step. Model answer:
  1. Initialize distances: source = 0, others = ∞.
  2. Use a min-priority queue of vertices keyed by tentative distance.
  3. Extract-min; relax outgoing edges (if d[u] + w < d[v], update).
  4. Mark u finalized.
  5. Repeat until all vertices are finalized. Works for non-negative weights; complexity O((V+E) log V) with a binary heap. Q93. Prove that a tree with n vertices has n − 1 edges. Model answer:
  • A tree on n vertices is connected and acyclic.
  • Adding each edge in a spanning construction connects a new component; n − 1 edges are needed to connect n vertices.
  • Any extra edge creates a cycle; fewer edges leave it disconnected.
  • Hence exactly n − 1 edges. Q94. Describe how dynamic programming solves the 0/1 knapsack problem. Model answer:
  • Define dp[i][w] = max value using first i items with capacity w.
  • Recurrence: dp[i][w] = max(dp[i−1][w], dp[i−1][w−wᵢ] + vᵢ) if wᵢ ≤ w.
  • Base case: dp[0][w] = 0.