The last assertion expects 5 but function returns 4, so it fails.
Final Answer:
The last assertion fails -> Option A
Quick Check:
4 != 5 causes failure [OK]
Hint: Check each assert expected vs actual carefully [OK]
Common Mistakes:
Assuming all assertions pass without checking values
Confusing multiplication results
Ignoring the last assertion
4. Identify the error in the following Python unit test code:
def divide(a, b):
return a / b
assert divide(10, 2) == 5
assert divide(5, 0) == 0
medium
A. The function should return integer only
B. Division by zero causes runtime error
C. The first assertion is incorrect
D. Assertions are missing parentheses
Solution
Step 1: Analyze function behavior
Function divides a by b. Dividing by zero is not allowed and causes an error.
Step 2: Check assertions
First assertion divides 10 by 2, which is valid. Second assertion divides 5 by 0, which causes a runtime ZeroDivisionError.
Final Answer:
Division by zero causes runtime error -> Option B
Quick Check:
Divide by zero error in second assert [OK]
Hint: Watch for zero division in tests [OK]
Common Mistakes:
Ignoring division by zero error
Thinking assertions need parentheses in Python
Assuming function returns only integers
5. You want to write a unit test for a function that returns the maximum number from a list. Which test case below best checks the function handles an empty list correctly?
hard
A. assert max_number([-1, -2]) == -1
B. assert max_number([1, 2, 3]) == 3
C. assert max_number([0]) == 0
D. assert max_number([]) == None
Solution
Step 1: Understand the edge case
An empty list has no maximum value, so the function should handle this gracefully, often by returning None or raising an error.
Step 2: Evaluate test cases
assert max_number([]) == None tests the empty list case expecting None, which is appropriate. Other options test normal lists, not empty.
Final Answer:
assert max_number([]) == None -> Option D
Quick Check:
Empty list test expects None [OK]
Hint: Test empty inputs with None or error expected [OK]
Common Mistakes:
Not testing empty list edge case
Using normal list tests for empty case
Assuming empty list returns zero
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.