100% found this document useful (1 vote)
707 views

Servlet MCQs

The document provides information about servlets including: 1. The servlet container provides lifecycle management, communication support, and multithreading support for servlets. 2. The session.getAttribute() method is used to get an attribute from an HTTP session object in servlets. 3. Servlets execute within the address space of the web server, are platform independent since they are written in Java, and can use the full functionality of the Java class libraries.

Uploaded by

Disha Thorat
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
100% found this document useful (1 vote)
707 views

Servlet MCQs

The document provides information about servlets including: 1. The servlet container provides lifecycle management, communication support, and multithreading support for servlets. 2. The session.getAttribute() method is used to get an attribute from an HTTP session object in servlets. 3. Servlets execute within the address space of the web server, are platform independent since they are written in Java, and can use the full functionality of the Java class libraries.

Uploaded by

Disha Thorat
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/ 8

Chapter : Servlet

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

3. What are the functions of Servlet container?

 A. Lifecycle management

 B. Communication support

 C. Multithreading support

 D. All of the above

Ans D

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

Ans D
5. Which of the following is true about servlets?

 A. Servlets execute within the address space of web server

 B. Servlets are platform-independent because they are written in java

 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

 B. A status of 300 to 399 are informational messages

 C. A status of 400 to 499 indicates an error in the server

 D. A status of 500 to 599 indicates an error in the client

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

 D. None of the above

Ans C

8. Which method take a string not a URL?

 A. sendRedirect

 B. forward

 C. Both

 D. None

Ans A
9. RequestDispatcher object is used

 A. to include other resources

 B. to include an image

 C. to include xml object

 D. to include e-mailing response

Ans A

10. Which of the following are interface?

1. ServletContext 2. Servlet 3. GenericServlet 4. HttpServlet

 A. 1,2,3,4

 B. 1,2

 C. 1,3,4

 D. 1,4

Ans B

11. What is the meaning of response.setHeader("xyz", "abc");

 A. Add a new header and value

 B. Add an additional value to exiting header

 C. Add a new header and value or add an additional value to exiting header

 D. Add new header and value always

Ans A

12. What are the features added in Servlet 2.5?

 A. Dependency on J2SE 5.0

 B. Support for annotations

 C. Loading the class

 D. All of these

Ans D
13 The life cycle of a servlet is managed by

 A. servlet context

 B. servlet container

 C. the supporting protocol (such as http or https)

 D. All of the above

Ans B

14. Which method shows the client what server is receiving?

 A. doGet

 B. doOption

 C. doTrace

 D. doPost

Ans A

15. How constructor can be used for a servlet?


a) Initialization
b) Constructor function
c) Initialization and Constructor function
d) Setup() method

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

17. What is the difference between servlets and applets?


i. Servlets execute on Server; Applets execute on browser
ii. Servlets have no GUI; Applet has GUI
iii. Servlets creates static web pages; Applets creates dynamic web pages
iv. Servlets can handle only a single request; Applet can handle multiple requests
a) i, ii, iii are correct
b) i, ii are correct
c) i, iii are correct
d) i, ii, iii, iv are correct

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

19. When destroy() method of a servlet is called?


a) The destroy() method is called only once at the end of the life cycle of a filter
b) The destroy() method is called after the filter has executed doFilter method
c) The destroy() method is called only once at the begining of the life cycle of a filter
d) The destroyer() method is called after the filter has executed

Ans A

20. Which of the following is true about servlets?


a) Servlets execute within the address space of web server
b) Servlets are platform-independent because they are written in java
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

21. Which of the below is not a session tracking method?


a) URL rewriting
b) History
c) Cookies
d) SSL sessions

Ans B

22. Which of the following is stored at client side?


a) URL rewriting
b) Hidden form fields
c) SSL sessions
d) Cookies

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

24. Which of the following is not true about session?


a) All users connect to the same session
b) All users have same session variable
c) Default timeout value for session variable is 20 minutes
d) New session cannot be created for a new user

Ans C

25. SessionIDs are stored in cookies.


a) True
b) False

Ans A

26. What is the maximum size of cookie?


a) 4 KB
b) 4 MB
c) 4 bytes
d) 40 KB

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.

27. How can we invalidate a session?


a) session.discontinue()
b) session.invalidate()
c) session.disconnect()
d) session.falsify()

Ans B

28. Application is instance of which class?


a) javax.servlet.Application
b) javax.servlet.HttpContext
c) javax.servlet.Context
d) javax.servlet.ServletContext

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

30. “request” is instance of which one of the following classes?


a) Request
b) HttpRequest
c) HttpServletRequest
d) ServletRequest

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

39.What is the lifecycle of a servlet?

a. Servlet class is loaded


b. Servlet instance is created
c. init,Service,destroy method is invoked
d. All mentioned above
Ans D

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

You might also like