(Units 1-7) - Questions with Correct Answers
100 questions
1. O(log n) algorithms typically:
A. Check every element
B. Compare every pair
C. Generate all subsets
D. Divide the problem in half at each step
Correct Answer: D. Divide the problem in half at each step
Rationale: Halving.
2. Generating all subsets of n items has complexity:
A. O(n)
B. O(2^n)
C. O(n^2)
D. O(log n)
Correct Answer: B. O(2^n)
Rationale: Power set.
3. Space complexity measures:
A. The number of CPU cores
B. The amount of memory an algorithm uses relative to input size
C. Disk speed
D. Lines of code
Correct Answer: B. The amount of memory an algorithm uses relative to input size
Rationale: Memory growth.
4. The worst case of an algorithm is:
A. The input that causes the maximum running time
B. The fastest input
C. The average input
D. The smallest input only
Correct Answer: A. The input that causes the maximum running time
Rationale: Upper performance limit.
5. Removing the last element of a Python list with pop() is:
A. O(n)
B. O(log n)
C. O(1)
D. O(n^2)
Correct Answer: C. O(1)
Rationale: No shifting.
6. Inserting at the beginning of a Python list is:
A. O(n)
B. O(1)
C. O(log n)
D. O(n^2)
Correct Answer: A. O(n)
Rationale: All elements shift.
,7. Which growth rate is the slowest (most efficient) for large n?
A. O(n)
B. O(n log n)
C. O(log n)
D. O(n^2)
Correct Answer: C. O(log n)
Rationale: Logarithmic grows slowly.
8. Polynomial time algorithms have complexity:
A. O(2^n)
B. O(n^k) for some constant k
C. O(n!)
D. Undefined
Correct Answer: B. O(n^k) for some constant k
Rationale: Considered tractable.
9. An algorithm that creates a copy of an input list of size n uses additional space of:
A. O(n)
B. O(1)
C. O(log n)
D. O(n^2)
Correct Answer: A. O(n)
Rationale: Linear extra memory.
10. Big-O notation describes:
A. The exact running time in seconds
B. An upper bound on an algorithm's growth rate as input size increases
C. The memory address of variables
D. The number of lines of code
Correct Answer: B. An upper bound on an algorithm's growth rate as input size increases
Rationale: Asymptotic upper bound.
11. Tail recursion in Python:
A. Is always optimized
B. Uses no stack
C. Is not optimized, so deep recursion can exceed the recursion limit
D. Is faster than loops always
Correct Answer: C. Is not optimized, so deep recursion can exceed the recursion limit
Rationale: Default recursion limit ~1000.
12. Why are constants dropped in Big-O?
A. Big-O focuses on growth rate as n becomes large
B. Constants are always zero
C. Computers ignore constants
D. It is a convention for small inputs
Correct Answer: A. Big-O focuses on growth rate as n becomes large
Rationale: Asymptotic behavior.
, 13. Which growth rate is the fastest (least efficient) for large n?
A. O(2^n)
B. O(n^3)
C. O(n^2)
D. O(n log n)
Correct Answer: A. O(2^n)
Rationale: Exponential.
14. O(n!) complexity is typical of:
A. Binary search
B. Hash table lookup
C. Brute-force evaluation of all permutations, such as exact TSP by enumeration
D. Linear search
Correct Answer: C. Brute-force evaluation of all permutations, such as exact TSP by
enumeration
Rationale: Factorial growth.
15. A loop that doubles a counter each iteration until it reaches n runs:
A. O(n) times
B. O(n^2) times
C. O(log n) times
D. O(1) times
Correct Answer: C. O(log n) times
Rationale: Logarithmic iterations.
16. The 'self' parameter refers to:
A. The class itself always
B. A global variable
C. The parent module
D. The current instance of the class
Correct Answer: D. The current instance of the class
Rationale: Instance reference.
17. Inheritance allows a class to:
A. Delete parent classes
B. Prevent instantiation
C. Reuse and extend another class's attributes and methods
D. Hash values
Correct Answer: C. Reuse and extend another class's attributes and methods
Rationale: Subclassing.
18. A Python tuple is:
A. An immutable ordered sequence
B. A mutable ordered sequence
C. An unordered set
D. A key-value store
Correct Answer: A. An immutable ordered sequence
Rationale: Cannot change.