Free SCWCD Mock exam for Custom Tags

9 May 2009 by Nikos

This practice exam focuses on Custom Tags. For those using Head First Servlets and JSP, 2nd Edition it is a perfect companion for chapter 10.

To get basic skills before going on, you may consider this first.

  1. Is this a valid use of header.tag, a JSP tag file that is located in WEB-INF/tags? (1 correct answer)
    <%@ taglib prefix="my" tagdir="/WEB-INF/tags" %>
    <html>
      <body>
        <my:header />
      </body>
    </html>
    
    1. Yes.
    2. No.

  2. Is this a valid use of header.tag, a JSP tag file that is located in WEB-INF/tags? (1 correct answer)
    <%@ taglib prefix="my" tagdir="/WEB-INF/tags/header.tag" %>
    <html>
      <body>
        <my:header />
      </body>
    </html>
    
    1. Yes.
    2. No.

  3. Is this a valid use of header.tag, a JSP tag file that is located in WEB-INF/tags? (1 correct answer)
    <%@ taglib prefix="my" uri="/WEB-INF/tags" %>
    <html>
      <body>
        <my:header />
      </body>
    </html>
    
    1. Yes.
    2. No.

  4. Is this a valid use of header.tag, a JSP tag file that is located in WEB-INF/tags? (1 correct answer)
    <%@ taglib prefix="my" uri="/WEB-INF/tags/header.tag" %>
    <html>
      <body>
        <my:header />
      </body>
    </html>
    
    1. Yes.
    2. No.

  5. Is this a valid use of header.tag, a JSP tag file that is located in WEB-INF/tags? (1 correct answer)
    <%@ taglib prefix="my" tagdir="/WEB-INF/tags/" uri="http://nikojava.wordpress.com" %>
    <html>
      <body>
        <my:header />
      </body>
    </html>
    
    1. Yes.
    2. No.

  6. Is this a valid use of header.tag, a JSP tag file that is located in WEB-INF/tags? (1 correct answer)
    <%@ taglib prefix="header" tagdir="/WEB-INF/tags" %>
    <html>
      <body>
        <header:header />
      </body>
    </html>
    
    1. Yes.
    2. No.

  7. Is this a valid use of header.tag, a JSP tag file that is located in WEB-INF/tags? (1 correct answer)
    <%@ taglib prefix="1" tagdir="/WEB-INF/tags" %>
    <html>
      <body>
        <1:header />
      </body>
    </html>
    
    1. Yes.
    2. No.

  8. Consider these files.
    <%-- WEB-INF/tags/header.tag --%>
    <h1>Hello !!</h1>
    
    <%-- output.jsp --%>
    <%@ taglib prefix="my" tagdir="/WEB-INF/tags" %>
    <html>
      <body>
        <my:header></my:header>
      </body>
    </html>
    

    Is the declaration and use of the JSP tag file valid? (1 correct answer)

    1. Yes.
    2. No.

  9. Consider these files.
    <%-- WEB-INF/tags/header.tag --%>
    <h1>Hello ${Manoli}!!</h1>
    
    <%-- output.jsp --%>
    <%@ taglib prefix="my" tagdir="/WEB-INF/tags" %>
    <html>
      <body>
        <my:header name="Manoli"/>
      </body>
    </html>
    

    Is the declaration and use of the JSP tag file valid? (1 correct answer)

    1. Yes.
    2. No.

  10. Consider these files.
    <%-- WEB-INF/tags/header.tag --%>
    <h1>Hello ${name}!!</h1>
    
    <%-- output.jsp --%>
    <%@ taglib prefix="my" tagdir="/WEB-INF/tags" %>
    <html>
      <body>
        <my:header name="Manoli"/>
      </body>
    </html>
    

    Is the declaration and use of the JSP tag file valid? (1 correct answer)

    1. Yes.
    2. No.

  11. Consider these files.
    <%-- WEB-INF/tags/header.tag --%>
    <%@ attribute name="name" %>
    <h1>Hello ${name}!!</h1>
    
    <%-- output.jsp --%>
    <%@ taglib prefix="my" tagdir="/WEB-INF/tags" %>
    <html>
      <body>
        <my:header name="Manoli"/>
      </body>
    </html>
    

    Is the declaration and use of the JSP tag file valid? (1 correct answer)

    1. Yes.
    2. No.

  12. Consider these files.
    <%-- WEB-INF/tags/header.tag --%>
    <%@ attribute name="name" %>
    <h1>Hello ${name}!!</h1>
    
    <%-- output.jsp --%>
    <%@ taglib prefix="my" tagdir="/WEB-INF/tags" %>
    <html>
      <body>
        <my:header name="Manoli"></my:header>
      </body>
    </html>
    

    Is the declaration and use of the JSP tag file valid? (1 correct answer)

    1. Yes.
    2. No.

  13. Consider these files.
    <%-- WEB-INF/tags/simple.tag --%>
    <%@ attribute name="city" required="false" %>
    <%@ tag pageEncoding="utf-8"%>
    This is ${city}.
    
    <%-- output.jsp --%>
    <%@ taglib prefix="ayo" tagdir="/WEB-INF/tags" %>
    <html>
      <body>
        <ayo:simple city="Sparta"/>
      </body>
    </html>
    

    What is the output? (1 correct answer)

    1. “This is .”
    2. “This is Sparta.”
    3. An exception occurs at runtime.

  14. Consider these files.
    <%-- WEB-INF/tags/simple.tag --%>
    <%@ attribute name="city" required="true" %>
    <%@ tag pageEncoding="utf-8"%>
    This is ${city}.
    
    <%-- output.jsp --%>
    <%@ taglib prefix="ayo" tagdir="/WEB-INF/tags" %>
    <html>
      <body>
        <ayo:simple/>
      </body>
    </html>
    

    What is the output? (1 correct answer)

    1. “This is .”
    2. “This is Sparta.”
    3. An exception occurs at runtime.

  15. Consider these files.
    <%-- WEB-INF/tags/simple.tag --%>
    <%@ attribute name="city" %>
    <%@ tag pageEncoding="utf-8"%>
    This is ${city}.
    
    <%-- output.jsp --%>
    <%@ taglib prefix="ayo" tagdir="/WEB-INF/tags" %>
    <% request.setAttribute("city", "Sparta");%>
    <html>
      <body>
        <ayo:simple city="${city}"/>
      </body>
    </html>
    

    What is the output? (1 correct answer)

    1. “This is .”
    2. “This is Sparta.”
    3. An exception occurs at runtime.

  16. Consider these files.
    <%-- WEB-INF/tags/simple.tag --%>
    <%@ attribute name="city" rtexprvalue="false" %>
    <%@ tag pageEncoding="utf-8"%>
    This is ${city}.
    
    <%-- output.jsp --%>
    <%@ taglib prefix="ayo" tagdir="/WEB-INF/tags" %>
    <% request.setAttribute("city", "Sparta");%>
    <html>
      <body>
        <ayo:simple city="${city}"/>
      </body>
    </html>
    

    What is the output? (1 correct answer)

    1. “This is .”
    2. “This is Sparta.”
    3. An exception occurs at runtime.

  17. Consider these files.
    <%-- WEB-INF/tags/simple.tag --%>
    <%@ attribute name="city" rtexprvalue="true" %>
    <%@ tag pageEncoding="utf-8"%>
    This is ${city}.
    
    <%-- output.jsp --%>
    <%@ taglib prefix="ayo" tagdir="/WEB-INF/tags" %>
    <html>
      <body>
        <ayo:simple city="${1 ge 10}"/>
      </body>
    </html>
    

    What is the output? (1 correct answer)

    1. “This is true.”
    2. “This is false.”
    3. An exception occurs at runtime.

  18. Consider these files.
    <%-- WEB-INF/tags/simple.tag --%>
    <%@ attribute name="city" rtexprvalue="true" %>
    <%@ tag pageEncoding="utf-8"%>
    This is ${city}.
    
    <%-- output.jsp --%>
    <%@ taglib prefix="ayo" tagdir="/WEB-INF/tags" %>
    <html>
      <body>
        <ayo:simple capital="${1 ge 10}"/>
      </body>
    </html>
    

    What is the output? (1 correct answer)

    1. “This is true.”
    2. “This is false.”
    3. An exception occurs at runtime.

  19. Consider these files.
    <%-- WEB-INF/tags/body.tag --%>
    Start <jsp:doBody /> End
    
    <%-- output.jsp --%>
    <%@ taglib prefix="ayo" tagdir="/WEB-INF/tags" %>
    <html>
      <body>
        <ayo:body>Middle</ayo:body>
      </body>
    </html>
    

    What is the output? (1 correct answer)

    1. “Start End”
    2. “Start Middle End”
    3. An exception occurs at runtime.

  20. Consider these files.
    <%-- WEB-INF/tags/body.tag --%>
    <%@ tag body-content="scriptless" %>
    Start <jsp:doBody /> End
    
    <%-- output.jsp --%>
    <%@ taglib prefix="ayo" tagdir="/WEB-INF/tags" %>
    <% request.setAttribute("position", "Middle"); %>
    <html>
      <body>
        <ayo:body>${position}</ayo:body>
      </body>
    </html>
    

    What is the output? (1 correct answer)

    1. “Start End”
    2. “Start Middle End”
    3. An exception occurs at runtime.

  21. Consider these files.
    <%-- WEB-INF/tags/body.tag --%>
    <%@ tag body-content="tagdependent" %>
    Start <jsp:doBody /> End
    
    <%-- output.jsp --%>
    <%@ taglib prefix="ayo" tagdir="/WEB-INF/tags" %>
    <% request.setAttribute("position", "Middle"); %>
    <html>
      <body>
        <ayo:body><%= request.getAttribute("position") %></ayo:body>
      </body>
    </html>
    

    What is the output? (1 correct answer)

    1. “Start End”
    2. “Start Middle End”
    3. An exception occurs at runtime.

  22. SimpleTagSupport is a class that belongs to package javax.servlet.jsp.tagext. (1 correct answer)
    1. true
    2. false

  23. SimpleTagSupport IS-A SimpleTag. (1 correct answer)
    1. true
    2. false

  24. SimpleTagSupport IS-A JspTag. (1 correct answer)
    1. true
    2. false

  25. How many times is the doTag() method of SimpleTagSupport called on each tag invocation? (1 correct answer)
    1. One.
    2. It depends on the container.

  26. Consider class SimpleTagSupport. What is the signature of its method doTag? (1 correct answer)
    1. public void doTag() throws ServletException
    2. public void doTag() throws IOException, JspException

  27. Consider a simple tag handler that is nested, has attributes and a body. Here are the actions taken by the Container when this tag is invoked. Place them in the correct order starting from what happens first.
    1. Calls the setJspBody(JspFragment).
    2. Calls the no-arg constructor.
    3. Calls the doTag() method.
    4. Calls the setParent(JspTag).
    5. Calls the attribute setters.
    6. Calls the setJspContext(JspContext).

  28. Assuming all imports are correct, will this code compile successfully? (1 correct answer)
    public class Test extends SimpleTagSupport {
    public void doTag() throws IOException, JspException {
        getJspContext().getOut().println("A simple tag handler!!");
    }
    }
    
    1. Yes.
    2. No.

  29. Assuming all imports are correct, will this code compile successfully? (1 correct answer)
    public class Test extends SimpleTagSupport {
    public void doTag() throws IOException, JspException {
        getJspContext().getOut().append("A simple tag handler!!");
    }
    }
    
    1. Yes.
    2. No.

  30. Assuming all imports are correct, will this code compile successfully? (1 correct answer)
    public class Test extends SimpleTagSupport {
    public void doTag() throws IOException, JspException {
        getJspContext().getOut().format("A simple tag handler!!");
    }
    }
    
    1. Yes.
    2. No.

  31. Assuming all imports are correct, will this code compile successfully? (1 correct answer)
    public class Test extends SimpleTagSupport {
    public void doTag() throws IOException {
        getJspContext().getOut().print("A simple tag handler!!");
    }
    }
    
    1. Yes.
    2. No.

  32. Assuming all imports are correct, will this code compile successfully? (1 correct answer)
    public class Test extends SimpleTagSupport {
    public void doTag() throws Exception {
        getJspContext().getOut().print("A simple tag handler!!");
    }
    }
    
    1. Yes.
    2. No.

  33. Is this code a valid simple tag handler? (1 correct answer)
    public class Test extends SimpleTagSupport {
    public Test(String name) {
    }
    public void doTag() throws IOException, JspException {
        getJspContext().getOut().println("A simple tag handler!!");
    }
    }
    
    1. Yes.
    2. No.

  34. Test IS-A SimpleTagSupport in package simpletaghandlers. Is it correctly declared in a TLD? (1 correct answer)
    <tag>
      <tag-class>simpletaghandlers.Test</tag-class>
      <body-content>empty</body-content>
    </tag>
    
    1. Yes.
    2. No.

  35. Test IS-A SimpleTagSupport in package simpletaghandlers. Is it correctly declared in a TLD? (1 correct answer)
    <tag>
      <name>simple</name>
      <body-content>empty</body-content>
    </tag>
    
    1. Yes.
    2. No.

  36. Test IS-A SimpleTagSupport in package simpletaghandlers. Is it correctly declared in a TLD? (1 correct answer)
    <tag>
      <name>simple</name>
      <tag-class>Test</tag-class>
      <body-content>empty</body-content>
    </tag>
    
    1. Yes.
    2. No.

  37. Test IS-A SimpleTagSupport in package simpletaghandlers. Is it correctly declared in a TLD? (1 correct answer)
    <tag>
      <name>simple</name>
      <tag-class>simpletaghandlers.Test</tag-class>
    </tag>
    
    1. Yes.
    2. No.

  38. Test IS-A SimpleTagSupport in package simpletaghandlers. Is it correctly declared in a TLD? (1 correct answer)
    <tag>
      <name>simple</name>
      <tag-class>simpletaghandlers.Test</tag-class>
      <body-content>empty</body-content>
    </tag>
    
    1. Yes.
    2. No.

  39. mytaglib.tld is a valid TLD with <uri>/webapp/mytaglib</uri> and a simple tag handler with <name>simple</name>. Is this a valid use of the tag handler? (1 correct answer)
    <%@ taglib tagdir="/WEB-INF/" prefix="show"%>
    <html>
      <body>
        <show:simple />
      </body>
    </html>
    
    1. Yes.
    2. No.

  40. mytaglib.tld is a valid TLD with <uri>/webapp/mytaglib</uri> and a simple tag handler with <name>simple</name>. Is this a valid use of the tag handler? (1 correct answer)
    <%@ taglib tagdir="/WEB-INF/mytaglib.tld" prefix="show"%>
    <html>
      <body>
        <show:simple />
      </body>
    </html>
    
    1. Yes.
    2. No.

  41. mytaglib.tld is a valid TLD with <uri>/webapp/mytaglib</uri> and a simple tag handler with <name>simple</name>. Is this a valid use of the tag handler? (1 correct answer)
    <%@ taglib tagdir="/webapp/mytaglib" prefix="show"%>
    <html>
      <body>
        <show:simple />
      </body>
    </html>
    
    1. Yes.
    2. No.

  42. mytaglib.tld is a valid TLD with <uri>/webapp/mytaglib</uri> and a simple tag handler with <name>simple</name>. Is this a valid use of the tag handler? (1 correct answer)
    <%@ taglib uri="/webapp/mytaglib" prefix="show"%>
    <html>
      <body>
        <show:simple />
      </body>
    </html>
    
    1. Yes.
    2. No.

  43. Assume that everything is declared properly and the TLD specifies <body-content>scriptless</body-content>. What is the output of the JSP? (1 correct answer)
    public class Repeater extends SimpleTagSupport {
      public void doTag() throws JspException, IOException {
        getJspBody().invoke(null);
      }
    }
    
    <%@ taglib uri="/webapp/mytaglib" prefix="do" %>
    <html>
      <body>
        (Before)
        <do:repeater />
        (After)
      </body>
    </html>
    
    1. (Before)(After)
    2. An exception occurs at runtime.

  44. Assume that everything is declared properly and the TLD specifies <body-content>scriptless</body-content>. What is the output of the JSP? (1 correct answer)
    public class Repeater extends SimpleTagSupport {
      public void doTag() throws JspException, IOException {
        if (getJspBody() == null) {
          getJspContext().getOut().print("Surprise");
        } else {
          getJspBody().invoke(null);
        }
      }
    }
    
    <%@ taglib uri="/webapp/mytaglib" prefix="do" %>
    <html>
      <body>
        (Before)
        <do:repeater></do:repeater>
        (After)
      </body>
    </html>
    
    1. (Before)(After)
    2. (Before)Surprise(After)
    3. An exception occurs at runtime.

  45. Assume that everything is declared properly and the TLD specifies <body-content>scriptless</body-content>. What is the output of the JSP? (1 correct answer)
    public class Repeater extends SimpleTagSupport {
      public void doTag() throws JspException, IOException {
        getJspBody().invoke(null);
      }
    }
    
    <%@ taglib uri="/webapp/mytaglib" prefix="do" %>
    <html>
      <body>
        (Before)
        <do:repeater>Surprise</do:repeater>
        (After)
      </body>
    </html>
    
    1. (Before)(After)
    2. (Before)Surprise(After)
    3. An exception occurs at runtime.

  46. Assume that everything is declared properly and the TLD specifies <body-content>empty</body-content>. What is the output of the JSP? (1 correct answer)
    public class Repeater extends SimpleTagSupport {
      public void doTag() throws JspException, IOException {
        getJspBody().invoke(null);
      }
    }
    
    <%@ taglib uri="/webapp/mytaglib" prefix="do" %>
    <html>
      <body>
        (Before)
        <do:repeater>Surprise</do:repeater>
        (After)
      </body>
    </html>
    
    1. (Before)(After)
    2. (Before)Surprise(After)
    3. An exception occurs at runtime.

  47. Assume that everything is declared properly and the TLD specifies <body-content>scriptless</body-content>. What is the output of the JSP? (1 correct answer)
    public class Repeater extends SimpleTagSupport {
      public void doTag() throws JspException, IOException {
        getJspBody().invoke(null);
        getJspBody().invoke(null);
      }
    }
    
    <%@ taglib uri="/webapp/mytaglib" prefix="do" %>
    <html>
      <body>
        (Before)
        <do:repeater>Surprise</do:repeater>
        (After)
      </body>
    </html>
    
    1. (Before)(After)
    2. (Before)Surprise(After)
    3. (Before)SurpriseSurprise(After)
    4. An exception occurs at runtime.

  48. Assume that everything is declared properly and the TLD specifies <body-content>scriptless</body-content>. What is the output of the JSP? (1 correct answer)
    public class Repeater extends SimpleTagSupport {
      public void doTag() throws JspException, IOException {
        getJspBody().invoke(null);
      }
    }
    
    <%@ taglib uri="/webapp/mytaglib" prefix="do" %>
    <html>
      <body>
        (Before)
        <do:repeater>${here}</do:repeater>
        (After)
      </body>
    </html>
    
    1. (Before)(After)
    2. (Before)here(After)
    3. (Before)${here}(After)
    4. An exception occurs at runtime.

  49. Assume that everything is declared properly and the TLD specifies <body-content>scriptless</body-content>. What is the output of the JSP? (1 correct answer)
    public class Repeater extends SimpleTagSupport {
      public void doTag() throws JspException, IOException {
        getJspBody().invoke(null);
      }
    }
    
    <%@ taglib uri="/webapp/mytaglib" prefix="do" %>
    <% request.setAttribute("here", "Surprise"); %>
    <html>
      <body>
        (Before)
        <do:repeater>${here}</do:repeater>
        (After)
      </body>
    </html>
    
    1. (Before)(After)
    2. (Before)Surprise(After)
    3. An exception occurs at runtime.

  50. Assume that everything is declared properly and the TLD specifies <body-content>scriptless</body-content>. What is the output of the JSP? (1 correct answer)
    public class Repeater extends SimpleTagSupport {
      public void doTag() throws JspException, IOException {
        getJspContext().setAttribute("here", "Surprise");
        getJspBody().invoke(null);
      }
    }
    
    <%@ taglib uri="/webapp/mytaglib" prefix="do" %>
    <html>
      <body>
        (Before)
        <do:repeater>${here}</do:repeater>
        (After)
      </body>
    </html>
    
    1. (Before)(After)
    2. (Before)Surprise(After)
    3. An exception occurs at runtime.

  51. Consider this simple tag handler
    public class Repeater extends SimpleTagSupport {
      private String surprise;
      public void doTag() throws JspException, IOException {
        getJspContext().getOut().print(surprise);
        getJspContext().getOut().print(" is a big surprise!");
      }
    }
    

    that is properly declared in a TLD with the following attribute.

    <attribute>
      <name>surprise</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    

    What is the output of this JSP? (1 correct answer)

    <%@ taglib uri="/webapp/mytaglib" prefix="do" %>
    <html>
      <body>
        (Before)
        <do:repeater surprise="NetBeans 6.5" />
        (After)
      </body>
    </html>
    
    1. (Before)(After)
    2. (Before)null is a big surprise!(After)
    3. (Before)NetBeans 6.5 is a big surprise!(After)
    4. An exception occurs at runtime.

  52. Consider this simple tag handler
    public class Repeater extends SimpleTagSupport {
      private String surprise;
      public void doTag() throws JspException, IOException {
        getJspContext().getOut().print(surprise);
        getJspContext().getOut().print(" is a big surprise!");
        public void setSurprise(String surprise) {
        }
      }
    }
    

    that is properly declared in a TLD with the following attribute.

    <attribute>
      <name>surprise</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    

    What is the output of this JSP? (1 correct answer)

    <%@ taglib uri="/webapp/mytaglib" prefix="do" %>
    <html>
      <body>
        (Before)
        <do:repeater surprise="NetBeans 6.5" />
        (After)
      </body>
    </html>
    
    1. (Before)(After)
    2. (Before)null is a big surprise!(After)
    3. (Before)NetBeans 6.5 is a big surprise!(After)
    4. An exception occurs at runtime.

  53. Consider this simple tag handler
    public class Repeater extends SimpleTagSupport {
      private String surprise;
      public void doTag() throws JspException, IOException {
        getJspContext().getOut().print(surprise);
        getJspContext().getOut().print(" is a big surprise!");
        public void setSurprise(String surprise) {
          this.surprise = surprise;
        }
      }
    }
    

    that is properly declared in a TLD with the following attribute.

    <attribute>
      <name>surprise</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    

    What is the output of this JSP? (1 correct answer)

    <%@ taglib uri="/webapp/mytaglib" prefix="do" %>
    <html>
      <body>
        (Before)
        <do:repeater surprise="NetBeans 6.5" />
        (After)
      </body>
    </html>
    
    1. (Before)(After)
    2. (Before)null is a big surprise!(After)
    3. (Before)NetBeans 6.5 is a big surprise!(After)
    4. An exception occurs at runtime.

  54. TagSupport is a class that belongs to package javax.servlet.jsp.tagext. (1 correct answer)
    1. true
    2. false

  55. TagSupport IS-A IterationTag. (1 correct answer)
    1. true
    2. false

  56. TagSupport IS-A Tag. (1 correct answer)
    1. true
    2. false

  57. TagSupport IS-A JspTag. (1 correct answer)
    1. true
    2. false

  58. Consider class TagSupport. What is the signature of its method doStartTag? (1 correct answer)
    1. public int doStartTag() throws JspException
    2. public void doStartTag() throws JspException
    3. public int doStartTag() throws JspException, IOException
    4. public void doStartTag() throws JspException, IOException

  59. Consider class TagSupport. What is the signature of its method doEndTag? (1 correct answer)
    1. public int doEndTag() throws JspException
    2. public int doEndTag(String) throws JspException
    3. public int doEndTag(Writer) throws JspException, IOException

  60. Consider interface IterationTag. What is the signature of its method doAfterBody? (1 correct answer)
    1. int doAfterBody() throws JspException
    2. void doAfterBody() throws JspException

  61. The following are all fields of interface Tag. (1 correct answer)
          ⇒ SKIP_BODY
          ⇒ SKIP_PAGE
          ⇒ EVAL_PAGE
          ⇒ EVAL_BODY_INCLUDE

    1. true
    2. false

  62. The following is a field of interface IterationTag. (1 correct answer)
          ⇒ EVAL_BODY_AGAIN

    1. true
    2. false

  63. Is this a valid classic tag handler? (1 correct answer)
    public class Classic extends TagSupport {
    }
    
    1. Yes.
    2. No.

  64. Is this a valid classic tag handler? (1 correct answer)
    public class Classic extends TagSupport {
        public Classic() {
        }
    }
    
    1. Yes.
    2. No.

  65. Is this a valid classic tag handler? (1 correct answer)
    public class Classic extends TagSupport {
        private Classic() {
        }
    }
    
    1. Yes.
    2. No.

  66. Is this a valid classic tag handler? (1 correct answer)
    public class Classic extends TagSupport {
        public Classic() {
        }
        public Classic(String construct) {
        }
    }
    
    1. Yes.
    2. No.

  67. Assume that everything is declared properly and the TLD specifies <body-content>scriptless</body-content>. What is the output of the JSP? (1 correct answer)
    public class Classic extends TagSupport {
      public int doStartTag() {
        return SKIP_BODY;
      }
    }
    
    <%@ taglib uri="correct" prefix="yes" %>
    <html>
      <body>
        (Before)
        <yes:classic>My Body</yes:classic>
        (After)
      </body>
    </html>
    
    1. (Before)(After)
    2. (Before)My Body(After)

  68. Assume that everything is declared properly and the TLD specifies <body-content>scriptless</body-content>. What is the output of the JSP? (1 correct answer)
    public class Classic extends TagSupport {
      public int doStartTag() {
        return SKIP_PAGE;
      }
    }
    
    <%@ taglib uri="correct" prefix="yes" %>
    <html>
      <body>
        (Before)
        <yes:classic>My Body</yes:classic>
        (After)
      </body>
    </html>
    
    1. (Before)(After)
    2. (Before)
    3. (Before)My Body(After)
    4. (Before)My Body

  69. Assume that everything is declared properly and the TLD specifies <body-content>scriptless</body-content>. What is the output of the JSP? (1 correct answer)
    public class Classic extends TagSupport {
      public int doStartTag() {
        return EVAL_BODY_INCLUDE;
      }
    }
    
    <%@ taglib uri="correct" prefix="yes" %>
    <html>
      <body>
        (Before)
        <yes:classic>My Body</yes:classic>
        (After)
      </body>
    </html>
    
    1. (Before)(After)
    2. (Before)
    3. (Before)My Body(After)
    4. (Before)My Body

  70. Assume that everything is declared properly and the TLD specifies <body-content>scriptless</body-content>. What is the output of the JSP? (1 correct answer)
    public class Classic extends TagSupport {
      public int doStartTag() {
        try {
          pageContext.getOut().print("Hello!!");
        } catch (Exception e) {
          e.printStackTrace();
        }
        return SKIP_BODY;
      }
    }
    
    <%@ taglib uri="correct" prefix="yes" %>
    <html>
      <body>
        (Before)
        <yes:classic>My Body</yes:classic>
        (After)
      </body>
    </html>
    
    1. (Before)(After)
    2. (Before)Hello!!(After)
    3. (Before)My Body(After)
    4. (Before)Hello!!My Body(After)

  71. Assume that everything is declared properly and the TLD specifies <body-content>scriptless</body-content>. What is the output of the JSP? (1 correct answer)
    public class Classic extends TagSupport {
      public int doStartTag() {
        try {
          pageContext.getOut().print("Hello!!");
        } catch (Exception e) {
          e.printStackTrace();
        }
        return EVAL_BODY_INCLUDE;
      }
    }
    
    <%@ taglib uri="correct" prefix="yes" %>
    <html>
      <body>
        (Before)
        <yes:classic>My Body</yes:classic>
        (After)
      </body>
    </html>
    
    1. (Before)(After)
    2. (Before)Hello!!(After)
    3. (Before)My Body(After)
    4. (Before)Hello!!My Body(After)

  72. This classic tag handler is properly declared in a TLD.
    public class Classic extends TagSupport {
      private List<String> movies = new ArrayList<String>();
      private int current = 0;
      public ClassicRepeater() {
        movies.add("Heat");
        movies.add("Pocahontas");
        movies.add("Lawrence of Arabia");
      }
      public int doStartTag() {
        setAttribute();
        return EVAL_BODY_INCLUDE;
      }
      public int doAfterBody() {
        while (current < movies.size()) {
          setAttribute();
          return EVAL_BODY_AGAIN;
        }
        return SKIP_BODY;
      }
      private void setAttribute() {
        pageContext.setAttribute("movie", movies.get(current));
        current++;
      }
    }
    

    Many testers complain that when they visit or repeatedly hit the refresh button on the following jsp, they sometimes get an IndexOutOfBoundsException. What’s wrong with the above implementation? (1 correct answer)

    <%@ taglib uri="correct" prefix="yes" %>
    <html>
      <body>
        <ol>
        <yes:classic><li>${movie}</li></yes:classic>
        </ol>
      </body>
    </html>
    
    1. We should override the doEndTag() method.
    2. The value returned by doStartTag() is not correct.
    3. The doAfterBody() should not have a while loop inside.
    4. The instance variables should be initialized in the doStartTag() method.
    5. The doStartTag() and doAfterBody() methods should throw a JspException.

© 2008-2009 Nikos Pougounias. A free contribution to the Java community. Please distribute it for free. http://nikojava.wordpress.com

Answers

  1. a
  2. b
  3. b
  4. b
  5. b
  6. a
  7. a
  8. a
  9. b
  10. b
  11. a
  12. a
  13. b
  14. c
  15. b
  16. c
  17. b
  18. c
  19. b
  20. b
  21. c
  22. a
  23. a
  24. a
  25. a
  26. b
  27. b, f, d, e, a, c
  28. a
  29. a
  30. b
  31. a
  32. b
  33. b
  34. b
  35. b
  36. b
  37. b
  38. a
  39. b
  40. b
  41. b
  42. a
  43. b
  44. b
  45. b
  46. c
  47. c
  48. a
  49. b
  50. b
  51. d
  52. b
  53. c
  54. a
  55. a
  56. a
  57. a
  58. a
  59. a
  60. a
  61. a
  62. a
  63. a
  64. a
  65. b
  66. a
  67. a
  68. c
  69. c
  70. b
  71. d
  72. d

Free SCWCD Mock exam for Servlets

12 March 2009 by Nikos

This practice exam focuses on Servlets. For those using Head First Servlets and JSP, 2nd Edition it’s a perfect companion for chapters 4-6.

  1. Which of the following are interfaces? (3 correct answers)
    1. Servlet
    2. HttpServlet
    3. ServletRequest
    4. HttpServletRequest

  2. Which of the following are abstract classes? (2 correct answers)
    1. Servlet
    2. HttpServlet
    3. GenericServlet
    4. HttpServletRequest

  3. Which of the following statements is true? (1 correct answer)
    1. HttpServlet extends GenericServlet that implements Servlet.
    2. HttpServlet extends GenericServlet that extends Servlet.
    3. HttpServlet implements GenericServlet that extends Servlet.

  4. Which of the following statements are true? (2 correct answers)
    1. HttpServlet IS-A GenericServlet.
    2. HttpServlet IS-A Servlet.
    3. HttpServlet IS-A ServletRequest.

  5. Here are some actions taken by the Container when a client request arrives. Place them in the correct order starting from what happens first.
    1. Calls the void service(HttpServletRequest, HttpServletResponse) method of the servlet.
    2. Creates a pair of request and response objects.
    3. Finds the correct servlet based on the URL.
    4. Creates a new thread or allocates an existing thread to the client’s request.

  6. How can a servlet access it’s associated ServletConfig object? (1 correct answer)
    1. getServletConfig();
    2. request.getServletConfig();
    3. response.getServletConfig();
    4. getServletContext().getServletConfig();
    5. request.getSession().getServletConfig();

  7. How can a servlet access the application’s ServletContext object? (3 correct answers)
    1. getServletContext();
    2. request.getServletContext();
    3. response.getServletContext();
    4. getServletConfig().getServletContext();
    5. request.getSession().getServletContext();

  8. How is a request dispatched to hello.jsp from a doGet() method? (1 correct answer)
    1. request.getRequestDispatcher().forward(”hello.jsp”);
    2. request.getRequestDispatcher().dispatch(”hello.jsp”);
    3. request.getRequestDispatcher(”hello.jsp”).forward(request, response);
    4. request.getRequestDispatcher(”hello.jsp”).dispatch(request, response);

  9. How is a request redirected to hello.jsp from a doGet() method? (1 correct answer)
    1. request.redirect(”hello.jsp”);
    2. response.redirect(”hello.jsp”);
    3. request.sendRedirect(”hello.jsp”);
    4. response.sendRedirect(”hello.jsp”);

  10. Dispatching a request occurs on the server-side and redirection on the client-side. (1 correct answer)
    1. true
    2. false

  11. Both context init parameters and servlet init parameters are declared in the web.xml. (1 correct answer)
    1. true
    2. false

  12. The value of a servlet init parameter can be changed programmatically, but the value of a context init parameter cannot. (1 correct answer)
    1. true
    2. false

  13. A context init parameter cannot have the same name with a servlet init parameter. (1 correct answer)
    1. true
    2. false

  14. A servlet init parameter cannot have the same name with the servlet it refers to. (1 correct answer)
    1. true
    2. false

  15. Where is a servlet init parameter stored after the servlet is initialized and available for use? (1 correct answer)
    1. In the ServletConfig object of the servlet.
    2. In the ServletContext object of the web application.

  16. Where is a context init parameter stored after the servlet is initialized and available for use? (1 correct answer)
    1. In the ServletConfig object of the servlet.
    2. In the ServletContext object of the web application.

  17. Assume the servlet HelloServlet that belongs to package com. The file HelloServlet.class is placed in the directory WEB-INF/classes/com. Is this a correct declaration of an init parameter for this servlet? (1 correct answer)
    <servlet>
        <servlet-name>Hello Servlet</servlet-name>
        <servlet-class>classes.com.HelloServlet</servlet-class>
        <init-param>
            <param-name>this</param-name>
            <param-value>Hello!</param-value>
        </init-param>
    </servlet>
    
    1. Yes.
    2. No, because servlet-name contains a space.
    3. No, because servlet-class has a wrong value.
    4. No, because param-name is a reserved Java keyword.
    5. No, because param-value contains an explanation mark (!).
    6. No, because init-param should be inside a servlet-mapping element.

  18. What happens when we compile and deploy this servlet? (1 correct answer)
    public class Test extends HttpServlet {
    
    }
    
    1. Compilation fails because there is no init() method defined.
    2. An exception is thrown at runtime because service() has no method to call!
    3. Deployment succeeds but we get a message “GET is not supported by this URL” if we access it.

  19. What happens when we compile and deploy this servlet? (1 correct answer)
    class Test extends HttpServlet {
    
    }
    
    1. Compilation fails because there is no init() method defined.
    2. An exception is thrown at runtime because the servlet has no modifier.
    3. An exception is thrown at runtime because service() has no method to call!
    4. Deployment succeeds but we get a message “GET is not supported by this URL” if we try to access its URL.

  20. What happens when this servlet is compiled, deployed and called? (1 correct answer)
    public class Test extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
            // TODO
        }
    }
    
    1. Compilation fails because doGet() is empty.
    2. An exception is thrown at runtime because doGet() is empty.
    3. Deployment succeeds but nothing is displayed to the user’s browser.

  21. What happens when this servlet is compiled, deployed and called? (1 correct answer)
    public class Test extends HttpServlet {
    public String doGet(HttpServletRequest request,
                        HttpServletResponse response)
                        throws ServletException, IOException {
            // TODO
        return null;
    }
    }
    
    1. Compilation fails because doGet() must be void.
    2. Deployment succeeds but nothing is displayed to the user’s browser.
    3. A NullPointerException is thrown at runtime because null is returned.
    4. A ServletException is thrown at runtime because service() cannot find the proper doGet() method.

  22. What happens when this servlet is compiled and deployed? (1 correct answer)
    public class Test extends HttpServlet {
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
                         throws IOException {
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<p>Hello!!</p>");
        out.println("</body></html>");
        out.close();
    }
    }
    
    1. Compilation fails because doGet() is protected.
    2. Compilation fails because doGet() does not declare a ServletException.
    3. Deployment succeeds and clients are served just fine.

  23. What happens when this servlet is deployed and called? (1 correct answer)
    public class Test extends HttpServlet {
    public void doGet(final HttpServletRequest request,
                      final HttpServletResponse response)
                      throws IOException, ServletException {
        response.setContentType("text/html; charset=UTF-8");
        final PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<p>Hello!!</p>");
        out.println("</body></html>");
        out.close();
    }
    }
    
    1. An exception is thrown at runtime because the Container cannot modify the request and response objects.
    2. An exception is thrown at runtime when the out object is closed.
    3. Deployment succeeds and clients are served just fine.

  24. What happens when this servlet is deployed and called? (1 correct answer)
    public class Test extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        //out.println("<html><body>");
        out.println("<p>Hello!!</p>");
        //out.println("</body></html>");
        out.close();
    }
    }
    
    1. A ServletException is thrown at runtime because the <html> and <body> tags are missing.
    2. Deployment succeeds and Hello!! is presented on the browser.
    3. The server responds with a HTTP status code 404: “Not Found”.

  25. What happens when this servlet is compiled, deployed and called? (1 correct answer)
    public class Test extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        //response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<html><body>Hello!!</body></html>");
        out.close();
    }
    }
    
    1. Deployment succeeds and Hello!! is presented on the browser.
    2. Compilation fails because the content type should be specified before any output is written.
    3. An exception is thrown at runtime because the response has not an explicitly set content type.

  26. What happens when this servlet is compiled, deployed and called? (1 correct answer)
    public class Test extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<html><body>Hello!!</body></html>");
        //out.close();
    }
    }
    
    1. An exception is thrown at runtime because out is not closed.
    2. Deployment succeeds and Hello!! is presented on the browser.
    3. Deployment succeeds but no output is presented on the browser.

  27. Does this servlet compile successfully? (1 correct answer)
    public class Test extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws Exception { // line #1
        response.setContentType("text/html; charset=UTF-8");
        response.getWriter().println("Hello!!").close(); // line #2
    }
    }
    
    1. Compilation succeeds.
    2. There is a compilation error at line #1.
    3. There is a compilation error at line #2.
    4. Both lines #1 and #2 contain a compilation error.

  28. Does this servlet compile successfully? (1 correct answer)
    public class Test extends HttpServlet {
    public void init(ServletConfig config) {
        super.init(config);
    }
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<html><body>Hello!</body></html>");
        out.close();
    }
    }
    
    1. Compilation succeeds.
    2. Compilation fails because there is no init(ServletConfig) in GenericServlet.
    3. Compilation fails because init(ServletConfig) of GenericServlet throws ServletException.
    4. Compilation fails because init(ServletConfig) of GenericServlet throws IOException and ServletException.

  29. Does this servlet compile successfully? (1 correct answer)
    public class Test extends HttpServlet {
    public void doGet(HttpServletRequest req,
        HttpServletResponse response) throws IOException {
        req.getRequestDispatcher("index.html").forward(req, response);
    }
    }
    
    1. Compilation succeeds.
    2. Compilation fails because index.html is not a jsp.
    3. Compilation fails because doGet should declare ServletException as well.
    4. Compilation fails because the request object’s reference must be request and not req.

  30. Does this servlet compile successfully? (1 correct answer)
    public class Test extends HttpServlet {
    public void doGet(HttpServletRequest req,
        HttpServletResponse response) throws IOException {
        response.sendRedirect("index.html");
    }
    }
    
    1. Compilation succeeds.
    2. Compilation fails because index.html is not a jsp.
    3. Compilation fails because doGet should declare ServletException as well.
    4. Compilation fails because the request object’s reference must be request and not req.

  31. Which of the following statements shoud be inserted for a successful compilation? (1 correct answer)
    public class Test extends HttpServlet {
        protected void doGet(HttpServletRequest request,
        HttpServletResponse response) {
            // insert statement
        }
    }
    
    1. request.getRequestDispatcher(”hello.jsp”).forward(request, response);
    2. request.getRequestDispatcher(”hello.jsp”).forward(response, request);
    3. response.getRequestDispatcher(”hello.jsp”).forward(request, response);
    4. response.getRequestDispatcher(”hello.jsp”).forward(response, request);
    5. None of the above.

  32. What happens when the servlet with the following method is deployed and called? (1 correct answer)
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        response.getWriter().print('a');
        request.getRequestDispatcher("hello.jsp").forward(request, response); // line #1
        response.getWriter().print('a'); // line #2
        response.getWriter().close(); // line #3
    }
    
    1. An IllegalStateException is thrown at runtime because response.getWriter() is called more than once.
    2. An IllegalStateException is thrown at runtime at line #1 because the request is dispatched after writing data.
    3. An IllegalStateException is thrown at runtime at line #2 because data is written after the request has been dispatched.
    4. An IllegalStateException is thrown at runtime at line #3 because the writer is closed after the request has been dispatched.
    5. The browser displays the content of hello.jsp without any exception at runtime.
    6. The browser displays aa without any exception at runtime.
    7. The browser displays a without any exception at runtime.

  33. What happens when the servlet with the following method is deployed and called? (1 correct answer)
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        getServletContext().getRequestDispatcher("hello.jsp").forward(request, response);
    }
    
    1. An exception is thrown at runtime because the provided URL does not start with “/”.
    2. Compilation fails because the provided URL does not start with “/”.
    3. The browser displays the content of hello.jsp.
    4. The browser displays an empty page.

  34. What happens when the servlet with the following method is deployed and called? (1 correct answer)
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        getServletContext().getRequestDispatcher("/hello.jsp");
    }
    
    1. An exception is thrown at runtime because the provided URL is absolute.
    2. Compilation fails because the provided URL is absolute.
    3. The browser displays the content of hello.jsp.
    4. The browser displays an empty page.

  35. A servlet init parameter with name “ice” and value “tea” is properly declared. What is the result of this code? (1 correct answer)
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        response.getWriter().println(getServletConfig().getInitParameter("tea"));
    }
    
    1. A NullPointerException occurs at runtime.
    2. The browser displays an empty page.
    3. The browser displays “null”.
    4. The browser displays “ice”.
    5. The browser displays “tea”.
    6. Compilation fails.

  36. A servlet init parameter with name “ice” and value “tea” is properly declared. What is the result of this code? (1 correct answer)
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        response.getWriter().println(getInitParameter("ice"));
    }
    
    1. A NullPointerException occurs at runtime.
    2. The browser displays an empty page.
    3. The browser displays “null”.
    4. The browser displays “ice”.
    5. The browser displays “tea”.
    6. Compilation fails.

  37. A context init parameter with name “ice” and value “tea” is properly declared. What is the result of this code? (1 correct answer)
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        response.getWriter().println(getServletContext().getInitParameter("tea"));
    }
    
    1. A NullPointerException occurs at runtime.
    2. The browser displays an empty page.
    3. The browser displays “null”.
    4. The browser displays “ice”.
    5. The browser displays “tea”.
    6. Compilation fails.

  38. A context init parameter with name “ice” and value “tea” is properly declared. What is the result of this code? (1 correct answer)
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        response.getWriter().println(getContextParameter("ice"));
    }
    
    1. A NullPointerException occurs at runtime.
    2. The browser displays an empty page.
    3. The browser displays “null”.
    4. The browser displays “ice”.
    5. The browser displays “tea”.
    6. Compilation fails.

  39. The Container creates a single instance for every servlet. The client requests are served with various threads. (1 correct answer)
    1. true
    2. false

  40. Assume that the container is running on port 9999 of localhost, our application is called test1 and that the following servlet is declared with url pattern /processor. What is the output? (1 correct answer)
    public class Processor extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        response.getWriter().println(request.getContextPath());
    }
    }
    
    1. http://localhost:9999/test1/processor
    2. /test1/processor
    3. /processor
    4. /test1

  41. Assume that the container is running on port 9999 of localhost, our application is called test1 and that the following servlet is declared with url pattern /processor. What is the output? (1 correct answer)
    public class Processor extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        response.getWriter().println(request.getServletPath());
    }
    }
    
    1. http://localhost:9999/test1/processor
    2. /test1/processor
    3. /processor
    4. /test1

  42. Assume that the container is running on port 9999 of localhost, our application is called test1 and that the following servlet is declared with url pattern /processor. What is the output? (1 correct answer)
    public class Processor extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        response.getWriter().println(request.getRequestURL());
    }
    }
    
    1. http://localhost:9999/test1/processor
    2. /test1/processor
    3. /processor
    4. /test1

  43. Assume that the container is running on port 9999 of localhost, our application is called test1 and that the following servlet is declared with url pattern /processor. What is the output? (1 correct answer)
    public class Processor extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        response.getWriter().println(request.getRequestURI());
    }
    }
    
    1. http://localhost:9999/test1/processor
    2. /test1/processor
    3. /processor
    4. /test1

  44. Consider this form.
    <form method="post" action="processor">
       <input type="submit" value="OK"/>
    </form>
    

    And this servlet.

    public class Processor extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        response.getWriter().println(request.getMethod());
    }
    }
    

    What is the output when the form is submitted? (1 correct answer)

    1. GET
    2. POST
    3. A HTTP 405 message informs that GET is not supported.
    4. A HTTP 405 message informs that POST is not supported.

  45. Consider this form.
    <form method="post" action="processor">
       <input type="submit" value="OK"/>
    </form>
    

    And this servlet.

    public class Processor extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        response.getWriter().println(request.getMethod());
    }
    public void doPost(HttpServletResponse response,
                      HttpServletRequest request)
                      throws ServletException, IOException {
        doGet(request, response);
    }
    }
    

    What is the output when the form is submitted? (1 correct answer)

    1. GET
    2. POST
    3. A HTTP 405 message informs that GET is not supported.
    4. A HTTP 405 message informs that POST is not supported.

  46. Consider this form.
    <form name="form" method="post" action="processor">
       <input name="submit" type="submit" value="OK"/>
    </form>
    

    And this servlet.

    public class Processor extends HttpServlet {
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
                       throws ServletException, IOException {
        response.getWriter().format("%s %s",
        request.getParameter("form"),
        request.getParameter("name"));
    }
    }
    

    What is the output when the form is submitted? (1 correct answer)

    1. form OK
    2. null OK
    3. form null
    4. null null

  47. Consider this form.
    <form name="form" method="post" action="processor">
       <input name="submit" type="submit" value="OK"/>
    </form>
    

    And this servlet.

    public class Processor extends HttpServlet {
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
                       throws ServletException, IOException {
        response.getWriter().format("%s %s",
        request.getParameter("form"),
        request.getParameter("submit"));
    }
    }
    

    What is the output when the form is submitted? (1 correct answer)

    1. form OK
    2. null OK
    3. form null
    4. null null

  48. Consider this form.
    <form name="form" method="post" action="processor">
       <input type="text" name="name" value="OK"/>
       <input name="submit" type="submit" value="OK"/>
    </form>
    

    And this servlet.

    public class Processor extends HttpServlet {
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
                       throws ServletException, IOException {
        response.getWriter().format("%s %s",
        request.getParameter("name"),
        request.getParameter("OK"));
    }
    }
    

    What is the output when the form is submitted? (1 correct answer)

    1. OK OK
    2. null OK
    3. OK null
    4. null null

  49. Consider doGet() of a valid servlet with url pattern /source,
    request.getRequestDispatcher("target").forward(request, response);
    

    and doGet() of another valid servlet with url pattern /target.

    response.getWriter().println(request.getRequestURL());
    

    What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)

    1. http://localhost:9999/exam/source
    2. http://localhost:9999/exam/target
    3. null

  50. Consider doGet() of a valid servlet with url pattern /source,
    request.getRequestDispatcher("target").forward(request, response);
    

    and doGet() of another valid servlet with url pattern /target.

    response.getWriter().println(request.getQueryString());
    

    What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)

    1. user=nikos&pass=12345
    2. null

  51. Consider doGet() of a valid servlet with url pattern /source,
    request.getRequestDispatcher("target").forward(request, response);
    

    and doGet() of another valid servlet with url pattern /target.

    response.getWriter().println(request.getAttribute("javax.servlet.forward.query_string"));
    

    What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)

    1. user=nikos&pass=12345
    2. null

  52. Consider doGet() of a valid servlet with url pattern /source,
    request.getRequestDispatcher("target").forward(request, response);
    

    and doGet() of another valid servlet with url pattern /target.

    response.getWriter().println(request.getAttribute("javax.servlet.forward.servlet_path"));
    

    What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)

    1. /source
    2. /target
    3. null

  53. Consider doGet() of a valid servlet with url pattern /source,
    request.getRequestDispatcher("target").forward(request, response);
    

    and doGet() of another valid servlet with url pattern /target.

    response.getWriter().println(request.getServletPath());
    

    What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)

    1. /source
    2. /target
    3. null

  54. Consider doGet() of a valid servlet with url pattern /source,
    request.getRequestDispatcher("target").forward(request, response);
    

    and doGet() of another valid servlet with url pattern /target.

    response.getWriter().println(request.getParameter("pass"));
    

    What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)

    1. 12345
    2. null

  55. Consider doGet() of a valid servlet with url pattern /source,
    response.sendRedirect("target");
    

    and doGet() of another valid servlet with url pattern /target.

    response.getWriter().println(request.getParameter("pass"));
    

    What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)

    1. 12345
    2. null

  56. Consider doGet() of a valid servlet with url pattern /source,
    response.sendRedirect("target");
    

    and doGet() of another valid servlet with url pattern /target.

    response.getWriter().println(request.getServletPath());
    

    What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)

    1. /source
    2. /target
    3. null

  57. Consider doGet() of a valid servlet with url pattern /source,
    response.sendRedirect("target");
    

    and doGet() of another valid servlet with url pattern /target.

    response.getWriter().println(request.getAttribute("javax.servlet.forward.query_string"));
    

    What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)

    1. user=nikos&pass=12345
    2. null

  58. Consider doGet() of a valid servlet with url pattern /source,
    response.sendRedirect("target");
    

    and doGet() of another valid servlet with url pattern /target.

    response.getWriter().println(request.getQueryString());
    

    What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)

    1. user=nikos&pass=12345
    2. null

  59. There is a top-level folder help in the war file with index.html inside. What is the output of this code? (1 correct answer)
    request.getRequestDispatcher("help/index.html").forward(request, response);
    
    1. The content of index.html
    2. HTTP Status 404

  60. There is a top-level folder help in the war file with index.html inside. What is the output of this code? (1 correct answer)
    request.getRequestDispatcher("/help/index.html").forward(request, response);
    
    1. The content of index.html
    2. HTTP Status 404

  61. What is the output of this code? (1 correct answer)
    request.getRequestDispatcher("http://fcom.gr").forward(request, response);
    
    1. The content of fcom.gr
    2. HTTP Status 404

  62. There is a top-level folder help in the war file with index.html inside. What is the output of this code? (1 correct answer)
    response.sendRedirect("help/index.html");
    
    1. The content of index.html
    2. HTTP Status 404

  63. There is a top-level folder help in the war file with index.html inside. What is the output of this code? (1 correct answer)
    response.sendRedirect("/help/index.html");
    
    1. The content of index.html
    2. HTTP Status 404

  64. What is the output of this code? (1 correct answer)
    response.sendRedirect("http://fcom.gr");
    
    1. The content of fcom.gr
    2. HTTP Status 404

  65. What happens when this servlet is deployed and a user hits repeatedly the refresh button of his browser? (1 correct answer)
    public class Test extends HttpServlet {
    private Integer number = 0;
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        number = number + 1;
        response.getWriter().println(number);
    }
    }
    
    1. The same value is always displayed.
    2. The displayed value increases with every refresh.

  66. What happens when this servlet is deployed and a user hits repeatedly the refresh button of his browser? (1 correct answer)
    public class Test extends HttpServlet {
    private Integer number = new Random().nextInt();
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        response.getWriter().println(number);
    }
    }
    
    1. The same value is always displayed.
    2. The displayed value increases with every refresh.

  67. The following types are ALL interfaces and are used as listeners in a web application. (1 correct answer)
          ⇒ ServletContextListener
          ⇒ ServletContextAttributeListener
          ⇒ ServletRequestListener
          ⇒ ServletRequestAttributeListener
          ⇒ HttpSessionListener
          ⇒ HttpSessionBindingListener
          ⇒ HttpSessionAttributeListener
          ⇒ HttpSessionActivationListener

    1. true
    2. false

  68. Consider the interface ServletContextListener. What is the signature of the method that is invoked when the servlet context is about to be shut down? (1 correct answer)
    1. void contextDeleted(ServletContextEvent)
    2. void contextDestroyed(ServletContextEvent)
    3. void servletContextDeleted(ServletContextEvent)
    4. void servletContextDestroyed(ServletContextEvent)

  69. There is a properly declared HttpSessionAttributeListener. How many times its attributeRemoved method is invoked when the following servlet is accessed once? (1 correct answer)
    public class Test extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        request.getSession().setAttribute("planet", "earth");
        request.getSession().removeAttribute("planet");
        request.getSession().removeAttribute("planet");
    }
    }
    
    1. 0
    2. 1
    3. 2

  70. There is a properly declared HttpSessionAttributeListener. How many times its attributeRemoved method is invoked when the following servlet is accessed once? (1 correct answer)
    public class Test extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        request.getSession().setAttribute("planet", "earth");
        request.getSession().invalidate();
        request.getSession().removeAttribute("planet");
        request.getSession().setAttribute("planet", "earth");
    }
    }
    
    1. 0
    2. 1
    3. 2

  71. There is a properly declared HttpSessionAttributeListener. How many times its attributeReplaced method is invoked when the following servlet is accessed once? (1 correct answer)
    public class Test extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        request.getSession().setAttribute("planet", "earth");
        request.getSession().removeAttribute("earth");
        request.getSession().setAttribute("planet", "earth");
    }
    }
    
    1. 0
    2. 1
    3. 2

  72. There is a properly declared HttpSessionAttributeListener. How many times its attributeReplaced method is invoked when the following servlet is accessed once? (1 correct answer)
    public class Test extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
        request.getSession().setAttribute("planet", "earth");
        request.getSession().removeAttribute("planet");
        request.getSession().setAttribute("planet", "venus");
        request.setAttribute("planet", "earth");
        request.setAttribute("planet", "venus");
    }
    }
    
    1. 0
    2. 1
    3. 2

© 2008-2009 Nikos Pougounias. A free contribution to the Java community. Please distribute it for free. http://nikojava.wordpress.com

Answers

  1. a, c, d
  2. b, c
  3. a
  4. a, b
  5. b, c, d, a
  6. a
  7. a, d, e
  8. c
  9. d
  10. a
  11. a
  12. b
  13. b
  14. b
  15. a
  16. b
  17. c
  18. c
  19. b
  20. c
  21. a
  22. c
  23. c
  24. b
  25. a
  26. b
  27. d
  28. c
  29. c
  30. a
  31. e
  32. e
  33. a
  34. d
  35. c
  36. e
  37. c
  38. f
  39. a
  40. d
  41. c
  42. a
  43. b
  44. d
  45. d
  46. d
  47. b
  48. c
  49. b
  50. a
  51. a
  52. a
  53. b
  54. a
  55. b
  56. b
  57. b
  58. b
  59. a
  60. a
  61. b
  62. a
  63. b
  64. a
  65. b
  66. a
  67. a
  68. b
  69. b
  70. b
  71. b
  72. a

Java Day in Athens (14 Feb 2009)

8 February 2009 by Nikos

Java Hellenic User Group announces an interesting free Java event.

Date 14 February 2009
Register Athen’s JUG Event
Sponsor Sun Microsystems Hellas
Place Divani Caravel hotel of Athens

Here’s the schedule.

Topic Presenter
Kickstart JPA Alexander Snaps
Using Maven 2 Andreas Andreou
RIA Design Patterns Ioannis Doxaras
Migrating J2EE applications Alexis Moussine
Open Solaris for developers Roman Strobl