Sample questions
Question 1Medium
Which object is used to forward the request processing from one servlet to another?
- ServeltContext
- ServletConfig
- RequestDispatcher
- ResponseDispatcher
Answer: C. RequestDispatcher
Explanation
Request Dispatcher is used to forward the request processing from one servlet to another.
Step-by-step solution
- A
RequestDispatcher is used to forward a request to another servlet or JSP. - It is obtained from the request or the context, and
forward() is called on it. ServletContext and ServletConfig are not used for forwarding, and ResponseDispatcher does not exist.- So the answer is
RequestDispatcher, which is option C.
Why the other options are wrong
- A. ServletContext gives server-wide information but does not forward requests.
- B. ServletConfig gives startup parameters for a servlet, not forwarding.
- D. ResponseDispatcher does not exist in the servlet API.
Tip: Forwarding between servlets uses RequestDispatcher.
Question 2Medium
What's the difference between servlets and applets?
1.Servlets executes on Servers, where as Applets executes on Browser
2.Servlets have no GUI, where as an Applet has GUI
3.Servlets creates static web pages, where as Applets creates dynamic web pages
4.Servlets can handle only a single request, where as Applet can handle multiple requests
- 1,2,3 are correct
- 1,2 are correct
- 1,3 are correct
- 1,2,3,4 are correct
Answer: B. 1,2 are correct
Explanation
Applets
Applets are applications designed to be transmitted over the network and executed by Java compatible web browsers.
An Applet is a client side java program that runs within a Web browser on the client machine.
An applet can use the user interface classes like AWT or Swing.
Applet Life Cycle Methods: init(), stop(), paint(), start(), destroy()
Servlets
Servlets are Java based analog to CGI programs, implemented by means of servlet container associated with an HTTP server. Servlet is a server side component which runs on the web server.
The servlet does not have a user interface.
Servlet Methods: doGet(), doPost()
Step-by-step solution
- Statement 1 is true: servlets run on the server, and applets run in the browser.
- Statement 2 is true: servlets have no GUI, while applets have one.
- Statement 3 is false: it reverses the roles, since servlets create dynamic pages and applets are client-side components.
- Statement 4 is false: servlets handle many requests, not just one.
- The answer is 1,2 are correct.
Why the other options are wrong
- A. Statement 3 is reversed and false, so 1, 2, 3 cannot all be correct.
- C. Statement 3 is reversed, since servlets create dynamic pages, not static ones.
- D. Statements 3 and 4 are both false.
Tip: Servlets run on the server and generate responses; applets run in the browser with a GUI.
Question 3Medium
init(),service() and destroy()methods are define in
1.javax.servlet.Servlet interface
2.javax.servlet.ServletHttp class
3.javax.servlet.ServletRequest interface
4.javax.servlet.ServletResponse interface
- 1,2,3,4,5
- 1
- 3,4,5
- 1,4,5
Answer: B. 1
Explanation
init(), service() and destroy() are declared in the javax.servlet.Servlet interface, so only item 1 is correct. ServletRequest and ServletResponse do not declare these methods, and there is no class called ServletHttp.
Step-by-step solution
- Init, service and destroy are methods of the javax.servlet.Servlet interface.
- ServletHttp is not a real class; the HTTP class is HttpServlet, which implements Servlet.
- ServletRequest and ServletResponse do not have these life-cycle methods.
- So the answer is 1.
Why the other options are wrong
- A. It lists 2 to 5, but 2, 3 and 4 do not have these methods, and 5 does not exist.
- C. Items 3 and 4 are the request and response interfaces, and 5 does not exist.
- D. It includes 4, the response interface, which does not define these methods, and 5, which does not exist.
Tip: The life-cycle methods init, service and destroy belong to the Servlet interface.
Question 4Medium
Which HttpServletRequest method returns the extra path information that follows the servlet path in the request URL?
getContextPath()getServletPath()getPathInfo()- None of this
Answer: C. getPathInfo()
Explanation
getPathInfo() returns the extra path information that follows the servlet path. getContextPath() returns only the context root.
Step-by-step solution
- Request path = contextPath + servletPath + pathInfo.
- The extra part after the servlet path is path information.
- HttpServletRequest.getPathInfo() returns it.
Why the other options are wrong
- A. getContextPath() returns the application context root, not path info.
- B. getServletPath() returns the servlet mapping part.
- D. One listed method does return path info.
Tip: Context path, servlet path, path info: three consecutive parts of the request URI.
Question 5Medium
The include() method of RequestDispatcher
- Sends a request to another resource like servlet, jsp or html
- Includes resource of file like servlet, jsp or html
- Appends the request and response objects to the current servlet
- None of the above
Answer: B. Includes resource of file like servlet, jsp or html
Explanation
The include() method merges the response written by the calling servlet, and the activated servlet. This way you can achieve "server side includes" using the include().
Step-by-step solution
- RequestDispatcher.include() adds the output of another resource into the current response.
- The included resource can be a servlet, JSP or HTML file.
- forward() is the method that transfers control completely, so option A describes forward.
- So the answer is: Includes resource of file like servlet, jsp or html.
Why the other options are wrong
- A. This describes forward(), which transfers the request, not include().
- C. The request and response objects are passed; they are not appended to the servlet.
- D. Option B is correct, so None of the above is wrong.
Tip: include() merges output; forward() hands over control.
Question 6Medium
Older servlet textbooks quote a limit for the data that can be sent from an HTML form with doGet() (GET puts it in the URL). What is that commonly quoted limit?
- 4K
- 8K
- 2K
- 1K
Answer: C. 2K
Explanation
Using get method we can able to pass 2K data from HTML All data we are passing to Server will be displayed in URL (request string).
Step-by-step solution
- With
doGet(), form data is sent in the URL as a query string. - The traditional limit quoted for data passed this way is 2K.
POST data has no such limit, which is why doPost() is used for large data.- So the limit is 2K.
Why the other options are wrong
- A. 4K is not the quoted limit for GET data.
- B. 8K is larger than the usual 2K limit quoted for GET.
- D. 1K is smaller than the usual 2K limit quoted for GET.
Tip: GET data goes in the URL, so it is small; POST is for large data.
Question 7Medium
How many ServletContext objects are available for an entire web application?
- One each per servlet
- One each per request
- One each per response
- Only one
Answer: D. Only one
Explanation
There is one ServletConfig object per servlet. So if there are 10 servlets in an application, there would be 10 ServletConfig objects.
And there is only one ServletContext object per aplication.
Step-by-step solution
- A ServletContext object represents the entire web application.
- It provides configuration information and resources to all parts of the application.
- Since it's shared across all servlets, there is only one per application.
Why the other options are wrong
- A. Each servlet has its own ServletConfig object.
- B. Each request creates a new HttpServletRequest object.
- C. Each response creates a new HttpServletResponse object.
Tip: Remember: One ServletContext for the whole app.
Question 8Medium
What are the mechanisms available in ServletContextListener interface?
contextInit(), contextService(), contextDestroyed()contextInitialized((),contextDestroyed()contextInitialized(), contextService(), contextDestroyed()- None of the above
Answer: B. contextInitialized((),contextDestroyed()
Explanation
public void contextInitialized(ServletContextEvent sce):Notification that the web application is ready to process requests.
contextDestroyed:
public void contextDestroyed(ServletContextEvent sce) Notification that the servlet context is about to be shut down.
Step-by-step solution
- ServletContextListener has two methods that are called when the web application starts and stops.
- These are contextInitialized(ServletContextEvent) and contextDestroyed(ServletContextEvent).
- So the answer is option B: contextInitialized((),contextDestroyed().
Why the other options are wrong
- A. contextService() does not exist; the methods are contextInitialized and contextDestroyed.
- C. contextService() does not exist in ServletContextListener.
- D. Option B lists the correct methods, so none of the above is wrong.
Tip: The listener methods are contextInitialized and contextDestroyed.
Question 9Medium
The Java ____ specification defines an application programming interface for communication between the Web server and the application program.
- Servlet
- Server
- Program
- Randomize
Answer: A. Servlet
Explanation
Servlets are commonly used to generate dynamic responses to HTTP requests.
Step-by-step solution
- The Java Servlet specification defines the API for communication between a web server and a web application.
- Server, Program and Randomize are not names of this specification.
- So the blank is the Servlet specification.
- The answer is Servlet.
Why the other options are wrong
- B. Server is not the name of the specification.
- C. Program is not the name of the specification.
- D. Randomize has nothing to do with web communication.
Tip: Servlet is the Java web specification; JSP and JDBC are the other names to know.
Question 10Medium
The life cycle of a servlet is managed by
- Servlet context
- Servlet container
- The supporting protocol (such as http or https)
- All of the above
Answer: B. Servlet container
Explanation
A servlet is unloaded by the container if the container shuts down, or if the container reloads the whole web application at runtime.
Step-by-step solution
- The servlet container creates the servlet, calls init once, calls service for each request, and calls destroy at the end.
- The servlet context and the HTTP protocol do not manage the servlet's life cycle.
- So the answer is Servlet container.
Why the other options are wrong
- A. The servlet context gives the web application's shared information; it does not manage the life cycle.
- C. HTTP is the protocol that carries requests; it does not create or destroy servlets.
- D. Not all of these, because options A and C do not manage the life cycle.
Tip: The container, such as Tomcat, handles init, service and destroy.
Practise all 30 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →