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 : C

Explaination

The init method is designed to be called only once. It is called when the servlet 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 servlet.

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 a particular attribute in servlet?

A - request.getAttribute(name)

B - response.getAttribute(name)

C - new Attribute(name)

D - None of the above.

Answer : A

Explaination

request.getAttribute(name) returns the value of the named attribute as an Object, or null if no attribute of the given name exists.

Answer : C

Explaination

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

Q 5 - Which of the following code retrieves the login of the user making this request?

A - request.getRemoteUser()

B - response.getRemoteUser()

C - Header.getRemoteUser()

D - None of the above.

Answer : A

Explaination

request.getRemoteUser() returns the login of the user making this request, if the user has been authenticated, or null if the user has not been authenticated.

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

A - request.sendError(statusCode)

B - response.sendError(statusCode)

C - header.sendError(statusCode)

D - None of the above.

Answer : B

Explaination

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

Q 7 - Which of the following code can be used to set the locale of the response?

A - request.setLocale(locale)

B - response.setLocale(locale)

C - header.setLocale(locale)

D - None of the above.

Answer : B

Explaination

response.setLocale(locale) sets the locale of the response.

Q 8 - Which of the following request attributes that an error-handling servlet can access to analyse the nature of error/exception?

A - javax.servlet.error.status_code

B - javax.servlet.error.exception_type

C - javax.servlet.error.message

D - All of the above.

Answer : D

Explaination

All of the above request attributes can be accessed by error-handling servlet.

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.

Answer : A

Explaination

You can call public void setMaxInactiveInterval(int interval) method of session to set the timeout for a session individually.

servlets-questions-answers.htm
Advertisements