Servlet MCQs
Servlet MCQs
1. 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)
Ans A
2. Which method is used to specify before any lines that uses the PintWriter?
A. setPageType()
B. setContextType()
C. setContentType()
D. setResponseType()
Ans C
A. Lifecycle management
B. Communication support
C. Multithreading support
Ans D
4. Which object of HttpSession can be used to view and manipulate information about a session?
A. session identifier
B. creation time
Ans D
5. Which of the following is true about servlets?
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
Ans D
6. Which of the following statements are correct about the status of the Http response.
A. A status of 200 to 299 signifies that the request was successful
Ans C
7. Which methods are used to bind the objects on HttpSession instance and get the objects?
A. setAttribute
B. getAttribute
C. Both A & B
Ans C
A. sendRedirect
B. forward
C. Both
D. None
Ans A
9. RequestDispatcher object is used
Ans A
A. 1,2,3,4
B. 1,2
C. 1,3,4
D. 1,4
Ans B
C. Add a new header and value or add an additional value to exiting header
Ans A
D. All of these
Ans D
13 The life cycle of a servlet is managed by
A. servlet context
B. servlet container
Ans B
A. doGet
B. doOption
C. doTrace
D. doPost
Ans A
Ans C
There is no any constructor in servlet. We need to write public void init () method
16. Can servlet class declare constructor with ServletConfig object as an argument?
a) True
b) False
Ans B
We never write constructor and instead of that we are sending servlet config method in public void init()
method
Ans b.
18. 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)
Ans A
Ans A
Ans D
Ans B
Ans D
23. Which of the following leads to high network traffic?
a) URL rewriting
b) Hidden form fields
c) SSL sessions
d) Cookies
Ans A
Ans C
Ans A
Ans A
The 4K is the maximum size for the entire cookie, including name, value, expiry date etc. To support most
browsers, it is suggested to keep the name under 4000 bytes, and the overall cookie size under 4093 bytes.
Ans B
Ans D
29. Which option is true about session scope?
a) Objects are accessible only from the page in which they are created
b) Objects are accessible only from the pages which are in same session
c) Objects are accessible only from the pages which are processing the same request
d) Objects are accessible only from the pages which reside in same application
Ans B
Ans C
31. Which class provides stream to read binary data such as image etc. from the request object?
a. ServltInputStream
b. ServletOutputStream
c. Both A & B
d. None of the above
Ans A
32. The sendRedirect() method of HttpServletResponse interface can be used to redirect response to
another resource, it may be servlet, jsp or html file.
a. True
b. False
Ans A
33. What type of servlets use these methods doGet(), doPost(),doHead, doDelete(), doTrace()?
a. Genereic Servlets
b. HttpServlets
c. All of the above
d. None of the above
Ans B
34. Which cookie it is valid for single session only and it is removed each time when the user closes
the browser?
a. Persistent cookie
b. Non-persistent cookie
c. All the above
d. None of the above
Ans B
35. Web server is used for loading the init() method of servlet.
a. True
b. False
Ans A
36. Which method is used to send the same request and response objects to another servlet in
RequestDispacher ?
a. forward()
b. sendRedirect()
c. Both A & B
d. None of the above
Ans A
37. Which packages represent interfaces and classes for servlet API?
a. javax.servlet
b. javax.servlet.http
c. Both A & B
d. None of the above
Ans C
38. Which class can handle any type of request so that it is protocol-independent?
a. GenericServlet
b. HttpServlet
c. Both A & B
d. None of the above
Ans A
40. Which method in session tracking is used in a bit of information that is sent by a web server to a
browser and which can later be read back from that browser?
a. HttpSession
b. URL rewriting
c. Cookies
d. Hidden form fields
Ans C