Monday, April 4, 2011

Self Test: Chapters 25 to 29

Questions:

Question 1:

The sendError() method sends what type of information to the client?
A. Response footer.
B. Response header.
C. Content body.
D. None of the above.

Question 2:

Which of the following is a correct sendError() call?
A. response.sendError(HttpServletResponse.SC_FORBIDDEN, "Sorry, restricted to geeks.");
B. request.sendError(HttpServletResponse.SC_FORBIDDEN, "Sorry, restricted to geeks.");
C. request.sendError(HttpServletResponse.SC_FORBIDDEN);
D. response.sendError("Sorry, restricted to geeks.");

Question 3:

The Status-Code element is a three-digit integer code. The first digit of the Status-Code defines the class of response while the last two digits do not have categories, although they are defined in the standard. What classification is used for redirect?
A. 2XX.
B. 3XX.
C. 4XX.
D. 5XX.

Question 4:

The status codes have been defined by whom?
A. The DSN group.
B. Sun's expert group.
C. The Internet Society.
D. The Apache group.

Question 5:

You need to terminate the request due to an erroneous set of credentials. What method should your servlet respond with?
A. addHeader(String name, String value) method of the response object to add a response header with the given name and value.
B. terminate(String name, int value) to add a response header with the given name and integer value.
C. doTerminate(String location) to send a temporary redirect response to the client so they can try again.
D. response.sendError(HttpServletResponse. SC_NOT_ACCEPTABLE, "Sorry, your crentials are invalid. Please try again.");.

Question 6:

Which method is best for telling the client that all went well and the request was processed successfully?

A. Do nothing. This is the default.
B. setHeader()
C. setStatus()
D. setIntHeader()

Question 7:

Which method commits the response, effectively terminating it?
A. sendError()
B. setHeader()
C. setStatus()
D. finalize()

Question 8:

How can you return an exception object to the client?
A. You cannot do this.
B. Use sendError.
C. Use setStatus.
D. Use sendException.

Question 9:

Which of the following will throw an IllegalStateException exception?
A. setIntHeader(long)
B. setHeader(long)
C. Calling the destroy method before the buffer is flushed.
D. Calling sendError after the response is committed.

Answers:


Answer 1:

D. The sendError() method sends both header and content body type information. This method is overloaded to handle either header only or both.

Answer 2:

A. This method is overloaded with two signatures. Only A is correct. B and C are wrong because this method is part of the response object not the request object. D is wrong because you must supply a status code; you can't just send a String message.

Answer 3:

B. 3XX is used for redirection. The other codes are 2XX for continuation, 4XX for client error, and 5XX for server error.

Answer 4:

C. The Internet Society is the group who defined these codes. The other groups were not involved.

Answer 5:

D. This is the correct way to send a “Not Acceptable” status code of 406. The other answers are correct syntax, but don't send the proper status code to the client.

Answer 6:

A. This is a difficult question. B, C, and D can be used to send information to the client. C is close because it is sometimes used to send an “OK” status of 200 back to the client. However, A is the correct answer. This particular task should be left to the container.

Answer 7:

A. This is the only method that actually terminates the request. sendError() commits the response, if it has not already been committed, and terminates it. No further output to the client can be made because data written to the response after this method is called is ignored.

Answer 8:

B. Sending an exception object is not possible without a lot of tweaking. This action is not mentioned anywhere in the specification. See “Returning an Error Code to the Client.”

Answer 9:

D. Calling sendError will throw an IllegalStateException exception if the response was already sent (committed). None of the other choices will throw this exception.

Previous Chapter: Quick Recap - Chapters 25 to 29

Next Chapter: Chapter 30 - Servlet API

No comments:

Post a Comment

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers