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.)
-
The container calls the appropriate methods, such as attributeAdded and attributeRemoved, in the classes specified in the listener-class element.
-
The methods in the classes specified in the listener-class element call the attributeAdded and attributeRemoved methods.
-
The methods such as attributeAdded and attributeRemoved are defined in the classes specified in the listener-class element.
-
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?
-
<context-param> <name>publisher</name> <value>QUE</value> </context-param>
-
<context_param> <param_name>publisher</param_name> <param_value>QUE</param_value> </context_param>
-
<context> <name>publisher</name> <value>QUE</value> </context>
-
<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?
-
HttpSessionListenerEvent
-
HttpSessionEventEvent
-
HttpSessionBindingEvent
-
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?
-
CATALINA_HOME\webapps\
-
CATALINA_HOME\webapps\WEB-INF\
-
CATALINA_HOME\WEB-INF\webapps\
-
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.)
-
init-type
-
init-param
-
listener-class
-
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?
-
HttpSessionListener
-
HttpSessionAttributeListener
-
SessionAttributeListener
-
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)?
-
ServletContext context = request.getServletContext();
-
ServletContext context = event.getServletContext();
-
ServletSession context = event.getContext();
-
ServletContext context = request.getSessionContext();
Answer B is correct. This option is the only correct syntax shown. The others have incorrect syntax.