=============================================================================== MCQ QUIZ - JSP AND SERVLET (Topic 3) STAGE 2 - 100 Hours Java Backend Developer Pedagogy =============================================================================== Instructions: - Total 60 questions (10 questions per session). - 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 - FORMS AND SESSION MANAGEMENT =============================================================================== Q1. In the employee registration example, the JSP form submits data to the servlet using the URL: a) request.getContextPath() + "/register" b) request.getPathInfo() + "/register" c) "/employeeregister.jsp" d) "/employeedetails.jsp" Q2. Which annotation maps the EmployeeServlet to the URL pattern "/register"? a) @RequestMapping("/register") b) @WebServlet("/register") c) @ServletMapping("/register") d) @UrlPattern("/register") Q3. Which method of HttpServletRequest is used inside doPost() to read a form field named "firstName"? a) request.getField("firstName") b) request.getParameter("firstName") c) request.getValue("firstName") d) request.getParam("firstName") Q4. The DAO method registerEmployee(Employee employee) returns a value of type: a) void b) boolean c) int d) String Q5. Which of the following statements about the Employee bean is TRUE? a) It must extend HttpSession b) It implements Serializable and holds private fields with getters/setters c) It must extend HttpServlet d) It must be an interface Q6. Which method of HttpServletResponse is used in EmployeeServlet after the employee is successfully registered? a) response.getWriter().println() b) response.sendRedirect("employeedetails.jsp") c) response.forward("employeedetails.jsp") d) response.setStatus(200) Q7. Which of the following is NOT a session tracking method discussed in the session? a) Cookies b) URL rewriting c) Hidden fields d) DNS lookup Q8. Which HttpSession method binds an object to the current session with a specified name? a) getAttribute() b) isNew() c) setAttribute() d) getId() Q9. A non-persistent cookie is: a) Valid for multiple sessions until the user logs out b) Valid only for a single session and removed when the browser is closed c) Stored permanently on the server hard disk d) Encrypted using SSL always Q10. What does HttpSession.isNew() return? a) false always b) the session id as a String c) true if the server does not yet know about this session (e.g. cookies are disabled) d) the time at which the session was created =============================================================================== SESSION 2 - DATABASE CONNECTIVITY =============================================================================== Q1. Which JSTL tag library directive is used to work with a database directly from a JSP page? a) <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> b) <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> c) <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> d) <%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%> Q2. Which tag is used to create (configure) the database connection used by the JSTL SQL tags? a) b) c) d) Q3. The example in the session uses which driver class? a) oracle.jdbc.driver.OracleDriver b) com.mysql.jdbc.Driver c) sun.jdbc.odbc.JdbcOdbcDriver d) org.postgresql.Driver Q4. Which tag executes a SELECT statement and stores the result in a variable? a) b) c) d) Q5. To iterate over the rows of a query result, which expression is used as the items attribute of ? a) ${result} b) ${result.rows} c) ${result.columns} d) ${resultset} Q6. Which JSTL tag is used to insert, update or delete records in the database examples? a) b) c) d) Q7. Which tag supplies a value for a "?" placeholder in an SQL statement (e.g. DELETE FROM Employees WHERE Id = ?)? a) b) c) d) Q8. The output of database values inside the JSP page is printed using: a) b) c) d) Q9. Before running the JSTL SQL examples, which of the following must be TRUE? a) The MySQL server must be running and the JDBC driver jar must be available b) The JSP page must be compiled with javac only c) The browser must have JavaScript enabled d) The application must use only servlets, no JSP Q10. Which of the following correctly describes the JDBC URL used in the session? a) jdbc:mysql://localhost/TEST connects to database TEST on the local MySQL server b) jdbc:mysql://localhost/TEST is a browser URL c) jdbc:mysql is a protocol of HTTP d) jdbc:mysql://localhost/TEST creates a new database automatically =============================================================================== SESSION 3 - CRUD OPERATIONS IN DATABASE =============================================================================== Q1. CRUD in "CRUD operations in database" stands for: a) Create, Read, Update, Delete b) Compile, Run, Upload, Deploy c) Copy, Replace, Use, Dump d) Connect, Request, Update, Display Q2. Which of the following is NOT one of the features of the simple CRUD application discussed? a) Create a user b) Retrieve a user c) Delete a user d) Compile a user Q3. In the project, the package that consists of all the database logic is: a) Model b) Web c) DAO (Data Access Object) d) View Q4. Which package/folder contains the JSP files used to design the front end? a) DAO b) Model c) Web (webapp) d) Bean Q5. Which package consists of Java classes for the back-end logic (model)? a) DAO b) Model c) Web d) Config Q6. Which MySQL statement creates the users table used in the example? a) CREATE USER usermanagement; b) CREATE TABLE users (id int(3) NOT NULL AUTO_INCREMENT, name varchar(120) NOT NULL, email varchar(220) NOT NULL, country varchar(120), PRIMARY KEY (id)); c) INSERT INTO users VALUES (1, 'a', 'b', 'c'); d) SELECT * FROM users; Q7. Which two JSP files are created for the front end of the CRUD project? a) index.jsp and login.jsp b) user-form.jsp and user-list.jsp c) register.jsp and detail.jsp d) list.jsp and edit.jsp Q8. Where are the project dependency jar files copied in the Dynamic Web Project? a) src/main/java b) WEB-INF/lib c) WebContent/css d) META-INF/classes Q9. Which IDE menu path is used to create the project? a) File > New > Dynamic Web Project b) File > New > Java Project c) Project > New > Maven Module d) Run > New > Server Only Q10. Before running the project, the developer must ensure that: a) The browser is closed b) The MySQL server is running on the desired port c) The JSP files are deleted d) The DAO package is removed =============================================================================== SESSION 4 - LISTING DATA ON WEB PAGE =============================================================================== Q1. The servlet in this session displays data from the database by writing HTML using: a) JSP implicit objects b) PrintWriter obtained from response.getWriter() c) System.out.println() d) A JSTL forEach tag Q2. Which method of the servlet is overridden in the display servlet example? a) doPost only b) doGet only c) service only d) init only Q3. Which driver class is loaded using Class.forName() in the example? a) com.mysql.jdbc.Driver b) sun.jdbc.odbc.JdbcOdbcDriver c) oracle.jdbc.driver.OracleDriver d) org.h2.Driver Q4. The JDBC URL used in the example is: a) jdbc:mysql://localhost:3306/TEST b) jdbc:odbc:mydsn c) jdbc:oracle:thin:@localhost:1521:xe d) jdbc:postgresql://localhost/test Q5. Which object is used to execute the query "select * from employee"? a) PreparedStatement stmt = con.prepareStatement(...) b) Statement stmt = con.createStatement(); stmt.executeQuery(...) c) CallableStatement d) ResultSet stmt = con.getResultSet() Q6. The cursor moves through result rows using a loop of the form: a) while (rs.next()) { ... } b) while (rs.hasNext()) { ... } c) for (Row r : rs) { ... } d) if (rs.cursor()) { ... } Q7. Which content type is set before writing the HTML output? a) res.setContentType("text/html"); b) res.setContentType("application/json"); c) res.setType("text/html"); d) res.setHeader("html"); Q8. The "DSN" (data source name) mentioned in the session is created from: a) Control Panel > Administrative Tools > Data Sources (ODBC) b) MySQL Workbench only c) The browser settings d) The web.xml editor Q9. The servlet is compiled using a command similar to: a) javac -cp servlet-api.jar display.java b) javac display.jsp c) java display.class d) mvn clean install only Q10. In the example, the database table "employee" has which columns? a) id, name, email, country b) empid, empname, sal c) username, password, address d) first, last, age =============================================================================== SESSION 5 - ADDING JSTL SUPPORT =============================================================================== Q1. JSTL stands for: a) Java Standard Template Language b) JSP Standard Tag Library c) Java Servlet Tag Library d) JSON Standard Transformation Language Q2. Which of the following CANNOT be done easily with EL and action elements alone (the reason JSTL is needed)? a) Loop through a collection b) Print a variable c) Set a variable d) Forward a request Q3. For a Maven project, which dependency must be added to avoid errors in JSP pages? a) jstl 1.2 and taglibs standard 1.1.2 b) servlet-api 2.5 c) mysql-connector only d) junit 4.12 Q4. Which prefix is used for the JSTL Core tag library? a) c b) fmt c) sql d) fn Q5. Which JSTL tag library is used for formatting numbers, dates and i18n support? a) core b) fmt c) xml d) functions Q6. Which tag library provides functions mainly for String manipulation such as concatenation and splitting? a) fn (functions) b) c (core) c) x (xml) d) sql Q7. Which core tag establishes a context for mutually exclusive conditional operations using and ? a) b) c) d) Q8. Which JSTL core tag is used for iteration over a collection? a) b) c) d) Q9. The tag is described as being the same as: a) jsp:forward b) jsp:include or the include directive c) jsp:include only for XML d) the page directive Q10. Which tag removes a variable from a given scope? a) b) c) d) =============================================================================== SESSION 6 - UPLOADING FILES WITH SERVLETS AND JSP =============================================================================== Q1. Which form attribute is mandatory on an HTML form used to upload files? a) method="get" b) enctype="multipart/form-data" c) type="application/x-www-form" d) accept="text/html" Q2. Which annotation configures the maximum file size and request size for a multipart servlet? a) @WebServlet b) @MultipartConfig c) @RequestMapping d) @UploadConfig Q3. Which method of HttpServletRequest retrieves all parts (files and form fields) of a multipart request? a) request.getParameters() b) request.getParts() c) request.getFiles() d) request.getInputStreams() Q4. In Servlet 3.1+, the uploaded file name can be obtained directly using: a) part.getFileName() b) part.getSubmittedFileName() c) part.getName() d) part.getHeader("name") Q5. Which expression is used to obtain the real path of the web application on the server? a) getServletContext().getRealPath("") b) request.getRealPath() c) response.getPath() d) File.getcwd() Q6. The Apache Commons FileUpload library requires which two dependencies? a) commons-fileupload and commons-io b) gson and jackson c) log4j and slf4j d) junit and mockito Q7. Which class is used to check whether an incoming request is a multipart content request? a) ServletFileUpload.isMultipartContent(request) b) MultipartRequest.isMulti(request) c) FileItem.isMultipart(request) d) HttpServletRequest.isMulti() Q8. Which factory class is configured with a size threshold and a repository (temporary directory) for uploads? a) DiskFileItemFactory b) MemoryFileFactory c) ServletFileFactory d) UploadItemFactory Q9. In the helper getFileName(Part part), the file name is extracted from the part header named: a) content-type b) content-disposition c) content-length d) content-encoding Q10. After compiling the project into a .war file, it is deployed by: a) Dropping it into the local Tomcat instance and starting it b) Uploading it to the browser cache c) Copying it to the MySQL data directory d) Running it with the java command directly =============================================================================== ANSWER SHEET =============================================================================== SESSION 1 - FORMS AND SESSION MANAGEMENT ----------------------------------------- Q1 : a Q2 : b Q3 : b Q4 : c Q5 : b Q6 : b Q7 : d Q8 : c Q9 : b Q10 : c SESSION 2 - DATABASE CONNECTIVITY ----------------------------------------- Q1 : b Q2 : c Q3 : b Q4 : b Q5 : b Q6 : c Q7 : a Q8 : a Q9 : a Q10 : a SESSION 3 - CRUD OPERATIONS IN DATABASE ----------------------------------------- Q1 : a Q2 : d Q3 : c Q4 : c Q5 : b Q6 : b Q7 : b Q8 : b Q9 : a Q10 : b SESSION 4 - LISTING DATA ON WEB PAGE ----------------------------------------- Q1 : b Q2 : b Q3 : b Q4 : b Q5 : b Q6 : a Q7 : a Q8 : a Q9 : a Q10 : b SESSION 5 - ADDING JSTL SUPPORT ----------------------------------------- Q1 : b Q2 : a Q3 : a Q4 : a Q5 : b Q6 : a Q7 : b Q8 : b Q9 : b Q10 : b SESSION 6 - UPLOADING FILES WITH SERVLETS AND JSP ----------------------------------------- Q1 : b Q2 : b Q3 : b Q4 : b Q5 : a Q6 : a Q7 : a Q8 : a Q9 : b Q10 : a =============================================================================== END OF QUIZ - 60 QUESTIONS TOTAL ===============================================================================