Chapter 1: The Servlet Model

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

D. IllegalArgumentException

12. What is the most likely outcome of running the following servlet code? (Choose one.) long date = request.getDateHeader("Host"); response.setContentType("text/plain"); response.getWriter().write("" + date); A. A formatted date is written to the response. B. 1 is written to the response. C. Won't run because won't compile. D. IllegalArgumentException E. NumberFormatException F. DateFormatException

C. POST

1. Which of the HTTP methods below is not considered to be "idempotent"? (Choose one.) A. GET B. TRACE C. POST D. HEAD74 E.OPTIONS F. SERVICE

B. Content-Type: application /x-www-form-urlencoded

10. What request header must be set for parameter data to be decoded from a form? (Choose one.) A. EncType: application /x-www-urlencoded B. Content-Type: application /x-www-form-urlencoded C. Content-Type: multipart /form-data D. Encoding-Type: multipart /form-data E. Accept-Encoding: application/www-form-encoded F. Encoding-Type: multipart/www-form-data

A. Accept C. Accept-Language D. From

11. Which of the following are likely to found as request header fi elds? (Choose three.) A. Accept B. WWW-Authenticate C. Accept-Language D. From E. Client-Agent F. Retry-After

D. Won't run: 2 compilation errors.

13. What is the likely outcome of attempting to run the following servlet code? String[] values = request.getHeaders("BogusHeader"); response.setContentType("text/plain"); response.getWriter().write(values[0]); A. IllegalArgumentException B. NumberFormatException C. Won't run: 1 compilation error. D. Won't run: 2 compilation errors. E. Nothing written to the response. F. null written to the response.

D. Output to the response including at least the name and value

14. What is the likely outcome of attempting to compile and run the following servlet code, assuming there is one cookie attached to the incoming request? 11 Cookie[] cookies = request.getCookies(); 12 Cookie cookie1 = cookies[0]; 13 response.setContentType("text/plain"); 14 String attributes = cookie1.getName(); 15 attributes += cookie1.getValue(); 16 attributes += cookie1.getDomain(); 17 attributes += cookie1.getPath(); 18 response.getWriter().write(attributes); A. Compilation error at line 11 B. Compilation error at line 16 C. Output to the response including at least the name and domain D. Output to the response including at least the name and value E. Output to the response including at least the name, value, and domain F. Output to the response including all of name, value, domain, and path

C If the container disallows access to the header information.

15. Under what circumstances can the HttpServletRequest.getHeaders(String name) method return null? (Choose one.) A. If there are no request headers present in the request for the given header name. B. If there are multiple headers for the given header name. C If the container disallows access to the header information. D. If there are multiple values for the given header name. E. If there is only a single value for the given header name. F. There is no such method on HttpServletRequest.

A. HttpServletResponse.addCookie(Cookie cookie) E. HttpServletResponse.addHeader(String name, String value)

16. Which of the following methods can be used to add cookies to a servlet response? (Choose two.) A. HttpServletResponse.addCookie(Cookie cookie) B. ServletResponse.addCookie(Cookie cookie) C. HttpServletResponse.addCookie(String contents) D. ServletResponse.addCookie(String contents) E. HttpServletResponse.addHeader(String name, String value) F. ServletResponse.addHeader(String name, String value)

B. A blank page is returned to the client. D. "true" is output on the server's console.

17. What is the outcome of running the following servlet code? (Choose two.) public void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/plain;charset-UTF-8"); PrintWriter out = response.getWriter(); out.flush(); out.close(); System.out.println(response.isCommitted()); response.setContentType("illegal/value"); } A An IllegalArgumentException is thrown. B. A blank page is returned to the client. C. A 500 error is reported to the client. D. "true" is output on the server's console. E. "false" is output on the server's console.

E. "What" is returned to the client.

18. What will be the outcome of executing the following code? (Choose one.) public void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/plain"); response.setContentLength(4); PrintWriter out = response.getWriter(); out.write("What will be the response? "); out.write("" + response.isCommitted()); } A. Won't execute because of a compilation error. B. An IllegalArgumentException is thrown. C. An IllegalStateException is thrown. D. A blank page is returned to the client. E. "What" is returned to the client. F. "What will be the response?" is returned to the client. G. "What will be the response? true" is returned to the client. H. "What will be the response? false" is returned to the client.

A. DELETE D. POST E. PUT

2. Which of the HTTP methods below are likely to change state on the web server? (Choose three.) A. DELETE B. TRACE C. OPTIONS D. POST E. PUT F. CONNECT G. HEAD H. SERVICE

A. response.sendRedirect("index.jsp"); F. response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT); response.setHeader("Location", "index.jsp");

20. Which of the approaches below will correctly cause a client to redirect to an alternative URL? (In the code fragments below, consider that "response" is an instance of HttpServletResponse.) (Choose two.) A. response.sendRedirect("index.jsp"); B. response.setLocation("index.jsp"); C. RequestDispatcher rd = response.getRequestDispatcher("index.jsp"); rd.sendRedirect(); D. response.redirect("index.jsp"); E. response.setHeader("Location", "index.jsp"); F. response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT); response.setHeader("Location", "index.jsp");

C. There could be anything from zero to many threads running through the service() method during the time the servlet is loaded. D. If the init() method for the servlet hasn't run, no threads have yet been able to run through the service() method.

21. Identify statements that are always true about threads running through the service() method of a servlet with the following class declaration. (Choose two.) public class MyServlet extends HttpServlet { // servlet code } A.The destroy() method never cuts short threads running through the service() method. B. Threads running through the service() method must run one at a time. C. There could be anything from zero to many threads running through the service() method during the time the servlet is loaded. D. If the init() method for the servlet hasn't run, no threads have yet been able to run through the service() method. E. At least one thread will run through the service() method if init() has been executed.

A. During web application startup C. On a client first requesting the servlet F. At some arbitrary point in the web application or application server lifetime G. After the time specified on an UnavailableException has expired

22. Under which of the following circumstances are servlets most likely to be instantiated? (Choose four.) A. During web application startup B. If there are insuffi cient instances of the servlet to service incoming requests C. On a client first requesting the servlet D. At the same time as a different servlet is instantiated, when that different servlet makes use of the servlet in question E. After the servlet's destroy() method is called, dependent on the server's keep-alive setting F. At some arbitrary point in the web application or application server lifetime G. After the time specified on an UnavailableException has expired

A. If a servlet is removed from service, then any requests to the servlet should result in an HTTP 404 (SC_NOT_FOUND) error. E. If a servlet is deemed temporarily unavailable; a container may return an HTTP 503 (SC_SERVICE_UNAVAILABLE) message on receiving requests to the servlet during its time of unavailability.

23. Which of the following are true statements about servlet availability? (Choose two.) A. If a servlet is removed from service, then any requests to the servlet should result in an HTTP 404 (SC_NOT_FOUND) error. B. The init() method must not throw an UnavailableException. C.If permanent unavailability is indicated via an UnavailableException, a servlet's destroy() method must be called. D. Servlet containers must distinguish between periods of temporary and permanent unavailability. E. If a servlet is deemed temporarily unavailable; a container may return an HTTP 503 (SC_SERVICE_UNAVAILABLE) message on receiving requests to the servlet during its time of unavailability.

B. When init() has not run to completion successfully D. After destroy() has already been called

24. Under what circumstances will a servlet instance's destroy() method never be called? (Choose two.) A. As a result of a web application closedown request B. When init() has not run to completion successfully C. If no thread has ever executed the service() method D. After destroy() has already been called E. During servlet replacement

D. destroy:service: F. init:service:init:service:

25.Given the following servlet code, identify the outputs that could not or should not occur during the lifetime of the web application housing the servlet. (choose 2) A. init:destroy: B. init:destroy:init:destroy: C. init:init:init: D. destroy:service: E. init:service:service:service:service:service: F. init:service:init:service:

A. doGet() B. doPost() D. doOptions() E. doHead()

3. Which of the following are valid servlet methods that match up with HTTP methods? (Choose four.) A. doGet() B. doPost() C. doConnect() D. doOptions() E. doHead() F. doRequest() G. doService()

C. 405 response code: SC_METHOD_NOT_ALLOWED.

4. What is the likely effect of calling a servlet with the POST HTTP method if that servlet does not have a doPost() method? (Choose one.) A. If the servlet has a doGet() method, it executes that instead. B.404 response code: SC_NOT_FOUND. C. 405 response code: SC_METHOD_NOT_ALLOWED. D. 500 response code: SC_INTERNAL_SERVER_ERROR. E. 501 response code: SC_NOT_IMPLEMENTED.

A.200 response code: SC_OK

5. What is the likely effect of calling a servlet with the HEAD HTTP method if that servlet does not have a doHead() method? (Choose one.) A.200 response code: SC_OK B. 404 response code: SC_NOT_FOUND C. 405 response code: SC_METHOD_NOT_ALLOWED D. 500 response code: SC_INTERNAL_SERVER_ERROR E. 501 response code: SC_NOT_IMPLEMENTED

C. A request is sent with the HTTP method GET. F. The parameters fullName and sbmButton are passed to the web server in the request URL.

6. What will be the result of pressing the submit button in the following HTML form? (Choose two.) <form action="/servlet/Register"> <input type="text" name="fullName" value="Type name here" /> <input type="submit" name="sbmButton" value="OK" /> </form> A. A request is sent with the HTTP method HEAD. B. A request is sent with the HTTP method POST. C. A request is sent with the HTTP method GET. D. The parameter fullName is the only parameter passed to the web server in the request URL. E. The parameter fullName is the only parameter passed to the web server as part of the request body. F. The parameters fullName and sbmButton are passed to the web server in the request URL. G. The parameters fullName and sbmButton are passed to the web server as part of the request body. H. No parameters are passed to the web server.

E. First:First:Second

7. Consider the following form and servlet code. Assuming the user changes none of the default settings and presses submit, what will the servlet output in the response? (Choose one.) <form action="PrintParams?param1=First" method="post"> <input type="hidden" name="param1" value="First" /> <input type="text" name="param1" value="Second" /> <input type="radio" name="param1" value="Third" /> <input type="submit" /> </form> protected void doPost HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.write("<html>\n<head>\n<title>Print Parameters</title>\n</head>\n<body>"); String[] param1 = request.getParameterValues("param1"); for (int i = 0; i < param1.length; i++) { out.write(param1[i] + ":"); } out.write("\n</body>\n</html>"); out.close(); } A. First:Second:Third B. First:Second:Second C. First:Third:Third D. Second:Third:First E. First:First:Second F. No response — servlet will not compile. G. No response — ServletException occurs.

H. 7

9. What is the maximum number of parameter values that can be forwarded to the servlet from the following HTML form? (Choose one.) <html> <body> <h1>Chapter 1 Question 9</h1> <form action="ParamsServlet" method="get"> <select name="Languages" size="3" multiple> <option value="JAVA" selected>Java</option> <option value="CSHARP">C#</option> <option value="C" selected>C</option> <option value="CPLUSPLUS">C++</option> <option value="PASCAL">Pascal</option> <option value="ADA">Ada</option> </select> <input type="submit" name="button" /> </form> </body> </html> A. 0 B. 1 C. 2 D. 3 E. 4 F. 5 G. 6 H. 7


Kaugnay na mga set ng pag-aaral

Physics- Quiz 1, Our View of the Universe

View Set

10 ASP Biology term 3 collection

View Set

ACE Health Coach Chapter 11 Quiz

View Set