Sample questions
Question 1Easy
In unit testing, an assertion is:
- A statement that checks actual output against the expected value
- A comment describing the test on every build
- A build configuration
- A database query
Answer: A. A statement that checks actual output against the expected value
Explanation
A failed assertion makes the test fail.
Step-by-step solution
- An assertion is a check inside a test.
- It compares the actual output with the expected value.
- If they differ, the assertion fails and the test fails.
- So the answer is option A.
Why the other options are wrong
- B. A comment describes the test; it does not check anything.
- C. A build configuration is set up in the build tool, not in assertions.
- D. A database query fetches data; it is not an assertion.
Tip: An assertion is the line that decides pass or fail: actual value against expected value.
Question 2Easy
Which JUnit annotation marks a method as a test (JUnit 5)?
- @Run
- @Check
- @Test
- @Assert
Answer: C. @Test
Explanation
@Test from org.junit.jupiter.api marks a test method.
Step-by-step solution
- JUnit uses @Test.
- The others don't exist.
- So @Test.
Why the other options are wrong
- A. Not a JUnit annotation.
- B. Not a JUnit annotation.
- D. Assertions are methods, not annotations.
Question 3Easy
Code coverage tools measure:
- How many bugs the code is known to have
- How fast the tests run on the server
- How many users run the code in production
- How much code the tests execute
Answer: D. How much code the tests execute
Explanation
Coverage shows which lines or branches run during testing, not the quality of the assertions.
Step-by-step solution
- It tracks execution.
- Not correctness.
- Executed code share.
Why the other options are wrong
- A. Coverage does not count bugs.
- B. Speed is separate.
- C. Usage is a production metric.
Question 4Easy
What does the following pytest test report?
def add(a, b):
return a + b
def test_add():
assert add(2, 3) == 6
- The test passes
- A syntax error occurs
- The test is skipped
- The test fails because 5 is not equal to 6
Answer: D. The test fails because 5 is not equal to 6
Explanation
add(2, 3) returns 5, so assert 5 == 6 raises AssertionError and pytest marks the test as failed.
Step-by-step solution
- add returns 5.
- Assertion compares to 6.
- Mismatch, so failure.
Why the other options are wrong
- A. 2 + 3 gives 5, so the assertion is false.
- B. The code is valid Python.
- C. Functions named test_ are collected, not skipped.
Question 5Medium
What is the correct order of the TDD cycle?
- Red, green, refactor
- Green, red, refactor
- Refactor, red, green
- Red, refactor, green
Answer: A. Red, green, refactor
Explanation
First write a failing test (red), make it pass (green), then clean the code (refactor).
Step-by-step solution
- Failing test first.
- Pass it.
- Clean up.
Why the other options are wrong
- B. You can't start with passing code.
- C. Refactor comes after passing.
- D. Refactoring before green is unsafe.
Question 6Medium
In test-driven development (TDD), the correct cycle is:
- Red, green, refactor
- Green, red, deploy
- Refactor, red, green
- Code, release, test
Answer: A. Red, green, refactor
Explanation
Write a failing test, make it pass, then clean up the code.
Step-by-step solution
- TDD starts by writing a test that fails, which is red.
- Then the code is written just enough to make the test pass, which is green.
- Then the code is cleaned up without changing behaviour, which is refactor.
- So the cycle is red, green, refactor, option A.
Why the other options are wrong
- B. Deploying is not part of the TDD cycle, and green comes after red.
- C. Refactoring comes last, after a failing and then a passing test.
- D. Writing code before the test and releasing it is the opposite of TDD.
Tip: TDD: red (failing test), green (make it pass), refactor (clean up).
Question 7Medium
Which statement about a mock object is correct?
- It replaces a dependency and can verify its calls
- It stores production data permanently in a large file
- It is the code that is under test
- It is a type of load generator tool
Answer: A. It replaces a dependency and can verify its calls
Explanation
A mock stands in for a collaborator such as an email service and lets the test assert interactions.
Step-by-step solution
- Replaces dependency.
- Records calls.
- Verify interactions.
Why the other options are wrong
- B. Mocks usually hold canned responses only for tests.
- C. The code under test is the SUT.
- D. Mocks do not generate load.
Question 8Medium
A mock object in unit testing is used to:
- Increase code coverage artificially in most projects
- Generate the UI
- Replace a dependency and verify interactions with it
- Store the test report
Answer: C. Replace a dependency and verify interactions with it
Explanation
Mocks isolate the unit and can check that calls happened.
Step-by-step solution
- A mock stands in for a real dependency, such as a database or an API.
- It lets the test control the dependency's responses.
- It can also check that the unit called the dependency the right way.
- So the answer is option C.
Why the other options are wrong
- A. A mock does not inflate coverage on its own; it is a tool for isolation.
- B. A mock does not generate a UI.
- D. A test report is stored by the test runner, not by a mock.
Tip: Mocks replace dependencies so a unit can be tested alone, and can verify the calls it made.
Question 9Hard
Which code could reach 100% line coverage but still miss a defect?
- A test that asserts every output carefully
- A test that runs the code but asserts nothing
- A suite that has a mutation score of 100 percent
- A test suite that also checks all boundaries
Answer: B. A test that runs the code but asserts nothing
Explanation
Coverage only shows that code ran. Without assertions a wrong result is not noticed.
Step-by-step solution
- Coverage counts execution.
- No assertion means no check.
- Defect slips by.
Why the other options are wrong
- A. Assertions would catch the defect.
- C. A perfect mutation score means strong tests.
- D. Boundary tests help find defects.
Question 10Hard
What does this unittest-style test do?
from unittest.mock import Mock
svc = Mock()
svc.send.return_value = True
ok = svc.send("a@b.com")
svc.send.assert_called_once_with("b@c.com")
- It passes without any error being raised
- It raises TypeError because a Mock object is not callable
- AssertionError: the call used a different argument
- It returns True and nothing else happens
Answer: C. AssertionError: the call used a different argument
Explanation
The mock recorded a call with "a@b.com". assert_called_once_with("b@c.com") compares arguments and raises AssertionError.
Step-by-step solution
- send called with a@b.com.
- Assertion expects b@c.com.
- They differ, so it fails.
Why the other options are wrong
- A. The call used a@b.com but the check expects b@c.com.
- B. Mock allows any attribute and call.
- D. assert_called_once_with raises when the check fails.
Practise all 17 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →