Mettl Coding Fundamentals and Logic 5 — Questions and Answers
Question 1: Which of the following correctly defines recursion?
- A loop that iterates a fixed number of times
- A function that calls itself to solve a smaller instance (Correct answer)
- A method that returns multiple values
- A class that inherits from itself
Correct answer: A function that calls itself to solve a smaller instance
Recursion is when a function calls itself with a smaller input, converging toward a base case.
Question 2: What is the result of the bitwise AND operation: 12 & 10?
- 14
- 8 (Correct answer)
- 2
- 22
Correct answer: 8
12 = 1100₂ and 10 = 1010₂; AND gives 1000₂ = 8.
Question 3: A stack currently holds [1, 2, 3] (3 is on top). After pop(), push(4), pop(), what is on top?
- 2 (Correct answer)
- 4
- 1
- 3
Correct answer: 2
pop() removes 3 → stack [1,2]; push(4) → [1,2,4]; pop() removes 4 → top is 2.
Question 4: Which of the following is a valid reason to use a queue over a stack?
- Undo/redo operations
- Depth-first search
- Print job scheduling (Correct answer)
- Parsing matching parentheses
Correct answer: Print job scheduling
Print job scheduling uses FIFO ordering — the first job submitted is the first printed.
Question 5: What is the output of this code snippet? def mystery(n): return n * (n + 1) // 2 print(mystery(4))
- 8
- 10 (Correct answer)
- 12
- 6
Correct answer: 10
mystery(4) = 4 * 5 // 2 = 20 // 2 = 10, the sum of integers 1 through 4.
Question 6: Which of the following statements about immutable objects is correct?
- Their value can be changed after creation
- They cannot be used as dictionary keys
- Their state cannot be modified after creation (Correct answer)
- They are always slower than mutable objects
Correct answer: Their state cannot be modified after creation
Immutable objects have a fixed state that cannot be altered after they are created.
Question 7: What does short-circuit evaluation mean in logical expressions?
- The CPU skips instructions for speed optimization
- Evaluation stops as soon as the result is determined (Correct answer)
- Both operands are always evaluated before returning
- The expression always returns a Boolean
Correct answer: Evaluation stops as soon as the result is determined
Short-circuit evaluation stops at the first operand that determines the outcome, e.g., False AND X skips X.
Which of the following correctly defines recursion?