Programming MCQs

20 Best Multiple-Choice Questions on Java Servlet

Google+ Pinterest LinkedIn Tumblr
Here are the collections of multiple-choice questions on Java Servlet includes the MCQ questions on the fundamentals of Java servlet and servlet life cycle. It also includes objective types of questions on the interfaces included in the servlet API, section tracking, GET and POST method, main functions of HttpServletRequest, and HttpServletResponse interface, the difference between session and cookie, and the servlet chaining.

Read Also: Solved MCQ of JDBC and JSP set-1

1. Which of the following are the principal stages in the life cycle of Java Servlet.
i) Server Initialization
ii) Servlet Execution
iii) Servlet Destruction
iv) Servlet Stop
A) i, ii, and iii only
B) i, iii, and iv only
C) ii, iii, and iv only
D) All i, ii, iii, and iv only

2. State whether the following statements about the Java servlet life cycle are True or False.
i) init() and destroy() method will be called only once during the lifetime of the servlet.
ii) Once the servlet is initialized any request that the servlet container receives will be forwarded to the servlet’s execute() method.
A) i-True, ii-True
B) i-True, ii-False
C) i-False, ii-True
D) i-False, ii-False

3. Which of the following are the advantages of Java Servlet over the other common server extensions.
i) Java servlets are faster than other server extensions like CGI scripts.
ii) Java servlets use a standard API that is supported by many browsers.
iii) Java servlets are portable between server and operating system.
A) i and ii only
B) ii and iii only
C) i and iii only
D) All i, ii and iii

4. Which of the following methods are provided to enable the servlet to process the client’s request.
i) getCookies()
ii) getRequest()
iii) getSession()
iv) getHeader()
A) i, ii, and iii only
B) i, iii, and iv only
C) ii, iii, and iv only
D) All i, ii, iii, and iv only

5. State whether the following statements on the methods of HTTP servlet request are True or False.
i) For the getHeader method, the match between the given name and the request header is case insensitive.
ii) If there is no query string, getQueryString method returns null.
A) i-True, ii-True
B) i-True, ii-False
C) i-False, ii-True
D) i-False, ii-False

6. … are the methods provided by HTTP servlet response to formulate the response to the client.
i) sendRedirect
ii) getWriter
iii) sendError
A) i and ii only
B) ii and iii only
C) i and iii only
D) All i, ii and iii

7. … method obtains a byte-based output stream that enables binary data to be sent to the client.
A) sendRedirect
B) getOutput()
C) getOutputStream()
D) getWirter

8. … method obtains a character-based output stream that enables text data to be sent to the client.
A) sendRedirect
B) getOutput()
C) getOutputStream()
D) getWirter

9. State whether the following statements about the methods provided by HTTP servlet response are True or False.
i) The addCookie method must be called before the response is committed so that the appropriate headers can be set.
ii) Further output should be made by the servlet after calling the sendError method.
A) i-True, ii-True
B) i-True, ii-False
C) i-False, ii-True
D) i-False, ii-False

10. Which of the following are the methods of cookies that are used for section tracking.
i) getMaxAge()
ii) getValue()
iii) getDate()
A) i and ii only
B) ii and iii only
C) i and iii only
D) All i, ii and iii

11. State whether the following statements about the methods of cookies that are used for section tracking are True or False.
i) A zero value on the Cookie.SetMaxAge(int expiry) tells the browser to delete the cookie immediately.
ii) If no maximum age was specified the getMaxAge() method returns zero.
A) i-True, ii-True
B) i-True, ii-False
C) i-False, ii-True
D) i-False, ii-False

12. State whether the following statements about the interfaces included in the servlet API are True or False.
i) The HttpServletRequest provides access to an input stream and so allows the servlet to read data from the client.
ii) The HttpServletResponse provides access to an output stream and so allows the servlet to send data to the client.
A) i-True, ii-True
B) i-True, ii-False
C) i-False, ii-True
D) i-False, ii-False

13. … are the main functions of the HttpServletRequest interface.
i) The interface HttpServletRequest encapsulates the functionality for a request object that is passed to an HTTP servlet.
ii) It allows the servlet to send data to the client.
iii) It contains the request from the client.
A) i and ii only
B) ii and iii only
C) i and iii only
D) All i, ii and iii

14. State whether the following statements about the GetQueryString method are True or False.
i) GetQueryString method returns a query string present in the request URL if any.
ii) If there is no query string, getQueryString() method returns null.
A) i-True, ii-True
B) i-True, ii-False
C) i-False, ii-True
D) i-False, ii-False

15. Which of the following are the purposes of section tracking.
i) Knowing the client preferences.
ii) Distinguishing between the different clients.
iii) Customizing the website as per the user requirement.
A) i and ii only
B) ii and iii only
C) i and iii only
D) All i, ii and iii

16. State whether the following statements about the GET method are True.
i) In the GET method entire form submission can be encapsulated in one URL.
ii) The query length is limited to 256 characters.
iii) The data is submitted as a part of the URL.
A) i and ii only
B) ii and iii only
C) i and iii only
D) All i, ii and iii

17. State whether the following statements about the POST method are True or False.
i) In the POST method, data is submitted inside the body of the HTTP request.
ii) Here the data is visible on the URL and less secure.
A) i-True, ii-True
B) i-True, ii-False
C) i-False, ii-True
D) i-False, ii-False

18. Which of the following is the difference between a session and a cookie.
i) Section is stored in the server but a cookie is stored in the client.
ii) There is a limit on the amount of data that can be stored on the session but it is not limited to cookies.
iii) Section can store objects and cookies can store only strings.
A) i and ii only
B) ii and iii only
C) i and iii only
D) All i, ii and iii

19. State whether the following statements about the servlet init parameters are True.
i) Servlet init parameters are for a single servlet only.
ii) It is declared inside the <servlet> tag inside the deployment descriptor.
iii) Servlet init parameters can also be accessed outside it.
A) i and ii only
B) ii and iii only
C) i and iii only
D) All i, ii and iii

20. State whether the following statements about the servlet chaining are True or False.
i) In servlet chaining two or more servlets can cooperate in servicing a single request.
ii) In servlet chaining, one servlet’s output is the input of the next servlet.
iii) We can achieve servlet chaining with the help of RequestDispatcher.
A) i-True, ii-True, iii-False
B) i-True, ii-False, iii-True
C) i-False, ii-True, iii-True
D) i-True, ii-True, iii-True

Answers:

  1. A) i, ii, and iii only
  2. B) i-True, ii-False
  3. D) All i, ii and iii
  4. B) i, iii, and iv only
  5. A) i-True, ii-True
  6. D) All i, ii and iii
  7. C) getOutputStream()
  8. D) getWirter
  9. B) i-True, ii-False
  10. A) i and ii only
  11. B) i-True, ii-False
  12. A) i-True, ii-True
  13. C) i and iii only
  14. A) i-True, ii-True
  15. D) All i, ii and iii
  16. C) i and iii only
  17. B) i-True, ii-False
  18. C) i and iii only
  19. A) i and ii only
  20. D) i-True, ii-True, iii-True

Read More: Collections of Java MCQ Questions

Avatar for Shuseel Baral

Shuseel Baral is a web programmer and the founder of InfoTechSite has over 8 years of experience in software development, internet, SEO, blogging and marketing digital products and services is passionate about exceeding your expectations.

Write A Comment