Home > Articles

  • Print
  • + Share This
This chapter is from the book

Practice Questions

Question 1

Read the following code snippet:

<listener>
 <listener-class>myContextListener
         </listener-class>
</listener>
<listener>
 <listener-class>
    mySessionAttributeListener
 </listener-class>
</listener>

Which two statements are true regarding this snippet? (Choose two.)

  1. The container calls the appropriate methods, such as attributeAdded and attributeRemoved, in the classes specified in the listener-class element.

  2. The methods in the classes specified in the listener-class element call the attributeAdded and attributeRemoved methods.

  3. The methods such as attributeAdded and attributeRemoved are defined in the classes specified in the listener-class element.

  4. The listener-class element goes in server.xml.

Answers A and C are correct. The listener-class element declares that a class in the application must be registered as a Web application listener bean. The value is the fully qualified class name of the listener class. In these classes, you can add the attributeAdded and attributeRemoved methods.

Question 2

What is the correct declaration of a context parameter?

  1.   <context-param>
        <name>publisher</name>
        <value>QUE</value>
      </context-param>
  2.   <context_param>
        <param_name>publisher</param_name>
        <param_value>QUE</param_value>
      </context_param>
  3.   <context>
        <name>publisher</name>
        <value>QUE</value>
      </context>
  4.   <context-param>
        <param-name>publisher</param-name>
        <param-value>QUE</param-value>
      </context-param>

Answer D is correct. This is the correct way to declare a context parameter named publisher with a value of QUE. All the other options have incorrect elements.

Question 3

What parameter is passed to the attributeAdded method for a session attribute?

  1. HttpSessionListenerEvent

  2. HttpSessionEventEvent

  3. HttpSessionBindingEvent

  4. HttpSessionAttributeEvent

Answer C is correct. Only the HttpSessionBindingEvent object is passed to attributeAdded by the container when an attribute is added to a session.

Question 4

Regarding the Web application directory structure, what is the root directory?

  1. CATALINA_HOME\webapps\

  2. CATALINA_HOME\webapps\WEB-INF\

  3. CATALINA_HOME\WEB-INF\webapps\

  4. CATALINA_HOME\WEB-INF\

Answer A is correct. Option A is the root directory for the Web application also known as the context.

Question 5

Which two of the following are elements of the Web application descriptor? (Choose two.)

  1. init-type

  2. init-param

  3. listener-class

  4. error-class

Answers B and C are correct. There is neither an init-type element nor an error-class element.

Question 6

What interface do you implement if you want to use the attributeAdded method?

  1. HttpSessionListener

  2. HttpSessionAttributeListener

  3. SessionAttributeListener

  4. SessionAttributeListener

Answer B is correct. The HttpSessionAttributeListener interface enables you to override the attributeAdded method, which is called by the container when an attribute is added to a session. HttpSessionListener enables you to listen to the notification that a session is created or destroyed. The other two options are not valid interfaces.

Question 7

Given the contextInitialized(ServletContextEvent event) method, how do you get the context (assuming that the request object is properly assigned)?

  1. ServletContext context = request.getServletContext();
  2. ServletContext context = event.getServletContext();
  3. ServletSession context = event.getContext();
  4. ServletContext context = request.getSessionContext();

Answer B is correct. This option is the only correct syntax shown. The others have incorrect syntax.

  • + Share This
  • 🔖 Save To Your Account