Unit 4: JavaServer Pages - Practice Quiz
1 What happens first when a JSP page is requested and has not yet been compiled?
jspDestroy() method is called
2 Which JSP lifecycle method is normally called only once after the servlet instance is created?
jspDestroy()
jspInit()
_jspService()
serviceRequest(), which processes every request received from a browser
3 Which method processes each client request for a JSP page?
jspInit()
jspDestroy()
_jspService()
translateJsp()
4 Which method is called before a JSP servlet is removed from service?
_jspService()
jspStart()
jspInit()
jspDestroy()
5 Which directory commonly stores compiled Java classes in a Java web application?
WEB-INF/pages
WEB-INF/classes
META-INF/classes
WEB-INF/resources/classes/generated/application/source
6 Which directory commonly stores JAR library files for a JSP web application?
WEB-INF/bin
WEB-INF/lib
META-INF/jar
WEB-INF/src
7
What is generally true about a JSP file placed inside the WEB-INF directory?
8 Which package traditionally contains the core JSP API classes and interfaces?
java.jsp.servlet
javax.servlet.jsp
javax.servlet.http
java.server.pages
9 Which JSP API class provides access to page-related attributes and servlet objects?
PageContext
ServletConfig
HttpSession
ServletOutputStream, which writes raw binary response data to the client
10 Which syntax represents a JSP scriptlet tag?
<%! declaration %>
<% Java code %>
<%@ directive %>
<%= expression %>
11 What can be written inside a JSP scriptlet?
12 Which syntax is used for a JSP expression?
<%= value %>
<%! value %>
<% value %>
<%@ value %>
13 What is the main purpose of a JSP expression tag?
14 Which syntax represents a JSP declaration tag?
<% declaration %>
<%@ declaration %>
<%! declaration %>
<%= declaration %>
15 What is commonly declared using a JSP declaration tag?
16 Which JSP implicit object represents the client's HTTP request?
session
application
request
response
17 Which JSP implicit object is used to write content to the response?
out
exception, which is available only when the current page is configured as an error page
page
config
18 Which JSP implicit object stores data associated with a user's session?
request
pageContext
session
config
19 Which JSP action tag includes another resource while processing the current request?
<jsp:include>
<jsp:useBean>
<jsp:setProperty>
<jsp:forward>
20 Which JSP action tag transfers request processing to another resource?
<jsp:useBean>, which locates or creates a JavaBean and associates it with a scope
<jsp:include>
<jsp:getProperty>
<jsp:forward>
21 A JSP page is requested for the first time after deployment. Which sequence best represents the major lifecycle stages performed by the container?
22 A JSP needs to open a database connection pool once when its generated servlet is initialized and release it before the servlet is removed. Which methods are most appropriate?
jspDestroy() and release it in jspInit()
_jspService() and release it in jspDestroy()
_jspService() and release it after each expression
jspInit() and release it in jspDestroy()
23 A JSP file is modified after it has already handled several requests. What will a typical JSP container do on the next request?
jspInit() on the existing servlet instance
24
A JSP imports the class com.example.Cart, which is not packaged in a JAR. Where should Cart.class be placed in the web application?
WEB-INF/lib/com/example/Cart.class
META-INF/classes/com/example/Cart.class
WEB-INF/jsp/com/example/Cart.class
WEB-INF/classes/com/example/Cart.class
25
A developer wants reports.jsp to be accessible only through a server-side forward, not by entering its URL directly. Which location is most suitable?
META-INF/views/reports.jsp
views/public/reports.jsp
resources/views/reports.jsp
WEB-INF/views/reports.jsp
26
A JSP writes a large amount of generated text using the implicit out object. What is the declared JSP API type of this object?
javax.servlet.ServletOutputStream
javax.servlet.jsp.JspWriter
java.io.PrintWriter
java.io.BufferedWriter
27
The same attribute name exists in page, request, session, and application scopes. Which value is returned by pageContext.findAttribute("status")?
28
Which statement correctly describes the _jspService() method defined by the JSP servlet model?
29
Which JSP fragment correctly uses a scriptlet to display Adult only when the request parameter age represents a value of at least 18?
<%= if (Integer.parseInt(request.getParameter("age")) >= 18) { %>Adult<% } %>
<%! if (Integer.parseInt(request.getParameter("age")) >= 18) { %>Adult<% } %>
<% if (Integer.parseInt(request.getParameter("age")) >= 18) { %>Adult<% } %>
<% if (Integer.parseInt(request.getParameter("age")) >= 18) %>Adult<% endif %>
30
Consider the fragment <% int total = 0; total++; %>. Where is total normally declared in the generated servlet?
_jspService()
31
Which scriptlet is functionally equivalent to the JSP expression <%= product.getPrice() %>?
<% return product.getPrice(); %>
<% pageContext.setAttribute("price", product); %>
<% out.print(product.getPrice()); %>
<% response.setStatus(product.getPrice()); %>
32
A page contains <%= request.getParameter("name").toUpperCase() %>. What happens if the request has no name parameter?
NullPointerException occurs during request processing
name is written to the response
33
A JSP must define a helper method named formatName that can be called from multiple expressions in the page. Which syntax declares it as a generated servlet member?
<%= String formatName(String s) { return s.trim(); } %>
<%@ String formatName(String s) { return s.trim(); } %>
<% String formatName(String s) { return s.trim(); } %>
<%! String formatName(String s) { return s.trim(); } %>
34
A JSP contains <%! int visits = 0; %> followed by <%= ++visits %>. Why can the displayed count be unreliable under concurrent requests?
visits is recreated as a local variable for every expression
visits is a shared servlet field updated without synchronization
visits is reset whenever the response buffer is flushed
visits is automatically stored separately in each user session
35
A custom error page needs to display the exception that caused request processing to fail. Which configuration makes the implicit exception object available?
<%@ page errorPage="true" %> on the error page
<%@ page isErrorPage="true" %> on the error page
<%@ page autoFlush="true" %> on the error page
<%@ page session="true" %> on the error page
36
A JSP declares <%@ page session="false" %>. Which implicit object is unavailable to that page?
session
application
request
response
37 A value must be shared by all users and all JSP pages within the same deployed web application. Which implicit object should store it?
request
session
application
pageContext
38
A JSP must include the current output of menu.jsp every time the page is requested. Which standard action is most appropriate?
<jsp:include page="menu.jsp" />
<%@ include file="menu.jsp" %>
<jsp:forward page="menu.jsp" />
<jsp:useBean id="menu" class="menu.jsp" />
39
A JSP has already flushed its response buffer and then executes <jsp:forward page="login.jsp" />. What is the most likely result?
IllegalStateException occurs because the response is committed
login.jsp is appended to the existing response
login.jsp
40
Consider <jsp:useBean id="cart" class="shop.Cart" scope="session" />. What happens when the session already contains an attribute named cart of the expected type?
41 For the first successful request to a newly deployed JSP that has not been precompiled, which lifecycle sequence is correct?
jspInit(), then call _jspService()
JspPage, call _jspService(), then call jspInit()
jspInit(), compile servlet, instantiate it, then call _jspService()
_jspService(), compile servlet, call jspInit()
42
A JSP contains the declaration <%! public void _jspService(HttpServletRequest r, HttpServletResponse s) { } %>. What is the expected result?
_jspService() is container-generated
43
A JSP is deployed as /WEB-INF/reports/summary.jsp. Which statement about accessing it is correct?
44
A loose compiled class has the binary name com.acme.Report and must be visible to JSP pages in a standard WAR. Where should it be placed?
META-INF/classes/com/acme/Report.class
WEB-INF/classes/com/acme/Report.class
WEB-INF/com/acme/Report.class
classes/WEB-INF/com/acme/Report.class
45 Which statement correctly describes the relationship among the core JSP page interfaces?
JspPage extends HttpServlet, while HttpJspPage adds jspInit() and jspDestroy()
JspPage extends HttpJspPage and declares all three JSP lifecycle methods
HttpJspPage extends JspPage and declares _jspService() for HTTP requests
HttpJspPage extends GenericServlet without inheriting the JspPage contract
46
A buffered JspWriter has already flushed some content to the client. Which distinction between clear() and clearBuffer() is correct?
clear() can fail after a flush, while clearBuffer() cannot retract content already emitted
clearBuffer() retracts flushed bytes, whereas clear() only removes unflushed characters
clear() retracts flushed bytes, whereas clearBuffer() only resets the writer index
clear() also resets the response headers
47
Assuming no surrounding whitespace, what is produced by <% for (int i = 0; i < 2; i++) { %><%= i %><% } %>?
0, because the expression terminates the generated Java loop body
11, because the expression is evaluated after the loop increments i
01, because the Java loop spans the separated JSP scripting elements
48
What happens when a JSP contains <% int calculate() { return 42; } %> inside a scriptlet?
IllegalAccessException
49
What is the result of placing <%= response.flushBuffer() %> in a JSP?
void
50
Why does <%! String user = request.getParameter("u"); %> normally fail to compile?
request is typed as ServletRequest, which lacks getParameter()
getParameter() may be invoked only from an expression tag
request is local to _jspService() and is unavailable in member scope
51
A JSP declares <%! int hits = 0; %> and executes <% hits++; %> for every request. Under the usual servlet threading model, which statement is correct?
hits++ can lose updates
hits field because _jspService() creates it
52
The attribute key exists in all four standard scopes with different values. What does pageContext.findAttribute("key") return?
53
A source JSP specifies <%@ page errorPage="failure.jsp" %>. Under what condition can failure.jsp directly use the implicit object exception?
<%@ page session="true" %>
<%@ page isErrorPage="true" %>
<%@ page isThreadSafe="false" %>
<%@ page autoFlush="false" %>
54
A JSP begins with <%@ page session="false" %> and later contains <%= session.getId() %>. What is the expected outcome?
null when no session already exists
session is unavailable
55
A parent JSP was translated yesterday. Today, fragment.jsp is modified, but the parent is not retranslated. Which mechanism is designed to use the fragment's current request-time output?
<%@ include file="fragment.jsp" %> because it recompiles the fragment on each request
<jsp:useBean class="fragment.jsp" /> because it loads the fragment dynamically
<jsp:include page="fragment.jsp" /> because inclusion occurs during request processing
<jsp:forward page="fragment.jsp" /> because it resumes the parent after forwarding
56
A JSP writes content into an uncommitted response buffer and then executes <jsp:forward page="target.jsp" />. What should occur?
IllegalStateException once any content has been written
57
A request-scope bean named c already exists with count = 9. What happens here: <jsp:useBean id="c" class="com.acme.Counter" scope="request"><jsp:setProperty name="c" property="count" value="1" /></jsp:useBean>?
count becomes 1
count becomes 1
count remains 9
58
No attribute named items exists in request scope. What happens with <jsp:useBean id="items" type="java.util.List" scope="request" />?
InstantiationException occurs because no creation class was supplied
null reference is bound to items and processing continues
ArrayList is created because it implements the declared type
List is created by the JSP container
59
A bean initially has name = "Mira", age = 4, and active = true. The request parameters are age=12 and extra=x. After <jsp:setProperty name="person" property="*" />, what state is expected?
name = null, age = 12, and active = false
name = "x", age = 12, and active = true
name = "Mira", age = 4, and active = true
name = "Mira", age = 12, and active = true
60
The original request contains mode=old, and a JSP performs <jsp:include page="target.jsp"><jsp:param name="mode" value="new" /></jsp:include>. What does request.getParameter("mode") return inside target.jsp?
new, because the action parameter takes precedence in the included resource
old, because original request parameters always have higher precedence
null, because duplicate parameter names cancel each other during inclusion
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →