Java MCQ Set 1
1. The ______ is a container used to gather tests for the purpose of grouping and invocation.
a) Result
b) TestCase
c) Suite
d) Test
Answer
Answer: c [Reason:] JUnit provides test Suite to facilitate the task of running more than one test file.
2. What happens if the tester does not define a Suite?
a) The test runner automatically creates a Suite
b) Compilation Error
c) Every test fails
d) Every test passes
Answer
Answer: a [Reason:] The default Suite scans the test class for any methods annotated with @Test. Internally, the default Suite creates an instance of the test class for each @Test method. JUnit then executes every @Test method independently from the others to avoid potential side effects.
3. The Suite object is a _____ that executes all of the @Test annotated methods in the test class.
a) Result
b) FolderConfigurationTest
c) FileConfigurationTest
d) Runner
Answer
Answer: d [Reason:] Suite is under org.junit.runners.Suite, Suite objects are Runners.
4. Suite class is the JUnit 4 equivalent of what feature of JUnit 3.8.x?
a) static Test suite() method
b) Test suite() method
c) static void suite() method
d) void suite() method
Answer
Answer: a [Reason:] The suite is made accessible to a TestRunner tool with the static method suite() which returns a Test suite.
5. For a Suite class, the @RunWith annotation has the value of which class?
a) org.junit.runners.class
b) org.junit.Suite.class
c) org.runners.Suite.class
d) org.junit.runners.Suite.class
Answer
Answer: d [Reason:] “org.junit.runners.Suite.class” contains the definition for the Suite class and a the declaration is @RunWith(value=org.junit.runners.Suite.class).
6. Which annotation is used to list all the classes in a suite?
a) @RunWith
b) @SuiteClasses
c) @Classses
d) @SuiteClass
Answer
Answer: b [Reason:] The SuiteClasses annotation specifies the classes to be run when a class annotated with @RunWith(Suite.class) is run. The formal definition of the annotation is “Annotation Type Suite.SuiteClasses”.
7. If we want to run test files Test1 and Test2 together, the @SuiteClasses annotation will be?
a) @SuiteClasses(value={Test1.class,Test2.class})
b) @SuiteClasses(value=All)
c) @SuiteClasses(Test1, Test2);
d) @SuiteClasses()
Answer
Answer: a [Reason:] The classes we want to test together are given as a tuple to the value of the SuiteClasses annotation.
8. JUnit Suites are independent of the capability of the ______ system.
a) Run
b) Class
c) Test
d) Build
Answer
Answer: d [Reason:] JUnit Suites are useful to organize tests in Java, independent of the capability of the build system, because it’s common for someone or a group other than the developers to maintain builds.
9. Will the second and third assert failures be reported?
-
public class TestFile
-
{
-
@Test
-
public void testMethod()
-
{
-
assertTrue(false);
-
assertTrue(false);
-
assertTrue(false);
-
}
-
}
a) Yes
b) No
Answer
Answer: b [Reason:] JUnit only reports the first failure in a single test.
10. Which attribute is added to the @Test annotation so that the test passes when an expected exception is thrown?
a) exception
b) throws
c) expected
d) expectedException
Answer
Answer: c [Reason:] @Test(expected=IndexOutOfBoundsException.class) is the example of a method expecting the IndexOutOfBoundsException.
Java MCQ Set 2
1. What are Parameterised tests used for in JUnit?
a) Run a test many times with different sets of parameters
b) Run a test with no parameters
c) Run a test with only String parameters
d) Run a test once with fixed set of parameters
Answer
Answer: a [Reason:] The Parameterized test runner allows to run a test many times with different sets of parameters.
2. A parameterised test class must carry which annotation?
a) @Test
b) @ParameterisedClass
c) @Runwith
d) @Class
Answer
Answer: c [Reason:] A class that is annotated with @RunWith or extends a class that is annotated with @RunWith, JUnit will invoke the class it has referenced to run the tests in that class instead of the runner that is built into JUnit.
3. The test class must carry the @RunWith annotation with the ______ class as its argument.
a) Default
b) Parameterised
c) Super
d) Inherited
Answer
Answer: b [Reason:] “Parameterised.class” should be the argument. The full syntax is @RunWith(value=Parameterized.class).
4. What will be the outcome for the following piece of code?
-
//The JUnit files are imported
-
public class TestClass {
-
@Test
-
public void testingMethod() {
-
String message = “Test”;
-
assertEquals(3,message.length());
-
}
-
}
a) Compilation Error
b) Runtime Error
c) Test Ran with Success
d) Test Ran with Failure
Answer
Answer: d [Reason:] The string “Test” has a length of 4 whereas the expected value is 3.
5. What does the fail() method do in JUnit?
a) Throws an assertion error unconditionally
b) Calls the default constructor
c) Outputs the message “Fail” to the console
d) Pauses the test for 1 second
Answer
Answer: a [Reason:] The method throws an assertion error unconditionally. This might be helpful to show an incomplete test (maybe still being worked upon) or to ensure that an expected exception is thrown.
6. Which annotation must be used to define suite classes?
a) @RunWith
b) @SuiteClasses
c) @Suite
d) @SuiteClass
Answer
Answer: b [Reason:] The SuiteClasses annotation is used to specify the classes to be run when a class that is annotated with @RunWith(Suite.class) has been run.
7. When is the tearDown() method called in JUnit?
a) After all the tests have run
b) At the beginning of every test case
c) After each test case has run
d) At the beginning of the first test case
Answer
Answer: c [Reason:] The tearDown() method is called after the execution of every @Test method.
8. What does the assertTrue(“message”,A) do?
a) Asserts that the condition A is true
b) Asserts that “message” = A
c) Asserts that A contains “message”
d) Asserts that the condition A is false
Answer
Answer: a [Reason:] assertTrue requires A to be a boolean expression and the “message” is displayed.
9. How can a method be made to run before the execution of every test case?
a) Annotate the method with @Before
b) Prefix the method name with startfirst
c) Annotate the method with a @BeforeClass
d) Such a method cannot be made
Answer
Answer: a [Reason:] Annotating a method with @Before implies that method to be run before the every test method. The @Before methods of superclasses are run before those of the current class.
10. Which method from TestCase class returns the name of a Test case?
a) String testCaseName()
b) String getTest()
c) String getTestCaseName()
d) String getName()
Answer
Answer: d [Reason:] getName() returns a string denoting the name of the TestCase.
Java MCQ Set 3
1. ________ are the first type of tests any application should have.
a) Functional Tests
b) Unit Tests
c) Integration Tests
d) Stress Tests
Answer
Answer: b [Reason:] The main goal of unit testing is to verify that your application works as expected and to catch bugs early.
2. Unit tests allow greater test _______ than functional tests.
a) Coverage
b) Redundancy
c) Prowess
d) Accuracy
Answer
Answer: a [Reason:] Unit tests can easily simulate error conditions, which is extremely difficult to do with functional tests.
3. Functional tests are more _______ compared to unit tests.
a) Fine grained
b) Accurate
c) Time consuming
d) Coarse grained
Answer
Answer: d [Reason:] Functional Tests need the full application (or a good part of it) to be ready before it can be tested.
4. Unit Tests can detect ____
a) Regressions
b) Quality Check
c) Database Errors
d) Enforced Error
Answer
Answer: a [Reason:] A unit test tells that a specific method is failing for a specific reason.
5. Agile methodologists favour writing code in _______ slices to produce a working use case.
a) Horizontal
b) Small
c) Diagonal
d) Vertical
Answer
Answer: d [Reason:] As opposed to writing code in horizontal slices to provide services layer by layer.
6. To retain a design across features, agile methodologies encourage ________ to adapt the code base as needed.
a) Changing
b) Adapting
c) Refactoring
d) Duplicating
Answer
Answer: c [Reason:] Unit tests tells when and where code breaks and gives the confidence to refactor.
7. The agile methodologies try to lower project risks by providing the ability to cope with ________
a) Change
b) Refactoring
c) Inefficiency
d) Redundancy
Answer
Answer: a [Reason:] Agile methodologies allow and embrace change by standardizing on quick iterations.
8. _____ is a principle of extreme programming (XP) that states that a functionality should not be added until deemed necessary.
a) KISS
b) MoSCoW Method
c) Overengineering
d) YAGNI
Answer
Answer: d [Reason:] The foundation on which YAGNI rests is a solid bed of unit tests.
9. YAGNI stands for _______
a) Your agile going now inside
b) You aren’t gonna need it
c) You are gonna need it
d) You are given no information
Answer
Answer: b [Reason:] YAGNI being dependent on supporting practices is part of the original definition of XP.
10. YAGNI is a principle behind the XP practice of ___
a) KISS
b) MoSCoW
c) WIB
d) DTSTTCPW
Answer
Answer: d [Reason:] DTSTTCPW is the acronym for “do the simplest thing that could possibly work”.
Java MCQ Set 4
1. ____ is a library that contains a lot of helpful matcher objects , ported in several languages.
a) Java
b) Pygame
c) AllenWake
d) Hamcrest
Answer
Answer: d [Reason:] Hamcrest is a library that contains a lot of helpful matcher objects (known also as constraints or predicates).
2. Hamcrest is not a ________ framework in itself.
a) Matching
b) Testing
c) Asserting
d) Checking
Answer
Answer: b [Reason:] Hamcrest helps you declaratively specify simple matching rules.
3. The package for the assertThat() function is?
a) org.hamcrest.CoreMatchers.assertThat
b) org.junit.Assert.assertThat
c) org.junit.JunitMatchers.assertThat
d) org.junit.hasItem.assertThat
Answer
Answer: b [Reason:] The asserThat() function is a part of the main JUnit core assert functions.
4. ________ Hamcrest matcher can be used to match absolutely everything.
a) Is
b) AnyOf
c) Anything
d) Not
Answer
Answer: c [Reason:] Matches absolutely anything. Useful in some cases where one wants to make the assert statement more readable.
5. ______ is used only to improve the readability of the statements.
a) anything
b) is
c) sameInstance
d) nullValue
Answer
Answer: b [Reason:] The Is Hamcrest matcher does not add any more functionality other than improving readability.
6. Which Hamcrest matcher is just like the && operator?
a) Is
b) Anything
c) sameInstance
d) allOf
Answer
Answer: d [Reason:] Checks to see if all contained matchers match (just like the && operator).
7. ________ checks to see if any of the contained matchers match.
a) anyOf
b) allOf
c) instanceOf
d) notNull
Answer
Answer: a [Reason:] The anyOf Hamcrest matcher is just like the || operator.
8. _____ traverses the meaning of the contained matchers.
a) not
b) or
c) allOf
d) andAll
Answer
Answer: a [Reason:] The not Hamcrest matcher is used to traverse the meaning of matchers contained in the argument.
9. The instanceOf matcher is equivalent to ________
a) isCompatibleType
b) isCompatible
c) isInstance
d) isSame
Answer
Answer: a [Reason:] Match whether objects are of compatible type (are instances of one another).
10. _____ is used to test object identity.
a) instanceOf
b) isCompatibleType
c) type
d) sameInstance
Answer
Answer: d [Reason:] Checks whether it is the same instance of the object or not.
Java MCQ Set 5
1 Which Hamcrest core matcher checks if a number is equal to a number of some acceptable error?
a) closeTo
b) equals
c) errorBy
d) similarTo
Answer
Answer: a [Reason:] Test whether given numbers are close to a given value.
2. assertThat(1.03, is(closeTo(1.0, 0.03))) is ______
a) True
b) False
c) Null
d) Error
Answer
Answer: a [Reason:] 1.0 + 0.03 is 1.03 which is the first value.
3. assertThat(0.03, is(closeTo(1.0, 0.03))) is _____
a) True
b) False
c) Null
d) Error
Answer
Answer: b [Reason:] 1.0-0.03 = 0.97>0.03, hence is false.
4. The closeTo function is found under which package?
a) org.hamcrest.TypeSafeMatcher
b) org.hamcrest.BaseMatcher
c) org.hamcrest.number.IsCloseTo
d) org.hamcrest.number.CloseTo
Answer
Answer: c [Reason:] The closeTo function is actually a method of the isCloseTo class of Hamcrest.
5. The isCloseTo class extends which base class?
a) Matcher
b) HamcrestCore
c) TypeMatcher
d) TypeSafeMatcher
Answer
Answer: 5 [Reason:] The TypeSafeMatcher is a useful base class for Matchers that require non-null values of a specific type.
6. The _____ method of the TypeSafeMatcher class is made final.
a) matches
b) matchesSafely
c) describeMismatchSafely
d) No mehtod
Answer
Answer: a [Reason:] The matches method made final to prevent accidental override.
7. The TypeSafeMatcher implements the ____ interface.
a) Self
b) Describing
c) EqulityCheck
d) SelfDescribing
Answer
Answer: d [Reason:] This interface dictates the ability of an object to describe what it does.
8. The ______ matcher checks if a given key is in a map.
a) hasEntry
b) hasValue
c) hasKey
d) isKey
Answer
Answer: c [Reason:] The hasKey matcher checks if the particular key is present in the map.
9. To write custom matcher _______ has to be implemented.
a) Matcher Interface
b) Hamcrest Interface
c) HamcrestMatching Interface
d) Not possible to implement
Answer
Answer: b [Reason:] The Matcher interface describes all methods needed to write custom matchers, which override the Matcher methods.
10. _____ is the base class for all Matcher implementations.
a) MatcherBase
b) Base
c) Matcher
d) BaseMatcher
Answer
Answer: d [Reason:] The BaseMatcher class is extended in every matcher subclass.