250+ TOP MCQs on JUnits and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “JUnits”.

1. JUnits are used for which type of testing?
a) Unit Testing
b) Integration Testing
c) System Testing
d) Blackbox Testing

Answer: a
Clarification: JUnit is a testing framework for unit testing. It uses java as a programming platform. It is managed by junit.org community.

2. Which of the below statement about JUnit is false?
a) It is an open source framework
b) It provides an annotation to identify test methods
c) It provides test runners for running test
d) They cannot be run automatically

Answer: d
Clarification: JUnits test can be run automatically and they check their own results and provide immediate feedback.

3. Which of the below is an incorrect annotation with respect to JUnits?
a) @Test
b) @BeforeClass
c) @Junit
d) @AfterEach

Answer: c
Clarification: @Test is used to annotate method under test, @BeforeEach and @AfterEach are called before and after each method respectively. @BeforeClass and @AfterClass are called only once for each class.

4. Which of these is not a mocking framework?
a) EasyMock
b) Mockito
c) PowerMock
d) MockJava

Answer: d
Clarification: EasyMock, jMock, Mockito, Unitils Mock, PowerMock and JMockit are a various mocking framework.

5. Which method is used to verify the actual and expected results in Junits?
a) assert()
b) equals()
c) ==
d) isEqual()

Answer: a
Clarification: assert method is used to compare actual and expected results in Junit. It has various implementation like assertEquals, assertArrayEquals, assertFalse, assertNotNull, etc.

6. What does assertSame() method use for assertion?
a) equals() method
b) isEqual() method
c) ==
d) compare() method

Answer: c
Clarification: == is used to compare the objects not the content. assertSame() method compares to check if actual and expected are the same objects. It does not compare their content.

7. How to let junits know that they need to be run using PowerMock?
a) @PowerMock
b) @RunWith(PowerMock)
c) @RunWith(Junits)
d) @RunWith(PowerMockRunner.class)

Answer: d
Clarification: @RunWith(PowerMockRunner.class) signifies to use PowerMock JUnit runner. Along with that @PrepareForTest(User.class) is used to declare the class being tested. mockStatic(Resource.class) is used to mock the static methods.

8. How can we simulate if then behavior in Junits?
a) if{..} else{..}
b) if(..){..} else{..}
c) Mockito.when(…).thenReturn(…);
d) Mockito.if(..).then(..);

Answer: c
Clarification: Mockito.when(mockList.size()).thenReturn(100); assertEquals(100, mockList.size()); is the usage to implement if and then behavior.

9. What is used to inject mock fields into the tested object automatically?
a) @InjectMocks
b) @Inject
c) @InjectMockObject
d) @Mock

Answer: a
Clarification: @InjectMocks annotation is used to inject mock fields into the tested object automatically.

@InjectMocks
MyDictionary dic = new MyDictionary();

10. How can junits be implemented using maven?
a)

  1.  <dependency>
  2.     <groupId>junitgroupId>
  3.     <artifactId>junitartifactId>
  4.     <version>4.8.1version>
  5.  dependency>

b)

  1.  <dependency>
  2.     <groupId>org.junitgroupId>
  3.     <artifactId>junitartifactId>
  4.     <version>4.8.1version>
  5.  dependency>

c)

  1.  <dependency>
  2.     <groupId>mock.junitgroupId>
  3.     <artifactId>junitartifactId>
  4.     <version>4.8.1version>
  5.  dependency>

d)

  1.  <dependency>
  2.     <groupId>junitsgroupId>
  3.     <artifactId>junitartifactId>
  4.     <version>4.8.1version>
  5.  dependency>

Answer: a
Clarification: JUnits can be used using dependency tag in maven in pom.xml. The version as desired and available in repository can be used.

 
 

Advanced 250+ TOP MCQs on Servlet and Answers

This set of Advanced Java Multiple Choice Questions & Answers (MCQs) on “Servlet”.

1. How constructor can be used for a servlet?
a) Initialization
b) Constructor function
c) Initialization and Constructor function
d) Setup() method

Answer: c
Clarification: We cannot declare constructors for interface in Java. This means we cannot enforce this requirement to any class which implements Servlet interface.
Also, Servlet requires ServletConfig object for initialization which is created by container.

2. Can servlet class declare constructor with ServletConfig object as an argument?
a) True
b) False

Answer: b
Clarification: ServletConfig object is created after the constructor is called and before init() is called. So, servlet init parameters cannot be accessed in the constructor.

3. What is the difference between servlets and applets?
i. Servlets execute on Server; Applets execute on browser
ii. Servlets have no GUI; Applet has GUI
iii. Servlets creates static web pages; Applets creates dynamic web pages
iv. Servlets can handle only a single request; Applet can handle multiple requests
a) i, ii, iii are correct
b) i, ii are correct
c) i, iii are correct
d) i, ii, iii, iv are correct

Answer: b
Clarification: Servlets execute on Server and doesn’t have GUI. Applets execute on browser and has GUI.

4. Which of the following code is used to get an attribute in a HTTP Session object in servlets?
a) session.getAttribute(String name)
b) session.alterAttribute(String name)
c) session.updateAttribute(String name)
d) session.setAttribute(String name)

Answer: a
Clarification: session has various methods for use.

5. Which method is used to get three-letter abbreviation for locale’s country in servlets?
a) Request.getISO3Country()
b) Locale.getISO3Country()
c) Response.getISO3Country()
d) Local.retrieveISO3Country()

Answer: a
Clarification: Each country is usually denoted by a 3 digit code.ISO3 is the 3 digit country code.

6. Which of the following code retrieves the body of the request as binary data?
a) DataInputStream data = new InputStream()
b) DataInputStream data = response.getInputStream()
c) DataInputStream data = request.getInputStream()
d) DataInputStream data = request.fetchInputStream()

Answer: c
Clarification: InputStream is an abstract class. getInputStream() retrieves the request in binary data.

7. When destroy() method of a filter is called?
a) The destroy() method is called only once at the end of the life cycle of a filter
b) The destroy() method is called after the filter has executed doFilter method
c) The destroy() method is called only once at the begining of the life cycle of a filter
d) The destroyer() method is called after the filter has executed

Answer: a
Clarification: destroy() is an end of life cycle method so it is called at the end of life cycle.

8. Which of the following is true about servlets?
a) Servlets execute within the address space of web server
b) Servlets are platform-independent because they are written in java
c) Servlets can use the full functionality of the Java class libraries
d) Servlets execute within the address space of web server, platform independent and uses the functionality of java class libraries

Answer: d
Clarification: Servlets execute within the address space of a web server. Since it is written in java it is platform independent. The full functionality is available through libraries.

9. How is the dynamic interception of requests and responses to transform the information done?
a) servlet container
b) servlet config
c) servlet context
d) servlet filter

Answer: d
Clarification: Servlet has various components like container, config, context, filter. Servlet filter provides the dynamic interception of requests and responses to transform the information.

10. Which are the session tracking techniques?
i. URL rewriting
ii. Using session object
iii.Using response object
iv. Using hidden fields
v. Using cookies
vi. Using servlet object
a) i, ii, iii, vi
b) i, ii, iv, v
c) i, vi, iii, v
d) i, ii, iii, v

Answer: b
Clarification: URL rewriting, using session object, using cookies, using hidden fields are session tracking techniques.