JUnit MCQ - Multiple Choice Questions and Answers

When diving into the world of Java development, one of the essential tools you'll likely encounter is JUnit. As one of the premier testing frameworks for Java, JUnit helps developers ensure that their code runs as intended. Whether you're revising for an interview, a certification exam, or merely looking to gauge your knowledge, this set of multiple-choice questions will provide a comprehensive overview. Let's dive in!

1. What is JUnit primarily used for?

a) Debugging
b) Profiling
c) Unit Testing
d) Continuous Integration

2. Which annotation is used to indicate a method as a test method in JUnit?

a) @Testify
b) @TestCase
c) @Testing
d) @Test

3. If a JUnit test method throws an exception, what will be the outcome?

a) Success
b) Compilation error
c) Failure
d) Ignored

4. Which of the following is NOT a core feature of JUnit?

a) Test suites
b) Test runners
c) Test profilers
d) Test listeners

5. How do you specify that a particular test method should be ignored during execution?

a) @IgnoreTest
b) @Skip
c) @Ignore
d) @Avoid

6. Which JUnit version introduced the use of annotations?

a) JUnit 3
b) JUnit 4
c) JUnit 5
d) JUnit 2

7. In which lifecycle method would you typically place setup code to run before every test method?

a) @BeforeAll
b) @AfterAll
c) @BeforeEach
d) @AfterEach

8. What does the @AfterEach annotation signify?

a) It runs after all test methods in the current test class.
b) It runs before each test method.
c) It runs after each test method.
d) It runs before all test methods in the current test class.

9. Which JUnit annotation allows parameterized tests?

a) @Parameters
b) @ParameterizedTest
c) @TestWithParams
d) @DataDrivenTest

10. How can you expect a specific exception to be thrown in a JUnit test?

a) Using the @ExpectException annotation.
b) Using assertThrows() method.
c) Using the expectException method on the test object.
d) By wrapping the code block in a try-catch.

11. How do you specify a timeout for a test method in JUnit?

a) Using the timeout attribute in the @Test annotation.
b) By calling the setTimeout() method on the test instance.
c) Using a separate @Timeout annotation before the test method.
d) By wrapping the test method inside a TimeLimited block.

12. In which version of JUnit was the Jupiter API introduced?

a) JUnit 3
b) JUnit 4
c) JUnit 5
d) JUnit 6

13. Which of the following assertions checks that a condition is true?

a) assertThat()
b) assertEquals()
c) assertTrue()
d) assertNotNull()

14. What is the purpose of the @DisplayName annotation in JUnit?

a) To display the name of the test runner.
b) To give a custom display name for the test method.
c) To specify the name of the test suite.
d) To rename the test method during runtime.

15. How can you group multiple test classes together in JUnit?

a) Using the @TestGroup annotation.
b) By placing them in the same package.
c) Using the @Suite annotation.
d) Grouping is not possible in JUnit.

16. Which method is used in JUnit to check if two objects are the same instance?

a) assertEquals()
b) assertThat()
c) assertSame()
d) assertInstance()

17. How do you specify that a method should be executed only once before any test methods in a test class?

a) @Before
b) @BeforeEach
c) @BeforeClass
d) @BeforeAll

18. Which version of JUnit introduced lambda functions for assertions?

a) JUnit 3
b) JUnit 4
c) JUnit 5
d) JUnit 6

19. Which assertion method is used to check if an object is null in JUnit?

a) assertNull()
b) assertNotNull()
c) assertZero()
d) assertExists()

20. Which of the following is NOT a type of assertion provided by JUnit?

a) Timeout assertions
b) Conditional assertions
c) Exception assertions
d) Repetition assertions

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare