Servlets - Online Quiz



Following quiz provides Multiple Choice Questions (MCQs) related to Servlets Framework. You will have to read all the given answers and click over the correct answer. If you are not sure about the answer then you can check the answer using Show Answer button. You can use Next Quiz button to check new set of questions in the quiz.

Questions and Answers

Answer : D

Explaination

The servlet container (i.e. web server) calls the service() method to handle requests coming from the client( browsers) and to write the formatted response back to the client. Each time the server receives a request for a servlet, the server spawns a new thread and calls service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.

Q 2 - What is javax.servlet.http.HttpServlet?

A - interface

B - abstract class

C - concreate class

D - None of the above.

Answer : B

Explaination

javax.servlet.http.HttpServlet is an abstract class.

Q 3 - Which of the following code is used to get session in servlet?

A - request.getSession()

B - response.getSession()

C - new Session()

D - None of the above.

Answer : A

Explaination

request.getSession() returns the current session associated with this request, or if the request does not have a session, creates one.

Answer : C

Explaination

request.getHeader(headerName) returns the value of the specified request header as a String.

Q 5 - Which of the following code checks whether this request was made using a secure channel, such as HTTPS?

A - response.isSecure()

B - request.isSafe()

C - Header.isSecure()

D - None of the above.

Answer : B

Explaination

request.isSecure() returns a boolean indicating whether this request was made using a secure channel, such as HTTPS.

Q 6 - Which of the following code can be used to send an error response to the client using the specified status code and error message?

A - request.sendError(statusCode,message)

B - response.sendError(statusCode,message)

C - header.sendError(statusCode,message)

D - None of the above.

Answer : B

Explaination

response.sendError(statusCode,message) sends an error response to the client using the specified status code and error message.

Answer : C

Explaination

Servlet Filters are Java classes that can be used to intercept requests from a client before they access a resource at back end and to manipulate responses from server before they are sent back to the client.

Answer : C

Explaination

The init method is designed to be called only once. It is called when the filter is first created, and not called again for each user request. It simply creates or loads some data that will be used throughout the life of the filter.

Q 9 - Which of the following code is used to add an attribute in a HTTP Session object in servlets?

A - session.addAttribute(name,value)

B - session.setAttribute(name,value)

C - session.createAttribute(name,value)

D - None of the above.

Answer : B

Explaination

session.setAttribute() binds an object to this session, using the name specified.

Q 10 - Which of the following code is used to set auto refresh of a page after 5 seconds?

A - session.setIntHeader("Refresh", 5)

B - response.setIntHeader("Refresh", 5)

C - request.setIntHeader("Refresh", 5)

D - None of the above.

Answer : B

Explaination

response.setIntHeader("Refresh", 5) sets refresh, autoload time as 5 seconds.

servlets-questions-answers.htm
Advertisements