Servlet Interview questions for freshers part-I
1. Who maintains the life cycle of the servlet?
Ans: a) Servlet container
b) the client
c) Both a and b
2.Which of the following method is called when the Servlet is first loaded?
a) initialize() method
Ans:b) init() method
c) Both of the above
3.Which of the following is a feature of Servlet?
a) Supports Multithreading
b) Platform independent
c) Protocol independent
Ans:d) All of the above
4.Servlets can run using
a) Any web server
Ans:b) Any web server with built in Servlet engine / add on for the servlet engine
c) Any server that has JVM inside it.
d) all of the above
5.a head request is just like a get request except that
a) it asks server to return response headers only and not the complete resource.
b) is used to check characteristics of a resource without downloading it
c) is used when you don’t need actual file contents
Ans:d) all of the above
6.Pre-requisites for running Servlets are
a) Web Server that is Java Enabled
b) Servlet API Class library
C) Java Runtime Environment
Ans:d) All the above
7.HTTP header fields include
a) Content-length
b) Cookies
c) Type of request - POST or GET
Ans:d) All the above
8.What will happen when the code is compiled and executed?
import javax. servlet.*; import javax.servlet.http.*; import java.io.*; class TestServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out=response.getWriter(); out.println("<b> Servlet Exam"); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
a) Compilation Error
Ans:b) Compiles fine but will not be executed. Displays error.
c) Displays output as Servlet Exam in HTML bold text
d) Displays output as < b > Servlet Exam
9. Method that is used to retrieve value from a session variable is (assuming session is an object of HttpSession)
a) session.getValue(”sessionvarname”);
Ans:b) session.getAttribute(”sessionvarname”);
c) session.getDetails(”sessionvarname”);
d) Options a and b
10.init method of a servlet is called only once in its lifetime
a) False
Ans:b) True
12. Which of the following is the best answer
Ans:a)Although webservers can be configured to ports other than 80 for http requests, it is fine to configure them to 8000 in production environments
b)Although webservers can be configured to ports other than 80 for http requests, it is not fine to configure them to 8000 in production environments
c)Webservers do not allow you to configure to ports other than 80 and 443
13.The following method is used to create a PrintWriter object that can be used to write text to the response
Ans:a) getWriter()
b) getOutputStream()
c) getBinaryStream()
-d) getStream()
14.The service() method has objects of these classes as parameters - HttpServletRequest and HttpServletResponse
Ans:a) True
b) False
15.Consider the following servlet
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class MyServlet extends HttpServlet { int var; public void init() throws ServletException { var=0; } .... } ?
For every browser request, a new instance of the servlet is created by the server
a) True
Ans:b) False




Leave a Reply
You must be logged in to post a comment.