Servlets - Online Quiz


Advertisements

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

Q 1 - Which of the following is the correct order of servlet life cycle phase methods?

A - init(), service(), destroy()

B - initialize(), service(), destroy()

C - init(), execute(), destroy()

D - init(), service(), delete()

Answer : A

Explaination

The servlet is initialized by calling the init () method. The servlet calls service() method to process a client's request. The servlet is terminated by calling the destroy() method.

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.

Q 4 - Which of the following code retrieves the name of the HTTP Method?

A - Header.getMethod()

B - response.getMethod()

C - request.getMethod()

D - None of the above.

Answer : C

Explaination

request.getMethod() returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.

Q 5 - Which of the following code encodes the specified URL by including the session ID in it?

A - response.encodeURL(url)

B - request.encodeURL(url)

C - Header.encodeURL(url)

D - None of the above.

Answer : A

Explaination

response.encodeURL(url) encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged.

Q 6 - Which of the following code can be used to force any content in the buffer to be written to the client?

A - request.flushBuffer()

B - response.flush()

C - response.flushBuffer()

D - None of the above.

Answer : C

Explaination

response.flushBuffer() forces any content in the buffer to be written to the client.

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 get session id of a HTTP Session object in servlets?

A - session.getSessionId()

B - session.getId()

C - session.getActiveId()

D - None of the above.

Answer : B

Explaination

session.getId() returns a string containing the unique identifier assigned to this session.

Q 10 - Which of the following code is used to get country/region code in servlets?

A - request.getCountry()

B - Locale.getCountry()

C - response.getCountry()

D - None of the above.

Answer : A

Explaination

request.getCountry() returns the country/region code in upper case for this locale in ISO 3166 2-letter format.

servlets-questions-answers.htm
Advertisements