Question 1
Which framework is most commonly used for unit testing in Java?
JUnit
TestNG
Mockito
Selenium
Answer:
JUnit
Question 2
In JUnit 5, which annotation is used to indicate a test method?
@TestCase
@RunWith
@Test
@Before
Answer:
@Test
Question 3
Which JUnit annotation is used to execute some code before each test method?
@BeforeAll
@After
@BeforeEach
@BeforeTest
Answer:
@BeforeEach
Question 4
Which method in JUnit is used to check if two objects are equal?
assertSame
assertTrue
assertEquals
assertNotNull
Answer:
assertEquals
Question 5
Which of the following is a mocking framework often used in Java unit tests?
TestNG
Mockito
JUnit
Cucumber
Answer:
Mockito
Question 6
In Mockito, which method is used to create a mock object?
mock()
createMock()
mockObject()
newMock()
Answer:
mock()
Question 7
What does the @Mock annotation do in Mockito?
It creates a real object
It creates a mock object
It verifies a method call
It initializes a mock object
Answer:
It creates a mock object
Question 8
Which JUnit annotation is used to run a piece of code after all tests in the test class have been run?
@AfterEach
@AfterAll
@AfterTest
@After
Answer:
@AfterAll
Question 9
In TestNG, which annotation is equivalent to JUnit's @BeforeEach?
@BeforeTest
@BeforeMethod
@BeforeClass
@BeforeSuite
Answer:
@BeforeMethod
Question 10
Which Mockito method is used to verify that a method was called with specific arguments?
verify()
check()
assert()
confirm()
Answer:
verify()
Question 11
In JUnit 5, which annotation is used to disable a test method?
@Ignore
@Disabled
@Skip
@Deactivate
Answer:
@Disabled
Question 12
Which of the following is not a lifecycle method in JUnit 5?
@BeforeEach
@AfterEach
@BeforeClass
@BeforeAll
Answer:
@BeforeClass
Question 13
Which of the following assertions is used to check if a condition is false in JUnit?
assertTrue()
assertFalse()
assertNull()
assertNotNull()
Answer:
assertFalse()
Question 14
What is the primary purpose of unit testing?
To test the entire application as a whole
To test individual units or components in isolation
To test the user interface
To test the performance of the application
Answer:
To test individual units or components in isolation
Question 15
In Mockito, which method is used to return a specific value when a method is called?
when().thenReturn()
doReturn().when()
mock().thenReturn()
verify().thenReturn()
Answer:
when().thenReturn()
Question 16
Which JUnit annotation is used to provide a timeout for a test method?
@Timeout
@Test(timeout = 1000)
@TimeLimit
@Test(timeout = 1)
Answer:
@Timeout
Question 17
Which of the following is not a valid JUnit assertion?
assertEquals()
assertNotNull()
assertThrows()
assertEmpty()
Answer:
assertEmpty()
Question 18
Which JUnit 5 annotation is used to run a test multiple times?
@Repeat
@RepeatedTest
@LoopTest
@TestRepeat
Answer:
@RepeatedTest
Question 19
In TestNG, which annotation is used to indicate that a method should be executed before any test methods in the current class?
@BeforeTest
@BeforeClass
@BeforeMethod
@BeforeSuite
Answer:
@BeforeClass
Question 20
In Mockito, how can you mock a method to throw an exception?
when(methodCall).thenThrow(new Exception())
doThrow(new Exception()).when(methodCall)
throwException(new Exception()).when(methodCall)
when(methodCall).throw(new Exception())
Answer:
when(methodCall).thenThrow(new Exception())
function capitalizeWords(sentence) {
ReplyDeletereturn sentence.replace(/\b(\(?\w)/g, function(match) {
return match.toUpperCase();
});
}
// Unit tests
function runTests() {
const tests = [
{ input: "hello world", expected: "Hello World" },
{ input: "(hello) world", expected: "(Hello) World" },
{ input: "this is a test", expected: "This Is A Test" },
{ input: "test with (brackets) inside", expected: "Test With (Brackets) Inside" },
{ input: "(open brackets) and [square brackets]", expected: "(Open Brackets) And [Square Brackets]" },
{ input: "singleWord", expected: "SingleWord" },
{ input: "", expected: "" },
{ input: "123 numbers", expected: "123 Numbers" },
{ input: "punctuation, marks!", expected: "Punctuation, Marks!" }
];
tests.forEach(({ input, expected }, index) => {
const result = capitalizeWords(input);
console.assert(result === expected, `Test ${index + 1} failed: expected "${expected}", got "${result}"`);
});
console.log("All tests passed!");
}
// Run the tests
runTests();
Explanation:
Function capitalizeWords:
The function uses a regular expression to find word boundaries (\b) followed by an optional opening bracket ((?) and a word character \w.
The match is then transformed by converting it to uppercase using the toUpperCase() method.
Unit Tests:
A series of test cases are defined to validate the function. Each test case consists of an input sentence and the expected capitalized result.
The tests include various scenarios like normal sentences, sentences with different types of brackets, numbers, and punctuation.
The console.assert method is used to verify that the result of capitalizeWords(input) matches the expected result. If the assertion fails, it will log an error message specifying which test failed.
Running the Tests:
The runTests function executes all the test cases, and if all assertions pass, it will log "All tests passed!" to the console.
This implementation ensures that each word in a sentence is capitalized correctly, even if the word is preceded by a bracket.