- 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
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 I
Q 1 - What are Servlets?
A - Java Servlets are programs that run on a Web or Application server.
Answer : C
Explaination
Java Servlets are programs that run on a Web or Application server and act as a middle layer between a request coming from a Web browser or other HTTP client and databases or applications on the HTTP server.
Q 2 - Which of the following is true about servlets?
A - Servlets execute within the address space of a Web server.
B - Servlets are platform-independent because they are written in Java.
C - The full functionality of the Java class libraries is available to a servlet.
Answer : D
Explaination
All of the above stand true for servlets.
Q 3 - Which of the following package contains servlet classes?
Answer : C
Explaination
Servlets can be created using the javax.servlet and javax.servlet.http packages, which are a standard part of the Java's enterprise edition, an expanded version of the Java class library that supports large-scale development projects.
Q 4 - Which of the following is the correct order of servlet life cycle phase methods?
A - init(), service(), destroy()
B - initialize(), service(), destroy()
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 5 - When init() method of servlet gets called?
A - The init() method is called when the servlet is first created.
B - The init() method is called whenever the servlet is invoked.
Answer : A
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. So, it is used for one-time initializations, just as with the init method of applets.
Q 6 - Which of the following is true about init() method of servlet?
B - The init() method is not called again and again for each user request.
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 7 - When service() method of servlet gets called?
A - The service() method is called when the servlet is first created.
B - The service() method is called whenever the servlet is invoked.
Answer : B
Explaination
Each time the server receives a request for a servlet, the server spawns a new thread and calls service() method.
Q 8 - Which of the following is true about service() method of servlet?
Answer : D
Explaination
The servlet container (i.e. web server) calls the service() method to handle requests coming from the client( browsers) and to write the formatted response back to the client. Each time the server receives a request for a servlet, the server spawns a new thread and calls service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.
Q 9 - When doGet() method of servlet gets called?
A - A GET request results from a normal request for a URL.
B - The service() method checks the HTTP request type as GET and calls doGet() method.
Answer : C
Explaination
A GET request results from a normal request for a URL or from an HTML form that has no METHOD specified and it should be handled by doGet() method. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.
Q 10 - When doPost() method of servlet gets called?
A - A POST request results from an HTML form that specifically lists POST as the METHOD.
B - The service() method checks the HTTP request type as POST and calls doPost() method.
Answer : C
Explaination
A POST request results from an HTML form that specifically lists POST as the METHOD and it should be handled by doPost() method. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.
Q 11 - When destroy() method of servlet gets called?
A - The destroy() method is called only once at the end of the life cycle of a servlet.
B - The destroy() method is called after the servlet has executed service method.
Answer : A
Explaination
The destroy() method is called only once at the end of the life cycle of a servlet.
Q 12 - Which of the following is true about destroy() method of servlet?
A - After the destroy() method is called, the servlet object is marked for garbage collection.
B - The servlet is terminated by calling the destroy() method.
Answer : C
Explaination
The servlet is terminated by calling the destroy() method. After the destroy() method is called, the servlet object is marked for garbage collection.
Q 13 - What is javax.servlet.Servlet?
Answer : A
Explaination
javax.servlet.Servlet is an interface.
Q 14 - What is javax.servlet.http.HttpServlet?
Answer : B
Explaination
javax.servlet.http.HttpServlet is an abstract class.
Q 15 - Which of the following is true about HTTP Get method?
A - The GET method sends the encoded user information appended to the page request.
B - The GET method is the defualt method to pass information from browser to web server.
Answer : C
Explaination
The GET method sends the encoded user information appended to the page request. It is the defualt method to pass information from browser to web server.
Q 16 - Which of the following is true about HTTP Post method?
A - The POST method sends the encoded user information as a seperate message to page request.
Answer : C
Explaination
The POST method sends the encoded user information as a seperate message to page request. It is used to submit form data normally.
Q 17 - Which of the following method can be used to get the value of form parameter?
B - request.getParameterValues()
Answer : A
Explaination
You call request.getParameter() method to get the value of a form parameter.
Q 18 - Which of the following method can be used to get the multiple values of a parameter like checkbox data?
B - request.getParameterValues()
Answer : B
Explaination
You call request.getParameterValues() method if the parameter appears more than once and returns multiple values, for example checkbox.
Q 19 - Which of the following method can be used to get complete list of all parameters in the current request?
B - request.getParameterValues()
Answer : C
Explaination
You call request.getParameterNames() method to get complete list of all parameters in the current request.
Q 20 - Which of the following code is used to set content type of a page to be serviced using servlet?
Answer : A
Explaination
You call response.setContentType() method to set content type of a page to be serviced using servlet.
Q 21 - Which of the following code is used to get PrintWriter object in servlet?
Answer : A
Explaination
You call response.getWriter() method to get PrintWriter object in servlet.
Q 22 - Which of the following code is used to get cookies in servlet?
Answer : B
Explaination
request.getCookies() returns an array containing all of the Cookie objects the client sent with this request.
Q 23 - Which of the following code is used to get names of the attributes in servlet?
A - response.getAttributeNames()
B - request.getAttributeNames()
Answer : B
Explaination
request.getAttributeNames() returns an enumeration containing the names of the attributes available to this request.
Q 24 - Which of the following code is used to get names of the headers in servlet?
Answer : B
Explaination
request.getHeaderNames() returns an enumeration of all the header names this request contains.
Q 25 - Which of the following code is used to get names of the parameters in servlet?
A - request.getParameterNames()
B - response.getParameterNames()
Answer : A
Explaination
request.getParameterNames() returns an Enumeration of String objects containing the names of the parameters contained in this request.
Answer Sheet
Question Number | Answer Key |
---|---|
1 | C |
2 | D |
3 | C |
4 | A |
5 | A |
6 | C |
7 | B |
8 | D |
9 | C |
10 | C |
11 | A |
12 | C |
13 | A |
14 | B |
15 | C |
16 | C |
17 | A |
18 | B |
19 | C |
20 | A |
21 | A |
22 | B |
23 | B |
24 | B |
25 | A |