0% found this document useful (0 votes)
179 views

MCQS Servlet

The document contains multiple choice questions about Java servlets. It covers servlet lifecycle stages, advantages over other server extensions, methods to process requests and formulate responses, deployment descriptors, initialization and more. The questions test understanding of fundamental servlet concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
179 views

MCQS Servlet

The document contains multiple choice questions about Java servlets. It covers servlet lifecycle stages, advantages over other server extensions, methods to process requests and formulate responses, deployment descriptors, initialization and more. The questions test understanding of fundamental servlet concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

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. 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

3. 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

4. … 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
5. …… 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
6. ……… 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

7. 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

8. Which interface must a Java class implement to create a servlet?

a) Servlet

b) HttpServlet

c) ServletConfig

d) ServletRequest

9. Which HTTP method is used to retrieve data from a server using a servlet?

a) GET

b) POST

c) PUT

d) DELETE
10. Which of the following is true about servlet containers?

a) They are used to compile servlets into bytecode

b) They are responsible for managing the lifecycle of servlets

c) They are part of the Java Development Kit (JDK)

d) They are used for storing servlet-related data

11. What is the purpose of the web.xml file in a servlet-based web application?

a) To define the HTML structure of web pages

b) To configure the servlet container and servlet mappings

c) To store client-side JavaScript code

d) To define the database schema

12. Which of the following is true about servlet threading?

a) Each servlet instance runs in a separate thread

b) All servlet instances run in the same thread

c) Servlets do not support multithreading

d) Servlets always run in parallel threads

13. How can a servlet send data back to the client?

a) By calling the forward() method


b) By using the println() method of the PrintWriter class

c) By invoking the getRequestDispatcher() method

d) By setting response headers

14. Which of the following is NOT a valid method to initialize a servlet?

a) Using the init() method

b) Using the @WebServlet annotation

c) Using the web.xml deployment descriptor

d) Using the doGet() method

15. Which object is used to obtain initialization parameters in a servlet?

a) ServletConfig

b) HttpServletRequest

c) HttpServletResponse

d) ServletContext

16. Which method is used to include the content of another resource during servlet processing?

a) include()

b) import()

c) use()

d) forward()

17. What is the purpose of the ServletContext object?

a) To hold client session information

b) To process client requests


c) To manage the lifecycle of servlet instances

d) To provide a shared context for servlets within a web application

18. What is the purpose of the RequestDispatcher interface in a servlet?

a) To handle HTTP request headers

b) To retrieve client cookies

c) To forward or include requests to other resources

d) To manage servlet configuration settings

19. 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)

20. Which object of HttpSession can be used to view and manipulate information about a
session?

a. session identifier
b. creation time
c. last accessed time
d. All mentioned above

21. Which interface must be implemented by all servlets?


a) WebServlet

b) ServletInterface

c) Servlet

d) ServletApp
22. Which method receives and processes client requests?
a) getRequest()

b) processRequest()

c) service()

d) handle()

23. Which servlet method is called for every client request?


a) start()

b) doService()

c) service()

d) handleRequest()

24. What is the default session timeout (in minutes) in web.xml?


a) 10

b) 15

c) 20

d) 30

25. Which method is used to retrieve parameter values from the query string?
a) getParams()

b) getParameter()

c) getQuery()

d) fetchParameter()

26. What is the use of the RequestDispatcher interface?


a) Manage request attributes

b) Forward or include resource/response

c) Manage HTTP headers

d) Handle session tracking

27. Which listener is used to listen to HttpSession events?


a) ServletRequestListener

b) ServletContextListener

c) HttpSessionListener

d) ServletRequestAttributeListener
28. Which annotation is used to define a servlet in the latest versions of the Servlet API?
a) @ServletDefinition

b) @WebServlet

c) @HttpServlet

d) @ServletConfig

29. Which method is used to get all the initialization parameters from the web.xml file?
a) getInitParameters()
b) getConfigParameters()

c) getServletParameters()

d) getInitParameterNames()

30. Which of the following code is used to set content type of a page to be serviced using servlet?
A - response.setContentType()
B - request.setContentType()
C - writer.setContentType()
D - None of the above.

31. Which of the following code retrieves the request header?

A - Header.getHeaderName(headerName)
B - response.getHeader(headerName)()
C - request.getHeader(headerName)
D - None of the above.

32. Which of the following application servers do not provide built in support for servlets?

a) Tomcat server

b) Glassfish

c) JBoss
d) None of the mentioned
33. Which class can handle any type of request so that it is protocol-independent?

(a) HttpServlet

(b) Request

(c) Http

(d) GenericServlet

34. Differentiate ServletConfig and ServletContext object.

(a) Both are same

(b) Scope of ServletConfig is servlet and Scope of ServletContext is web application

(c) Scope of ServletConfig is web application and Scope of ServletContext is servlet

(d) ServletContext object can't be nullified but ServletConfig object can be.

35. Which method is used to send the same request and response objects to another servlet ?

(a) forward()

(b) next()

(c) sendForward()

(d) sendRedirect()

36. Deployment Descriptor(DD) is a

(a) Servlet used to maintain other servlet.

(b) Text document

(c) XML document that is used by Web Container to run servlets and JSPs.

(d) Library file.


37. Consider below web.xml file,

<web-app>

<servlet>

<servlet-name>DemoServlet </servlet-name>

<servlet-class>DemoServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>DemoServlet </servlet-name>

<url-pattern>/Demo</url-pattern>

</servlet-mapping>

</web-app>

Which url Is use to access DemoServlet ?

(a) /Demo

(b) /*

(c) /DemoServlet

(d) /Servlet

38. is responsible to create the object of servlet?

(a) web container

(b) servlet container

(c) both A & B

(d) only B
39. When servlet object is created? (select the correct option)

i) When the application begins.

ii) When servlet is first accessed.

iii) When init() of the servlet is called.

iv) When service() of the servlet is called.

v) When run() of Servlet is called

(a) i) and ii)

(b) i),ii) and iii)

(c) i),iii) and iv)

(d) only v)

40. What happens if you add a main method to servlet?

(a) It will give an error.

(b) Program will start execution with main method without any error.

(c) No error but this method will not get executed automatically.

(d) main method will be executed automatically after service method().

41. What is MIME Type?

(a) Multiple Internet Mail Extension

(b) Multiple Intranet Mail Extension

(c) Multipurpose Internet Mail Expansion

(d) Multipurpose Internet Mail Extension


42. Is there any constructor in Servlet?

(a) Yes, servlet is java class, thus it contain constructor.

(b) No constructor because Servlet is web class and not java class.

(c) It will give run time error if you write constructor in servlet.

(d) It can have constructor but it is not the right way to initialize servlet.

43. is the parent class of java servlet.

(a) Servlet

(b) GenericServlet

(c) HttpServlet

(d) ServletConfig

44. Maximum size of data that can be sent using doGet() is?

(a) 240 bytes

(b) 1024 bytes

(c) 128 bytes

(d) 2048 bytes

45. Which exceptions are thrown by init()?

(a) ServletException

(b) IOExecption

(c) both A & B

(d) only A

46. provides an interface between the container and servlet.

(a) ServletConfig

(b) ServletContext

(c) ServletRequest

(d) ServletResponse
47. controls the behaviour of Java Servlet.

(a) ServletConfig

(b) ServletContext

(c) Servlet Interface

(d) web.xml

48. When the object of servlet context is created?

(a) during initialization

(b) during request time

(c) during deployment

(d) during response time

49. How many servlet context object are there per web application?

(a) only one

(b) more than one

(c) atleast one

(d) one or zero

50 . As long as web application is executing, ______object will be available, and it will be destroyed once the
application is removed from the server.
(a) ServletContext

(b) ServletConfig

(c) ServletRequest

(d) ServletResponse

51. The ____interface provides the facility of dispatching the request to another resource.

(a) ServletRequest

(b) HttpSession

(c) Filter

(d) RequestDispatcher
52. The tasks of authentication, blocking of requests, data compression, Encryption and Decryption are
performed by

(a) Servlet Config

(b) Servlet Context

(c) Servlet Filter

(d) Servlet Container

53. Which method is used to redirect response to another resource?

(a) response.sendRedirect()

(b) request.sendRedirect()

(c) request.forward()

(d) response.include()

54. Http is ……. .

(a) Stateless protocol

(b) Statefull protocol

(c) Transfer protocol

(d) none of these

You might also like