Servlets - Mock Test


Advertisements


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 I

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 3 - Which of the following package contains servlet classes?

A - javax.servlet

B - javax.servlet.http

C - Both of the above.

D - None of the above.

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()

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.

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.

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.

Answer : B

Explaination

Each time the server receives a request for a servlet, the server spawns a new thread and calls service() method.

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.

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.

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.

Answer : A

Explaination

The destroy() method is called only once at the end of the life cycle of a servlet.

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.

Answer : A

Explaination

javax.servlet.Servlet is an interface.

Q 14 - 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.

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.

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?

A - request.getParameter()

B - request.getParameterValues()

C - request.getParameterNames()

D - None of the above.

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?

A - request.getParameter()

B - request.getParameterValues()

C - request.getParameterNames()

D - None of the above.

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?

A - request.getParameter()

B - request.getParameterValues()

C - request.getParameterNames()

D - None of the above.

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?

A - response.setContentType()

B - request.setContentType()

C - writer.setContentType()

D - None of the above.

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?

A - response.getWriter()

B - request.getWriter()

C - new PrintWriter()

D - None of the above.

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?

A - response.getCookies()

B - request.getCookies()

C - Cookies.getCookies()

D - None of the above.

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()

C - Header.getAttributeNames()

D - None of the above.

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?

A - response.getHeaderNames()

B - request.getHeaderNames()

C - Header.getHeaderNames()

D - None of the above.

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()

C - Header.getParameterNames()

D - None of the above.

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

servlets-questions-answers.htm

Advertisements