Вы находитесь на странице: 1из 10

SCWCD 5.

0 Mock Exams

by www.javabeat.net

Objective 1 [The Servlet Technology Model]


1) Which of the following statements are true about HTTP's requests?

a. While using HTTP POST method, the requested target resource must be an active
resource like Servlets or JSP.
b. HTTP POST method is a less secure method as data is directly available in the URL
portion of the client's request.
c. There is no limit in the amount of data being sent from the client to server in the
case of HTTP's GET method.
d. Only the Response Header will be sent to the client in the case of HTTP's HEAD
request.

2) Which of the following actions will trigger Http Request on the client side?

a. Entering username/password and clicking the submit button.


b. Clicking a hyperlink displayed in the HTML page.
c. Enter a valid URL in the Browser's Address field and pressing the Enter key.
d. Using Javascript's API such as reload() to refresh the current page.

3) How to make a POST request when a user clicks the hyperlink in some HTML
page?

a. Add the attribute 'method = 'POST'' in the '<a>' tag.


b. Provide the attribute onclick with the value being 'doPost()'.
c. There is no way to invoke a HTTP POST method while clicking a hyperlink.

4) What method can be used to retrieve all the parameter names being sent as
part of the request by the client?

a. Use the method 'HttpServletRequest.getParameterNames()' which will return an


enumeration of parameter names.
b. Use the method 'HttpServletResponse.getParameterNames()' which will return an
enumeration of parameter names.
c. Use the method 'HttpServletRequest.getAllParameters()' which will return an
enumeration of parameter names.
d. There is no direct support in the Servlet API to retrieve the name of all the
parameters sent by the client.

5) Which of the following statements are true about Java Servlets Technology?

a. A Servlet is a Server-side entity that will rest only on the Server.


b. The life-cycle management of Servlets is done by the Servlet Container.
c. Java Servlet Technology provides support for handling XML requests through a
special Servlet called XmlServlet.
d. A Servlet instance is created by the client during the first request to the Servlet
Container.

6) Which of the following method is used to send response in the form of


character data to the client?

a. HttpServletRequest.getWriter();
b. HttpServlerResponse.getCharacterWriter();
c. HttpServetRequest.getOutputStream()
d. HttpServletResonse.getWriter();

7) What you can infer from the following Servlet?


package scwcd14.chap01;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class MyServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {

PrintWriter out = response.getWriter();


out.print("My Servlet");

response.setContentType("text/html");
out.close();
}

public void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
doPost(request, response);
}
}
a. The Servlet sends a response in simple html content with some data.
b. Setting the content type doesn't have any effect in the response.
c. An IllegalStateException will be thrown at the run-time because the method
setContentType() should be called before sending the response to the client.
d. For sending character data to the Client Application, getOutputStream() must be
used instead of getWriter().

8) Assuming that we want a develop a multi-purpose Servlet using which we want


to send both character and byte data to the Client browser. What will be the
output of the following code?
package scwcd14.chap01;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;
public class MultiPurposeServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {

PrintWriter writer = response.getWriter();


writer.print("Character data");
writer.close();

byte[] byteArray = "Binary data".getBytes();


BufferedOutputStream stream = new BufferedOutputStream(response.getOutputStream())
stream.write(byteArray, 0, byteArray.length);
stream.close();
}
}
a. The HTML Browser will have the character data being retrieved first followed by the
binary data.
b. It is possible to display only one type of data to the HTML Browser in which case the
character data takes preference over the Binary data.
c. It is possible to display only one type of data to the HTML Browser in which case the
binary data takes preference over the character data.
d. An exception of type IllegalStateException will be thrown at the run-time.

9) Which of the following statements are true?

a. The Client Application calls the Servlet's load() method to load the Servlet.
b. The service() method will be called first followed by init() and destroy().
c. A new Servlet instance will be created by the Servlet Container upon client's
request.
d. If a client has done with a particular Servlet, then it should call the destroy method
to remove its instance from the Server address space.
e. None of the above.

10) What might be the output of the following program if a HTTP GET Request is
made on the Servlet object?
package scwcd14.chap01;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class ServletLifeCycleTest extends HttpServlet {

public ServletLifeCycleTest(){
System.out.println("ctor");
}

public void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
System.out.println("doGet");
}
public void service(){
System.out.println("service");
}

public void init(){


System.out.println("init");
}

public void destroy(){


System.out.println("destroy");
}
}
a. The program will output 'ctor service init doGet destroy'.
b. The program will output 'ctor init doGet destroy'.
c. The program will output 'ctor service doGet destroy'.
d. The program will output 'ctor init service doGet destroy'.

11) Which of the following methods can be used to return the supported HTTP
methods on a Web Server?
a. GET
b. PUT
c. POST
d. HEAD
e. OPTIONS
f. TRACE

12) Which of the following symbols is used to identifying the beginning of the
query string and the consecutive name-value pairs in a request?

a. ? and &
b. & and =
c. ? and =
d. % and &

13) The request header fields like 'If-Modified-Since', 'If-Unmodified-Since', etc


can be used with which HTTP method?

a. TRACE
b. PUT
c. GET
d. OPTIONS
e. DELETE

14) Which of the following statements are true about HTTP methods?

a. Book marking a page cannot be done in the case of HTTP's GET request.
b. To obtain information from a database, use HTTP's POST method.
c. POST method can be submitted from with a form.
d. HEAD method can be used to retrieve only the response body.
e. None of the above.

15) Which of the following methods defines the correct signature of the doGet()
method?

a. public void doGet(ServletRequest req, ServletResponse res)


b. public void doGet(ServletRequest req, ServletResponse res) throws IOException
c. public void doGet(ServletRequest req, ServletResponse res) throws IOException,
ServletException
d. protected void doGet(ServletRequest req, ServletResponse res) throws IOException,
ServletException
e. None of the above.

16) Assuming that the following is a valid request,

GET /Register/index.html HTTP/1.0


Accept-Encoding: gzip, deflate
What would be return type of the method request.getHeader("Accept-Encoding");
a. A string whose value is equal to "gzip, deflate".
b. A string array whose elements are "gzip" and "deflate"
c. A string value whose value is equal to "gzip".
d. A null value.

17) Which of the following interfaces provide access to the method


getRequestedSessionId()?

a. HttpSession
b. HttpServletRequest
c. HttpServletResponse
d. ServletContext

18) What will be the value of the context path, Servlet path and the path info
elements, if the request path is music/Play/album1234?name='xyz' (assuming
that the context of the Web Application is 'music')?

a. Play, album1234 and name='xyz'


b. music, Play and album1234?name='xyz'
c. music, Play and album1234
d. music, Play/ album1234 and name='xyz'

19) The Servlet 'CallingServlet' uses the RequestDispatcher mechanism to forward


the control to another Servlet called 'CalledServlet'. What will be the output of the
following program?

CallingServlet
public void doGet(….)
{
PrintWriter out = respone.getWriter();

out.println("Output from Calling Servlet");

RequestDisparcher dispatcher =
request.getRequestDispatcher("/CalledServlet");
dispatcher.forward(req, res);

out.flush();
out.close();
}
CalledServlet
public void doGet(…)
{
PrintWriter out = respone.getWriter();

out.println("Output from Called Servlet");


out.flush();
out.close();

}
a. The output will be 'Output from Calling Servlet' followed by 'Output from Called
Servlet'.
b. The output will be 'Output from Called Servlet' followed by 'Output from Calling
Servlet'.
c. The program will throw IllegalStateException at run-time.
d. The program will output 'Output from Called Servlet'.
e. None of the above.

20) Which of the following interfaces can be used to acquire a reference to


ServletContext object?

a. HttpServletRequest
b. HttpServletResponse
c. HttpSession
d. HttpServletConfig
e. ServletConfig

21) Which of the following statements are true about Error Handling?

a. An incorrectly formatted request message from client to server will result in HTTP
400 – Bad Request error message.
b. A response status code of 200 means that the request has been processed
successfully.
c. A response status code of 401 means that the request needs authentication.
d. All the above.

22) Which of the following statements are correct regarding Request types?

a. The doGet() method is generally used for retrieving information from the server.
b. POST request can send unlimited amount of data from client to server.
c. Data is given more protection in GET method rather than POST method.
d. All the above.

23) Which of the following statements are true about the OPTIONS method type?

a. The OPTIONS method can be used to identify the list of supported HTTP methods on
the Server.
b. The OPTIONS method can be used to identify the optional HTTP methods available
on the Server.
c. The response header as a result of making OPTIONS request will contain the header
attribute 'Allow' whose value contains the supported method types.
d. All the above.
24) Assume that you have an input control by name 'countries' that allows you to
select a list of country names. Which of the following code can be placed in the
Servlet to get the list of selected country names?

a. String[] countryNames = request.getParameterValues("countries");


b. List countryNames = (List)request.getParameterValues("countries");
c. String countryNames = request.getParameterValues("countries");
d. None of the above.

25) Pick out the following methods which are used to provide binary as well as
character stream for transferring data from Server to client?

a. HttpServletResponse.getBinaryStream() and HttpServletResponse.getCharWriter()


b. HttpServletResponse.getOutputStream() and HttpServletResponse.getWriter()
c. HttpServletResponse.getWriter() and HttpServletResponse.getOutputStream()
d. None of the above.

26) Which of the following methods is used to get a list of attributes that were
previously set on the Request object?

a. List HttpServletRequest.getAttributeNames()
b. Enumeration HttpServletRequest.getAttributes()
c. Enumeration HttpServletRequest.getAttributeNames()
d. None of the above.

27) Pick out the context path, Servlet path and path information from the request
path '/books/Find/abcd?query=Java' (assuming that the context is 'books')?

a. '/books', '/Find' and '/abcd'


b. '/books', '/Find/abcd' and 'query=Java'
c. '/books/Find', '/abcd' and 'query=Java'
d. None of the above.

28) An image file representing a company's logo has to be uploaded to the server.
Which of the following HTTP methods can be used in this situation?

a. doGet()
b. doPost()
c. doTrace()
d. doPut()

29) The body of the response will be blank if a HEAD request is made to server. Is
this statement correct?

a. Yes
b. No

30) In which units, the 'session-timeout' tag is identified in the deployment


descriptor?

a. Milliseconds
b. Seconds
c. Minutes
d. None of the above.

Answers
1) a and d.
Option b is incorrect as HTTP GET method is less secure because the Request information
will be available as part of the URL. Only a limited set of information can be passed through
HTTP's GET method, so option b is incorrect.

2) a, b, c and d.
There is a possibility of a Request being triggered for all the operations defined.

3) c.
There is no way to trigger a HTTP POST method while clicking a hyperlink, so option c is
correct.

4) a.
Using HttpServletRequest and by calling the method getParamterNames(), all the parameter
names can be retrieved.

5) a and b.
Option c is incorrect as the Java Servlets Specification doesn't provide support for handling
XML-based requests. While a Servlet instance is created upon first client's request or even
before that differs from Servlet Container's implementation.

6) d.
A Response object is needed for sending the response to the Client Application. For sending
character-based response, the method getWriter() has to be called on the
HttpServletResponse object. So option d is correct.

7) b.
The content type of the response should be called well before writing the response. So,
calling the method setContentType() doesn't have any effect as the response is committed
even before that. So option b is correct.

8) d.
It is possible to produce only one type of output (either character-based or binary-based),
so a run-time IllegalStateException will be thrown by the Servlet Container.

9) e.
None of the above statements are true.

10) b.
When a Servlet Container creates a new instance of the Servlet, the Servlet's constructor
will be invoked. After that the method init() will be invoked automatically by the Servlet
Container. Since the method is HTTP GET method, the doGet() will called and finally
followed by the destroy() method.

11) e
OPTIONS method is used to retrieve the supported methods for a Web Server.

12) a
Option a is correct. An example query string might be http://someServer.org?a=b&c=d

13) c
Option c is correct. These fields are normally used as part of conditional GET requests.

14) c
Option a is incorrect as book-marking a page can be achieved through HTTP GET method.
POST method is generally used to post information, and hence option b is incorrect. Option
d is incorrect because HEAD method can be used to retrieve the header values that are
associated with a response and not the body of the response.

15) e
None of the above. The doGet() method will take HttpServletRequest and
HttpServletResponse as arguments. So none of the four options are correct. The correct
method signature of the doGet() method in a class that extends HttpServlet may take any
of the following forms.
public void doGet(HttpServletRequest, HttpServletResponse)
Or
public void doGet(HttpServletRequest, HttpServletResponse) throws IOException,
ServletException.

16) a
Option a is correct. If we would have used request.getHeaders(""), then option b is correct.

17) b
Option b is correct. The method getRequestedSessionId() is available in the interface
HttpServletRequest

18) d
Option d is correct.

19) d
Option d is correct. Since the Servlet 'CallingServlet' has forwarded its control to the
'CalledServlet', any response that has been written already by the CallingServlet will be
simply ignored by the target Servlet.

20) a and e
Both the interfaces HttpServletRequest and ServletConfig defines the method
getServletContext().

21) d.
All the above options are correct.

22) a and b.
Options a and b are correct. Option c is incorrect since the request data is passed on
through the body in POST method and hence data protection is more in POST method rather
than in GET method.

23) a and c.
Options a and c are correct.

24) a.
Option a is correct. When a parameter corresponds to multiple values, then a call to
getParameterValues() by passing in the parameter name will return a string array
containing all the parameter values.

25) b.
Option b is correct. The methods getOutputStream() and getPrintWriter() defined on the
HttpResponse object can be used to send binary and character data to the clients.

26) c.
Option c is correct. The method getAttributeNames() defined on the HttpServletRequest
object is used to retrieve the attribute names which will be returned to the caller as an
Enumeration.

27) a.
Option a is correct. The context path, Servlet path and the path information are '/books',
'/Find' and '/abcd' respectively.

28) d.
Option d is correct since uploading a file on the server is more like publishing some
resource, for which case doPut() is the ideal method.

29) a.
Option a is correct. A HEAD request contains response in the form of headers only. There
will be no response body.

30) c.
The value specified for the 'session-timeout' tag is identified in minutes in the Deployment
Descriptor.

References

Group :: http://tech.groups.yahoo.com/group/JavaBeat_SCWCD/

Forums :: http://forums.javabeat.net/index.php?board=3.0

Books :: http://www.javabeat.net/cert/scwcd-books/

Вам также может понравиться