CS160 MIDTERM ACTUAL EXAM PREP
2026 ALL QUESTIONS AND CORRECT
DETAILED ANSWERS ALREADY A
GRADED WITH EXPERT FEEDBACK
|NEW AND REVISED
1. In most statically typed languages, which error is caught at compile time
rather than at run time?
A. Null pointer dereference.
B. Type mismatch between variable and assigned value.
C. Division by zero with dynamic input.
D. Array index out of bounds when index computed at runtime.
Static type systems detect incompatible types during compilation,
preventing assignment/type errors before execution.
2. Which data structure provides O(1) average-time amortized push and pop at
the end?
A. Singly linked list head operations.
B. Binary search tree.
C. Dynamic array (e.g., amortized array list) append/pop at end.
D. Heap.
Dynamic arrays resize occasionally so amortized cost per append/pop at
the end is O(1).
3. Which sorting algorithm has worst-case time complexity O(n^2) and is in-
place?
A. Merge sort.
B. Insertion sort (and selection sort/quick sort worst case).
C. Heap sort.
D. Tim sort.
,2|Page
Insertion sort performs O(n^2) comparisons/swaps in worst case and sorts
in place using constant extra memory.
4. What is the output of this pseudocode?
for i in 1..3:
for j in 1..i:
print(i)
A. 1 2 3
B. 1 2 2 3 3 3
C. 1 1 2 2 3 3
D. 3 3 3 2 2 1
Inner loop prints i j times: prints one 1, two 2s, three 3s yielding 1 2 2 3 3 3.
5. Which Big-O characterizes binary search on a sorted array of length n?
A. O(n)
B. O(n log n)
C. O(log n)
D. O(1)
Binary search halves the search range each step, giving logarithmic time
complexity.
6. Which structure is best for implementing breadth-first search on a graph?
A. Stack.
B. Priority queue.
C. Queue.
D. Hash table.
BFS explores neighbors level by level using a FIFO queue to process
vertices in order discovered.
7. In object-oriented programming, which principle restricts direct access to
internal object state?
A. Polymorphism.
B. Encapsulation.
C. Inheritance.
D. Abstraction only.
Encapsulation hides internal details and exposes a public interface,
controlling access to state.
8. Which SQL clause filters rows after grouping with GROUP BY?
A. WHERE
B. JOIN
C. HAVING
,3|Page
D. ORDER BY
HAVING applies predicates to aggregated groups; WHERE filters
individual rows before grouping.
9. What is the primary purpose of a hash function in a hash table?
A. To sort keys.
B. To map keys to bucket indices uniformly for fast lookup.
C. To compress data for storage.
D. To encrypt sensitive values.
Hash functions compute indices to store and retrieve keys in near-constant
time when collisions are minimal.
10.Which of the following best describes stack overflow in recursion?
A. Only when integer overflow occurs.
B. When the call stack grows beyond available memory due to too many
nested calls.
C. When heap memory is exhausted.
D. When variables exceed CPU registers.
Excessive recursive depth consumes stack frames until stack memory is
exhausted causing overflow.
11.Which algorithmic paradigm divides the problem into subproblems, solves
them, and combines results?
A. Greedy.
B. Divide and conquer.
C. Dynamic programming only.
D. Backtracking.
Divide and conquer splits the problem (e.g., merge sort, quicksort), solves
recursively, then merges solutions.
12.Which complexity class describes problems solvable in polynomial time?
A. NP-complete.
B. EXP.
C. P.
D. Undecidable.
Class P contains decision problems that have deterministic polynomial-
time algorithms.
13.In a singly linked list, deleting a node given only a pointer to that node (not
the head) can be done by:
A. Rewiring previous pointer without traverse.
B. Copying next node's data into the node and removing next node (if
next exists).
C. Immediately freeing memory without modification.
D. Swapping head and node.
, 4|Page
Without head access, you can overwrite current node with next node and
delete next—cannot if node is tail.
14.Which HTTP method is idempotent by definition?
A. POST only.
B. GET and PUT (both idempotent; POST is not).
C. CONNECT.
D. TRACE only.
GET and PUT are idempotent: repeated identical requests have same
effect; POST may create multiple resources.
15.Which statement about pointers/references is true?
A. References allow pointer arithmetic in safe languages.
B. Pointers store memory addresses; dereferencing accesses the value at
that address.
C. References and values are identical at runtime in all languages.
D. Pointers are always type-agnostic without casts.
Pointers hold addresses; dereferencing reads/writes memory at the pointed
location; semantics vary by language.
16.What does SQL injection exploit?
A. Poor hashing of passwords.
B. Lack of input validation allowing attacker-supplied SQL to be
executed.
C. Disk fragmentation.
D. Incorrect timestamps.
SQL injection occurs when unsanitized user input is concatenated into
SQL, enabling arbitrary queries.
17.Which of these is a stable sorting algorithm?
A. Heapsort.
B. Quick sort.
C. Merge sort (stable when implemented to preserve order).
D. Selection sort.
Merge sort preserves relative order of equal elements if implemented
appropriately, hence stable.
18.In a CPU scheduling context, which algorithm minimizes average waiting
time for nonpreemptive scheduling with known burst times?
A. Round robin.
B. Shortest Job First (SJF).
C. First-come, first-served always.
D. Priority with random ties.
SJF schedules shortest job next, minimizing average waiting time when
job lengths are known.