- Servlets Tutorial
- Servlets - Home
- Servlets - Overview
- Servlets - Environment Setup
- Servlets - Life Cycle
- Servlets - Examples
- Servlets - Form Data
- Servlets - Client Request
- Servlets - Server Response
- Servlets - Http Codes
- Servlets - Writing Filters
- Servlets - Exceptions
- Servlets - Cookies Handling
- Servlets - Session Tracking
- Servlets - Database Access
- Servlets - File Uploading
- Servlets - Handling Date
- Servlets - Page Redirect
- Servlets - Hits Counter
- Servlets - Auto Refresh
- Servlets - Sending Email
- Servlets - Packaging
- Servlets - Debugging
- Servlets - Internationalization
- Servlets - Annotations
- Servlets Useful Resources
- Servlets - Questions and Answers
- Servlets - Quick Guide
- Servlets - Useful Resources
- Servlets - Discussion
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.
Servlets Mock Test II
Q 1 - Which of the following code is used to get session in servlet?
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?
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)
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?
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?
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?
B - response.getCharacterEncoding()
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?
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?
Answer : B
Explaination
request.getContextPath() returns the portion of the request URI that indicates the context of the request.
Q 9 - Which of the following code retrieves the request header?
A - Header.getHeaderName(headerName)
B - response.getHeader(headerName)()
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?
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?
B - response.getParameter(name)
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?
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?
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?
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?
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?
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?
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?
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()
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?
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?
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)
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?
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)
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?
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 |