Free SCWCD Mock Exam for Servlets


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 dispatchedto 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 redirectedto 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 {
          response.setContentType("text/html; charset=UTF-8");
          response.getWriter().println("Hello!!").close();
      }
      }
      1. Compilation succeeds.
      2. There is a compilation error at line 4.
      3. There is a compilation error at line 6.
      4. Both lines 4 and 6 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);
          response.getWriter().print('a');
          response.getWriter().close();
      }
      1. An IllegalStateException is thrown at runtime because response.getWriter() is called more than once.
      2. An IllegalStateException is thrown at runtime at line 5 because the request is dispatched after writing data.
      3. An IllegalStateException is thrown at runtime at line 6 because data is written after the request has been dispatched.
      4. An IllegalStateException is thrown at runtime at line 7 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.htmlinside. 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.htmlinside. 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.htmlinside. 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.htmlinside. 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 attributeRemovedmethod 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 attributeRemovedmethod 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 attributeReplacedmethod 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 attributeReplacedmethod 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-2011 Nikos Pougounias. A free contribution to the Java community. Please distribute it for free. https://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

35 Responses to Free SCWCD Mock Exam for Servlets

  1. […] Hi, I’ve compiled a free mock specific for Servlets >here. […]

  2. Milton says:

    thanks for the mock.

    My comment is, the answer no have literal caracter correspondency

  3. Milton says:

    Hi, again, i want to ask you about the question 45, i think that the right answer is B, can you explain why is d? Thanks for the mock again.

  4. Nikos says:

    Hi Milton! The answer would be GET if the servlet was directly accessed. But here, it is accessed through a JSP…

  5. Milton says:

    what do you mean directly accesed?

    I test this question with a html file, and the POST method is called normally.

    Tonight I will test accesing through a JSP, but i think that the answer will be the same!

  6. Nikos says:

    Have you used the exact code of the question?🙂

  7. Milton says:

    I get it,

    the method parameters are inverted (HttpSevletResponse, HttpSevletRequest).

    I will be most focus on that stuff

    Thanks again.

  8. Francis says:

    Hi Niko,

    Thanks for the mock.🙂
    A question, why does #16 have B as answer?
    I would say A, because you can access ServletContext from the ServletConfig, so in fact ServletConfig has a ServletContext object.

  9. Nikos says:

    Hi Francis,

    Of course you can access the ServletContext from any ServletConfig (see question 7). However it’s the ServletContext that holds context init parameters!

  10. Amit says:

    Can someone explain me why in Q 71, the answer will be (B). I dont understand now since the session is invalidated, how can i remove any attributes from it. The session doesnt exists ?? So how can an attribute be still there ?

  11. Daisy says:

    Amit,
    you mean qn 70?
    Yes. When session is invalidated then the attribute removed is called.
    Here attribute removed is not called when the statement: request.getSession().removeAttribute(“planet”);
    is executed but when the session is invalidated.

  12. priya says:

    Hello…….

    I get confused in qu.29 and qu.30. In 29 code gets fails as it require that servlet should throw ServletException then is that not needed for qu.30..
    please help me…

  13. Ankit Garg says:

    I think the answer of question 50 is wrong.

    In question 62, the question would be more exact if there was a URL representing the requested servlet which performs the redirect as the path in sendRedirect is relative.

    Similarly in question 63, I would recommend that you wrote that the application is deployed on a context path (whatever it may be) as the answer would be wrong if the application was deployed on root or default context path…

  14. Ankit Garg says:

    Answer of question 45 also seems wrong to me…

  15. Ankit Garg says:

    priya, the RequestDispatcher.forward method throws ServletException but the HttpServletResponse.sendRedirect doesn’t throw ServletException…

  16. Marcelo says:

    I did not understand q29 and q30. Don’t they both require ServletException because the overriden methods declares?

  17. […] Free SCWCD Mock Exam for Servlets […]

  18. sreena says:

    Hi All,

    Can anybody please explain me Q72… why attributeReplaced() method is not called?

    Thanks.

  19. padma says:

    Hi,

    I also couldnt understand why Q72 has that answer .. can anyone please explain?

    Thanks in advance..

  20. kjb says:

    Hi,
    About Q72:
    The question is about HttpSessionAttributeListener, and only the first three lines manipulates session attributes. (The last two operates on request attributes so attributeAdded and attributeReplaced methods are called on the declared ServletRequestAttributeListener instead). In the first line a new attribute is added so attributeAdded is called, then this attribute is removed on the second line (resulting an attributeRemoved call), so the third line is again an attribute addition, not a replace. Therefore no attributeReplaced are called on the declared HttpSessionAttributeListener…

  21. komisarz says:

    Hi.

    I don’t understand why I can write something to the output stream after forwarding with requestdispatcher. Why there is no exception ? (Q. #32)

  22. padma says:

    Thanks kjb for the explanation…

    I have one more doubt which is in Q50. I think answer must be option B.

    However when I tested it, I got option A as the output. Can anyone please explain?
    Thanks

  23. Gagan says:

    For the Q# 7 How can a servlet access the application’s ServletContext object? (3 correct answers)
    a. getServletContext();
    b. request.getServletContext();
    c. response.getServletContext();
    d. getServletConfig().getServletContext();
    e. request.getSession().getServletContext();

    Answer given is a,d,e

    But i feel answer should be a,d only ….i have checked the APIs also …Can somebody comment on this ?

  24. kjb says:

    Hi,
    padma, about #50, I guess You think that only query parameters provided in the string used to get RequestDispatcher are forwarded to the included page (for example request.getRequestDispatcher(“target?user=nikos”)). That’s not the case though. Although query parameters provided in this way takes precedence over original request parameters with the same name (in the duration of the forward or include call) the non-overridden query string parameters remains intact and are passed to the included/forwarded page.

  25. kjb says:

    Hi,
    Gagan, about Q#7:
    … excerpt from javax.servlet.http.HttpSession API:

    ServletContext getServletContext()
    Returns the ServletContext to which this session belongs.

    … maybe You’ve checked API for wrong Session (javax.jms.Session or javax.mail.Session) or wrong version (this method is only available since Servlet 2.3)

  26. Ralph says:

    About Q36, for me there is no compilation error and it works just fine.

    Thanks

  27. Pedro Kowalski says:

    Q7 – why the b answer is incorrect? According to this:
    http://download.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getServletContext%28%29

    I can use request.getServletContext() to obtain ServletContext object.

  28. mcscores says:

    Is it just me or the code fragments in the page are not rendering properly ?:/ I have tried in Chrome as well as FF:/

  29. mcscores says:

    On inspecting the

     element from browser, I found that by not keeping the font-size larger, it solved the rendering problem. 
    I am not a CSS guy :( Any solution is welcomed :)
    
    element.style {
    font-size: larger;     //  This is the problem
    background-color: lightGrey;
    font-family: 'Courier New';
    }
  30. mcscores says:

    On inspecting the *pre* element from browser, I found that by not keeping the font-size larger, it solved the rendering problem.
    I am not a CSS guy😦 Any solution is welcomed🙂

    element.style {
    font-size: larger; // This is the problem
    background-color: lightGrey;
    font-family: ‘Courier New’;
    }

  31. SVB says:

    Thanks to NikoJava, for the fabulous compilation of Q&A.
    1. Q29 and Q30 seem to be the same
    2. Not able to see the code fragments completely
    (so, my point 1 may be invalid)

    Please correct the font problem, am preparing for SCWCD, badly
    need this mock. Please!

  32. PF says:

    I had the same problems with the font. I just used copy & paste and pasted the text to notepad. There, I got the whole text. Not the best solution but it worked for me.

  33. prabha says:

    in most of the questions the content is missed, i don’t know what is the use of this site?

  34. phani says:

    Q.62 answer is b

  35. umar says:

    question number 45 will give you the output post

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: