=============================================================================== MCQ QUIZ - TOPIC 1: SERVLET (STAGE 2) 100 Hours Java Backend Developer Pedagogy =============================================================================== Instructions: - Total 70 questions (10 questions for each of the 7 sessions). - Choose the best option (a, b, c or d) for each question. - Answers are given in the ANSWER SHEET at the end of this file. =============================================================================== =============================================================================== SESSION 1 - BASIC UNDERSTANDING OF SERVLETS =============================================================================== Q1. Servlet can be described as a technology used to create a web application. It is also: a) an interface that must be implemented (directly or indirectly) for creating any servlet b) a class that must be extended by every Java class c) a database driver d) a browser plug-in Q2. Servlets are a key part of which platform? a) Java SE b) Java Enterprise Edition (Java EE) c) Java ME d) Android SDK Q3. In CGI technology, for each request the web server: a) reuses a single thread b) starts a new process c) reuses the same servlet instance d) does nothing Q4. Servlets give better performance than CGI mainly because: a) the web container creates a thread for each request instead of a process b) servlets are written in C/C++ c) servlets do not need a web server d) threads cannot share memory Q5. The Servlet interface provides how many life cycle methods? a) 2 b) 4 c) 3 d) 5 Q6. Which of the following is NOT an interface in the javax.servlet package? a) ServletConfig b) FilterChain c) RequestDispatcher d) GenericServlet Q7. Which of the following is a CLASS (not an interface) of the javax.servlet package? a) ServletRequest b) ServletException c) ServletContext d) FilterConfig Q8. Which interface provides the forward() / include() capability to send a request to another resource? a) Servlet b) Filter c) RequestDispatcher d) ServletContext Q9. A servlet is: a) a web component that is deployed on the server to create a dynamic web page b) a static HTML page stored in the browser c) a database table d) an operating system service Q10. Which of the following is a disadvantage of CGI mentioned in the session? a) It shares a common memory area b) It uses threads for each request c) It is written in Java d) It uses platform dependent languages e.g. C, C++, perl =============================================================================== SESSION 2 - SERVLET ENVIRONMENT SETUP =============================================================================== Q1. Which of the following is needed to develop servlets? a) Only a web browser b) JDK, an IDE, the Servlet API JAR files (from a container such as Tomcat) and a servlet container c) Only MySQL d) Only a text editor Q2. The Java Servlet API classes and interfaces are bundled with: a) the Java EE platform; if you use a servlet container like Tomcat, the necessary servlet API JAR files come with its installation b) the web browser c) the MySQL installer d) the operating system Q3. A servlet class created for handling HTTP requests normally: a) extends JFrame and overrides paint() b) implements Runnable and defines run() c) extends HttpServlet and overrides the doGet() / doPost() methods d) extends Thread and overrides start() Q4. Which method of HttpServlet handles the HTTP GET request? a) doPost() b) doGet() c) doPut() d) doTrace() Q5. The HttpServlet class extends which class? a) GenericServlet b) Filter c) ServletContext d) HttpServlet does not extend any class Q6. In the GenericServlet class, which method is declared abstract? a) init(ServletConfig config) b) destroy() c) getServletInfo() d) service(ServletRequest, ServletResponse) Q7. Which method returns information about the servlet such as writer, copyright and version? a) getServletConfig() b) getServletInfo() c) getServletContext() d) getServletName() Q8. Before writing HTML content through the response, the servlet must call: a) res.setContentType("text/html") b) res.sendRedirect() c) res.invalidate() d) res.init() Q9. The method getInitParameter(String name) of GenericServlet returns: a) the value of a request parameter b) the value of a cookie c) the parameter value for the given parameter name defined in web.xml d) the name of the servlet Q10. In the example servlet "First extends GenericServlet", the output is written using: a) System.out.println() only b) PrintWriter obtained from res.getWriter() c) JspWriter obtained from pageContext d) FileOutputStream =============================================================================== SESSION 3 - LIFE CYCLE OF SERVLET =============================================================================== Q1. The init() method of a servlet: a) is called only once during the lifecycle b) is called on every request c) is called after destroy() d) is never called by the container Q2. The service() method of the servlet delegates the request to: a) init() and destroy() b) main() and run() c) the appropriate doXxx() method such as doGet(), doPost() d) the JSP engine only Q3. The destroy() method is invoked: a) for every client request b) only once, when the servlet is unloaded or the container shuts down c) before init() d) ten times during the lifecycle Q4. The three states of a servlet are: a) new, running, dead b) new, ready and end c) start, pause, stop d) open, close, flush Q5. Servlets handle multiple requests concurrently because each request is processed in a separate: a) process b) JVM c) server d) thread Q6. The mostly used approach to create a servlet is: a) inheriting the HttpServlet class b) implementing the Servlet interface directly c) inheriting the GenericServlet class d) writing a JavaBean Q7. After compiling, the servlet class file must be placed in: a) WEB-INF/lib b) WEB-INF/jsp c) the classes folder under WEB-INF d) the browser cache Q8. The deployment descriptor from which the web container gets the information about the servlet is: a) web.xml b) pom.xml c) build.xml d) index.html Q9. In web.xml, which element holds the pattern used at the client side to invoke the servlet? a) b) c) d) Q10. To compile a servlet, the servlet-api jar must be loaded by: a) setting the classpath (or pasting the jar into the server's lib folder) b) opening the browser c) running the MySQL service d) editing index.html =============================================================================== SESSION 4 - UNDERSTANDING JAVA BEANS =============================================================================== Q1. A JavaBean class should have: a) a main method b) a no-arg constructor c) a static field d) an entry in web.xml Q2. A JavaBean should: a) implement java.io.Serializable b) extend HttpServlet c) be abstract d) define a run() method Q3. The method setFirstName(String) of a bean is called: a) accessor b) constructor c) mutator d) filter Q4. The method getFirstName() of a bean is called: a) accessor b) mutator c) callback d) listener Q5. What is the default scope of the action tag? a) session b) request c) application d) page Q6. Which action tag locates or instantiates a bean object? a) jsp:forward b) jsp:useBean c) jsp:include d) jsp:param Q7. means: a) set all bean properties whose names match the incoming request parameters b) delete all properties of the bean c) print all properties of the bean d) create a new bean for each property Q8. Which action tag returns (prints) the value of a bean property? a) jsp:setProperty b) jsp:useBean c) jsp:getProperty d) jsp:plugin Q9. Which is a disadvantage of JavaBeans mentioned in the session? a) JavaBeans cannot be serialized b) JavaBeans are mutable, so it can't take advantages of immutable objects c) JavaBeans must be declared final d) JavaBeans cannot have private fields Q10. The scope that allows a bean to be used from any JSP page in the same session (whether it processes the same request or not) is: a) session b) request c) page d) application =============================================================================== SESSION 5 - WORKING WITH FILTERS =============================================================================== Q1. A filter is an object that is invoked at the: a) preprocessing and postprocessing of a request b) translation of a JSP only c) deployment of the web application only d) compilation of the servlet only Q2. The Filter API in the javax.servlet package contains which three interfaces? a) Servlet, ServletRequest, ServletResponse b) Filter, FilterChain, FilterConfig c) FilterConfig, ServletConfig, HttpSession d) RequestDispatcher, Filter, Listener Q3. The chain.doFilter(req, resp) call inside a filter: a) ends the response permanently b) logs the request to a server file c) passes control to the next filter or resource in the chain d) recompiles the servlet Q4. A servlet filter is pluggable, which means: a) it cannot be removed once deployed b) it is compiled into the servlet class c) changing the servlet code is required to disable it d) its entry is defined in web.xml, and removing the entry removes the filter without changing the servlet Q5. For mapping a filter, which element has the advantage that it can be applied on servlet, JSP or HTML? a) b) c) d) Q6. The FilterConfig object is created by: a) the web container b) the servlet class c) the JSP page d) the browser Q7. FilterConfig.getInitParameter(String parameterName) returns: a) the request parameter value b) the init parameter value for the specified name from web.xml c) the cookie value d) the header value Q8. In the authentication filter example, the request is passed to the next resource only if the password equals: a) root b) test c) admin d) password Q9. Which Filter method is invoked only once to initialize the filter? a) doFilter() b) destroy() c) service() d) init() Q10. To calculate the total response time, the filter records: a) System.currentTimeMillis() before and after chain.doFilter() b) the value of getInitParameter("time") c) the cookie creation time d) the JSP translation time =============================================================================== SESSION 6 - WORKING WITH COOKIES =============================================================================== Q1. A cookie in servlets is: a) a file stored in the web application folder b) a small piece of information that is persisted between the multiple client requests c) a database table row d) a JSP directive Q2. The Cookie class belongs to which package? a) javax.servlet.http b) javax.servlet c) java.net d) java.sql Q3. The commonly used constructor of the Cookie class is: a) Cookie(String value) b) Cookie(int maxAge) c) Cookie(String name, String value) d) Cookie() Q4. Which method adds a cookie to the response object? a) response.addCookie(cookie) b) request.addCookie(cookie) c) out.addCookie(cookie) d) session.addCookie(cookie) Q5. How are cookies read from an incoming request? a) response.getCookies() b) session.getCookies() c) request.getParameter() d) request.getCookies(), which returns an array of type Cookie[] Q6. The default value returned by getMaxAge() is: a) 0 b) -1 c) 100 d) 3600 Q7. To remove a cookie immediately, you should: a) setMaxAge(-1) b) setMaxAge(100) c) setMaxAge(0) d) call cookie.clear() Q8. A non-persistent cookie is removed: a) each time the user closes the browser b) only when the user logs out c) after 30 days d) only when the server restarts Q9. Which is a disadvantage of cookies listed in the session? a) cookies are the simplest technique of maintaining the state b) cookies are maintained at the client side c) cookies are processed on the server d) it will not work if cookie is disabled from the browser Q10. In the login example of the session, which username / password combination creates the cookies? a) admin / admin b) root / 1234 c) jai / 1234 d) test / test =============================================================================== SESSION 7 - MANAGING USER SESSION =============================================================================== Q1. HTTP is described as: a) a stateless protocol - all requests and responses are independent b) a stateful protocol that remembers every user automatically c) a database protocol d) an object serialization protocol Q2. Session management is: a) a technique to make HTTP faster b) a browser setting c) a mechanism used by the Web container to store session information for a particular user d) a method of compiling JSP pages Q3. Which is NOT one of the four session management techniques listed? a) Cookies b) Hidden form field c) URL Rewriting d) XML parsing Q4. Which of the following is one of the four session management techniques used by a servlet application? a) JDBC b) HttpSession c) ServletConfig d) JSTL Q5. When a user wants to exit from the application, the servlet should: a) destroy the object holding his session information (invalidate the session) b) copy the object into another session c) rename the object d) write it into web.xml Q6. The main purpose of session management is to: a) compile the JSP only once b) store cookies in the database c) keep track of the client's activity across multiple requests d) increase the speed of HTTP Q7. The basic concept behind a session is to save unique identification information about the user in an object that is: a) available only inside one JSP file b) available throughout the application until it is destroyed c) stored in the browser cache only d) written into web.xml Q8. Which protocol is stateless, which is why session management is needed? a) HTTP b) HttpSession c) Cookie d) Servlet Q9. With a session, the server can always manage: a) only the IP address of the client b) only the browser name c) only the requested URL d) which user is doing what, across all the requests the user makes Q10. Which technique appends a session identifier parameter with every request and response to keep track of the session? a) Cookies b) Hidden form field c) URL rewriting d) Session Tracking API =============================================================================== ANSWER SHEET =============================================================================== SESSION 1 - BASIC UNDERSTANDING OF SERVLETS ------------------------------------------- Q1 : a Q2 : b Q3 : b Q4 : a Q5 : c Q6 : d Q7 : b Q8 : c Q9 : a Q10 : d SESSION 2 - SERVLET ENVIRONMENT SETUP ------------------------------------------- Q1 : b Q2 : a Q3 : c Q4 : b Q5 : a Q6 : d Q7 : b Q8 : a Q9 : c Q10 : b SESSION 3 - LIFE CYCLE OF SERVLET ------------------------------------------- Q1 : a Q2 : c Q3 : b Q4 : b Q5 : d Q6 : a Q7 : c Q8 : a Q9 : b Q10 : a SESSION 4 - UNDERSTANDING JAVA BEANS ------------------------------------------- Q1 : b Q2 : a Q3 : c Q4 : a Q5 : d Q6 : b Q7 : a Q8 : c Q9 : b Q10 : a SESSION 5 - WORKING WITH FILTERS ------------------------------------------- Q1 : a Q2 : b Q3 : c Q4 : d Q5 : c Q6 : a Q7 : b Q8 : c Q9 : d Q10 : a SESSION 6 - WORKING WITH COOKIES ------------------------------------------- Q1 : b Q2 : a Q3 : c Q4 : a Q5 : d Q6 : b Q7 : c Q8 : a Q9 : d Q10 : c SESSION 7 - MANAGING USER SESSION ------------------------------------------- Q1 : a Q2 : c Q3 : d Q4 : b Q5 : a Q6 : c Q7 : b Q8 : a Q9 : d Q10 : c =============================================================================== END OF QUIZ - 70 QUESTIONS TOTAL ===============================================================================