Servlets - Mock Test



This section presents you various set of Mock Tests related to Servlets Framework. You can download these sample mock tests at your local machine and solve offline at your convenience. Every mock test is supplied with a mock test key to let you verify the final score and grade yourself.

Questions and Answers

Servlets Mock Test II

Q 1 - 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 2 - 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

Explaination

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

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.

Q 4 - Which of the following code retrieves the name of the authentication scheme?

A - new AuthType()

B - response.getAuthType()

C - request.getAuthType()

D - None of the above.

Answer : C

Explaination

request.getAuthType() returns the name of the authentication scheme used to protect the servlet, for example, BASIC or SSL or null if the JSP was not protected.

Q 5 - Which of the following code retrieves the body of the request as binary data?

A - new InputStream()

B - response.getInputStream()

C - request.getInputStream()

D - None of the above.

Answer : C

Explaination

request.getInputStream() retrieves the body of the request as binary data using a ServletInputStream.

Q 6 - Which of the following code retrieves the character encoding used in the body of this request?

A - new CharacterEncoding()

B - response.getCharacterEncoding()

C - request.getCharacterEncoding()

D - None of the above.

Answer : C

Explaination

request.getCharacterEncoding() returns the name of the character encoding used in the body of this request.

Q 7 - Which of the following code retrieves the MIME type of the body of the request?

A - new MimeType()

B - request.getContentType()

C - response.getContentType()

D - None of the above.

Answer : B

Explaination

request.getContentType() returns the MIME type of the body of the request, or null if the type is not known.

Q 8 - Which of the following code retrieves the context of the request?

A - new ClassContextPath()

B - request.getContextPath()

C - response.getContextPath()

D - None of the above.

Answer : B

Explaination

request.getContextPath() returns the portion of the request URI that indicates the context of the request.

Answer : C

Explaination

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

Q 10 - 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 11 - Which of the following code retrieves the value of a request parameter?

A - Header.getParameter(name)

B - response.getParameter(name)

C - request.getParameter(name)

D - None of the above.

Answer : C

Explaination

request.getParameter(name) returns the value of a request parameter as a String, or null if the parameter does not exist.

Q 12 - Which of the following code retrieves any extra path information associated with the URL the client sent?

A - Header.getPathInfo()

B - response.getPathInfo()

C - request.getPathInfo()

D - None of the above.

Answer : C

Explaination

request.getPathInfo() returns any extra path information associated with the URL the client sent when it made this request.

Q 13 - Which of the following code retrieves name and version of the protocol?

A - Header.getProtocol()

B - response.getProtocol()

C - request.getProtocol()

D - None of the above.

Answer : C

Explaination

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

Q 14 - Which of the following code retrieves the query string that is contained in the request URL after the path?

A - Header.getQueryString()

B - response.getQueryString()

C - request.getQueryString()

D - None of the above.

Answer : C

Explaination

request.getQueryString() returns the query string that is contained in the request URL after the path.

Q 15 - Which of the following code retrieves the Internet Protocol (IP) address of the client that sent the request?

A - request.getRemoteAddr()

B - response.getRemoteAddr()

C - Header.getRemoteAddr()

D - None of the above.

Answer : A

Explaination

request.getRemoteAddr() returns the Internet Protocol (IP) address of the client that sent the request.

Q 16 - Which of the following code retrieves the fully qualified name of the client making this request?

A - request.getRemoteHost()

B - response.getRemoteHost()

C - Header.getRemoteHost()

D - None of the above.

Answer : A

Explaination

request.getRemoteHost() returns the fully qualified name of the client that sent the request.

Q 17 - 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 18 - Which of the following code retrieves the part of this request's URL from the protocol name?

A - request.getRequestURI()

B - response.getRequestURI()

C - Header.getRequestURI()

D - None of the above.

Answer : A

Explaination

request.getRequestURI() returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.

Q 19 - 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

Explaination

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

Q 20 - 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 21 - Which of the following code returns the port number on which this request was received?

A - response.getServerPort()

B - request.getServerPort()

C - Header.getServerPort()

D - None of the above.

Answer : B

Explaination

request.getServerPort() returns the port number on which this request was received.

Q 22 - Which of the following code encodes the specified URL for use in the sendRedirect method?

A - response.encodeRedirectURL(url)

B - request.encodeRedirectURL(url)

C - Header.encodeRedirectURL(url)

D - None of the above.

Answer : A

Explaination

response.encodeRedirectURL(url) encodes the specified URL for use in the sendRedirect method or, if encoding is not needed, returns the URL unchanged.

Q 23 - 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 24 - Which of the following code indicates whether the named response header has already been set?

A - response.containsHeader(headerName)

B - request.containsHeader(headerName)

C - Header.containsHeader(headerName)

D - None of the above.

Answer : A

Explaination

response.containsHeader(headerName) returns a boolean indicating whether the named response header has already been set.

Q 25 - Which of the following code indicates whether the response has been committed?

A - response.isCommitted()

B - request.isCommitted()

C - Header.isCommitted()

D - None of the above.

Answer : A

Explaination

response.isCommitted() returns a boolean indicating if the response has been committed.

Answer Sheet

Question Number Answer Key
1 A
2 A
3 A
4 C
5 C
6 C
7 B
8 B
9 C
10 C
11 C
12 C
13 C
14 C
15 A
16 A
17 A
18 A
19 A
20 B
21 B
22 A
23 A
24 A
25 A
servlets-questions-answers.htm
Advertisements