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

Multiple Choice Questions On Java Servlets

The document contains 10 multiple choice questions about Java servlets. It tests knowledge of servlet lifecycles, request and response methods, sessions, locales and error handling. The questions cover topics like retrieving parameter values, getting the protocol, setting character encoding and updating session attributes. Correct answers are provided along with short explanations for each question.

Uploaded by

Aniruddh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
1K views

Multiple Choice Questions On Java Servlets

The document contains 10 multiple choice questions about Java servlets. It tests knowledge of servlet lifecycles, request and response methods, sessions, locales and error handling. The questions cover topics like retrieving parameter values, getting the protocol, setting character encoding and updating session attributes. Correct answers are provided along with short explanations for each question.

Uploaded by

Aniruddh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Multiple Choice Questions on Java Servlets

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

Explanation: 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 - Which of the following method can be used to get the multiple values of a parameter like checkbox
data?
A - request.getParameter()
B - request.getParameterValues()
C - request.getParameterNames()
D - None of the above.

Answer: B

Explanation: You call request.getParameterValues() method if the parameter appears more than once
and returns multiple values, for example checkbox.

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


A - request.getlocale()
B - response.getLocale()
C - new Locale()
D - None of the above.

Answer: A

Explanation: request.getlocale() returns the preferred Locale that the client will accept content in, based
on the Accept-Language header.

Q 4 - Which of the following code retrieves name and version of the protocol?
A - Header.getProtocol()
B - response.getProtocol()

1
C - request.getProtocol()
D - None of the above.

Answer: C

Explanation: request.getProtocol() returns the name and version of the protocol the request.

Q 5 - Which of the following code retrieves session ID specified by the client?


A - request.getRequestedSessionId()
B - response.getRequestedSessionId()
C - Header.getRequestedSessionId()
D - None of the above.

Answer: A

Explanation: request.getRequestedSessionId() returns the session ID specified by the client.

Q 6 - Which of the following code can be used to clear any data that exists in the buffer as well as the
status code and headers?
A - request.reset()
B - response.reset()
C - response.resetBuffer()
D - None of the above.

Answer: B

Explanation: response.reset() clears any data that exists in the buffer as well as the status code and
headers.

Q 7 - Which of the following code can be used to set the character encoding for the body of the
response?
A - response.setCharacterEncoding(charset)
B - request.setCharacterEncoding(charset)
C - header.setCharacterEncoding(charset)
D - None of the above.

Answer: A

Explanation: response.setCharacterEncoding(charset) sets the character encodinge for the body of the
response.

Q 8 - Which of the following is true about javax.servlet.error.status_code?

2
A - This attribute gives exact error message which can be stored and analysed after storing in a
java.lang.String data type.
B - This attribute gives information about exception type which can be stored and analysed after storing
in a java.lang.Class data type.
C - This attribute gives status code which can be stored and analysed after storing in a java.lang.Integer
data type.
D - This attribute gives information about URL calling the servlet and it can be stored and analysed after
storing in a java.lang.String data type.

Answer: C

Explanation: javax.servlet.error.status_code attribute gives status code which can be stored and analysed
after storing in a java.lang.Integer data type.

Q 9 - Which of the following code is used to update an attribute in a HTTP Session object in servlets?
A - session.setAttribute(name,value)
B - session.alterAttribute(name,value)
C - session.updateAttribute(name,value)
D - None of the above.

Answer: A

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

Q 10 - Which of the following code is used to get language code in servlets?


A - response.getLanguage()
B - Locale.getLanguage()
C - request.getLanguage()
D - None of the above.

Answer: C

Explanation: request.getLanguage() returns the language code in lower case for this locale in ISO 639
format.

You might also like