Unit 4: JavaServer Pages - Practice Quiz

CSE406 — Advanced Java Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 What happens first when a JSP page is requested and has not yet been compiled?

Lifecycle of a JSP page Easy
A. The jspDestroy() method is called
B. The generated response is discarded
C. The JSP is translated into a servlet
D. The session object is invalidated

2 Which JSP lifecycle method is normally called only once after the servlet instance is created?

Lifecycle of a JSP page Easy
A. jspDestroy()
B. jspInit()
C. _jspService()
D. serviceRequest(), which processes every request received from a browser

3 Which method processes each client request for a JSP page?

Lifecycle of a JSP page Easy
A. jspInit()
B. jspDestroy()
C. _jspService()
D. translateJsp()

4 Which method is called before a JSP servlet is removed from service?

Lifecycle of a JSP page Easy
A. _jspService()
B. jspStart()
C. jspInit()
D. jspDestroy()

5 Which directory commonly stores compiled Java classes in a Java web application?

Directory structure of JSP Easy
A. WEB-INF/pages
B. WEB-INF/classes
C. META-INF/classes
D. WEB-INF/resources/classes/generated/application/source

6 Which directory commonly stores JAR library files for a JSP web application?

Directory structure of JSP Easy
A. WEB-INF/bin
B. WEB-INF/lib
C. META-INF/jar
D. WEB-INF/src

7 What is generally true about a JSP file placed inside the WEB-INF directory?

Directory structure of JSP Easy
A. It is compiled only when the application server is permanently stopped
B. It is always accessible through a public URL
C. It cannot be requested directly by a browser
D. It is automatically converted into a JavaBean

8 Which package traditionally contains the core JSP API classes and interfaces?

JSP API Easy
A. java.jsp.servlet
B. javax.servlet.jsp
C. javax.servlet.http
D. java.server.pages

9 Which JSP API class provides access to page-related attributes and servlet objects?

JSP API Easy
A. PageContext
B. ServletConfig
C. HttpSession
D. ServletOutputStream, which writes raw binary response data to the client

10 Which syntax represents a JSP scriptlet tag?

Scriptlet tag Easy
A. <%! declaration %>
B. <% Java code %>
C. <%@ directive %>
D. <%= expression %>

11 What can be written inside a JSP scriptlet?

Scriptlet tag Easy
A. Java statements
B. Only HTML comments
C. Only XML elements
D. A complete deployment descriptor containing servlet and security configuration

12 Which syntax is used for a JSP expression?

Expression tag Easy
A. <%= value %>
B. <%! value %>
C. <% value %>
D. <%@ value %>

13 What is the main purpose of a JSP expression tag?

Expression tag Easy
A. To define multiple servlet mappings and request filters in the deployment descriptor
B. To display an evaluated value
C. To declare an instance method
D. To configure the web server

14 Which syntax represents a JSP declaration tag?

Declaration tag Easy
A. <% declaration %>
B. <%@ declaration %>
C. <%! declaration %>
D. <%= declaration %>

15 What is commonly declared using a JSP declaration tag?

Declaration tag Easy
A. Servlet mappings, filter chains, listeners, and application-level security constraints
B. Request parameters
C. Fields and methods
D. HTML form controls

16 Which JSP implicit object represents the client's HTTP request?

JSP implicit objects Easy
A. session
B. application
C. request
D. response

17 Which JSP implicit object is used to write content to the response?

JSP implicit objects Easy
A. out
B. exception, which is available only when the current page is configured as an error page
C. page
D. config

18 Which JSP implicit object stores data associated with a user's session?

JSP implicit objects Easy
A. request
B. pageContext
C. session
D. config

19 Which JSP action tag includes another resource while processing the current request?

JSP action tags Easy
A. <jsp:include>
B. <jsp:useBean>
C. <jsp:setProperty>
D. <jsp:forward>

20 Which JSP action tag transfers request processing to another resource?

JSP action tags Easy
A. <jsp:useBean>, which locates or creates a JavaBean and associates it with a scope
B. <jsp:include>
C. <jsp:getProperty>
D. <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?

Lifecycle of a JSP page Medium
A. Service the request, translate JSP, compile it, initialize it
B. Initialize JSP, translate servlet, compile it, service the request
C. Translate JSP, compile servlet, initialize it, service the request
D. Compile JSP, initialize servlet, translate it, service the request

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?

Lifecycle of a JSP page Medium
A. Open it in jspDestroy() and release it in jspInit()
B. Open it in _jspService() and release it in jspDestroy()
C. Open it in _jspService() and release it after each expression
D. Open it in 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?

Lifecycle of a JSP page Medium
A. Ignore the modification until the server is restarted
B. Call only jspInit() on the existing servlet instance
C. Execute the modified JSP without compiling it again
D. Retranslate and recompile the JSP before serving it

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?

Directory structure of JSP Medium
A. WEB-INF/lib/com/example/Cart.class
B. META-INF/classes/com/example/Cart.class
C. WEB-INF/jsp/com/example/Cart.class
D. 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?

Directory structure of JSP Medium
A. META-INF/views/reports.jsp
B. views/public/reports.jsp
C. resources/views/reports.jsp
D. 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?

JSP API Medium
A. javax.servlet.ServletOutputStream
B. javax.servlet.jsp.JspWriter
C. java.io.PrintWriter
D. java.io.BufferedWriter

27 The same attribute name exists in page, request, session, and application scopes. Which value is returned by pageContext.findAttribute("status")?

JSP API Medium
A. The value stored in page scope
B. The value stored in request scope
C. The value stored in session scope
D. The value stored in application scope

28 Which statement correctly describes the _jspService() method defined by the JSP servlet model?

JSP API Medium
A. The container invokes it only before servlet destruction
B. The container invokes it for each request to the JSP
C. The JSP author overrides it for one-time initialization
D. The JSP author calls it to translate JSP source code

29 Which JSP fragment correctly uses a scriptlet to display Adult only when the request parameter age represents a value of at least 18?

Scriptlet tag Medium
A. <%= if (Integer.parseInt(request.getParameter("age")) >= 18) { %>Adult<% } %>
B. <%! if (Integer.parseInt(request.getParameter("age")) >= 18) { %>Adult<% } %>
C. <% if (Integer.parseInt(request.getParameter("age")) >= 18) { %>Adult<% } %>
D. <% 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?

Scriptlet tag Medium
A. As a static field of the generated servlet
B. As an instance field of the generated servlet
C. As a local variable inside _jspService()
D. As an attribute in the application scope

31 Which scriptlet is functionally equivalent to the JSP expression <%= product.getPrice() %>?

Expression tag Medium
A. <% return product.getPrice(); %>
B. <% pageContext.setAttribute("price", product); %>
C. <% out.print(product.getPrice()); %>
D. <% response.setStatus(product.getPrice()); %>

32 A page contains <%= request.getParameter("name").toUpperCase() %>. What happens if the request has no name parameter?

Expression tag Medium
A. An empty string is automatically written to the response
B. A NullPointerException occurs during request processing
C. The literal text name is written to the response
D. The expression is skipped without producing an error

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?

Declaration tag Medium
A. <%= String formatName(String s) { return s.trim(); } %>
B. <%@ String formatName(String s) { return s.trim(); } %>
C. <% String formatName(String s) { return s.trim(); } %>
D. <%! 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?

Declaration tag Medium
A. visits is recreated as a local variable for every expression
B. visits is a shared servlet field updated without synchronization
C. visits is reset whenever the response buffer is flushed
D. 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?

JSP implicit objects Medium
A. Set <%@ page errorPage="true" %> on the error page
B. Set <%@ page isErrorPage="true" %> on the error page
C. Set <%@ page autoFlush="true" %> on the error page
D. Set <%@ page session="true" %> on the error page

36 A JSP declares <%@ page session="false" %>. Which implicit object is unavailable to that page?

JSP implicit objects Medium
A. session
B. application
C. request
D. 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?

JSP implicit objects Medium
A. request
B. session
C. application
D. 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 action tags Medium
A. <jsp:include page="menu.jsp" />
B. <%@ include file="menu.jsp" %>
C. <jsp:forward page="menu.jsp" />
D. <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?

JSP action tags Medium
A. An IllegalStateException occurs because the response is committed
B. The container clears the response and performs the forward normally
C. The output of login.jsp is appended to the existing response
D. The browser receives a redirect request to 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?

JSP action tags Medium
A. A translation error is generated immediately
B. The existing bean is moved to page scope
C. The existing session bean is reused
D. A new bean replaces the existing bean

41 For the first successful request to a newly deployed JSP that has not been precompiled, which lifecycle sequence is correct?

Lifecycle of a JSP page Hard
A. Translate JSP, compile servlet, load and instantiate it, call jspInit(), then call _jspService()
B. Compile JSP directly, instantiate JspPage, call _jspService(), then call jspInit()
C. Translate JSP, call jspInit(), compile servlet, instantiate it, then call _jspService()
D. Load servlet class, translate JSP, call _jspService(), compile servlet, call jspInit()

42 A JSP contains the declaration <%! public void _jspService(HttpServletRequest r, HttpServletResponse s) { } %>. What is the expected result?

Lifecycle of a JSP page Hard
A. The declaration is ignored because lifecycle methods cannot appear in JSP pages
B. The declared method replaces the container-generated service implementation
C. The declared method runs immediately before the generated service method
D. The page fails translation or compilation because _jspService() is container-generated

43 A JSP is deployed as /WEB-INF/reports/summary.jsp. Which statement about accessing it is correct?

Directory structure of JSP Hard
A. It is inaccessible both directly and through server-side dispatching
B. It is directly accessible only when the request uses HTTPS
C. It is protected from direct requests but reachable through a request dispatcher
D. It becomes directly accessible after being mapped in the welcome-file list

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?

Directory structure of JSP Hard
A. META-INF/classes/com/acme/Report.class
B. WEB-INF/classes/com/acme/Report.class
C. WEB-INF/com/acme/Report.class
D. classes/WEB-INF/com/acme/Report.class

45 Which statement correctly describes the relationship among the core JSP page interfaces?

JSP API Hard
A. JspPage extends HttpServlet, while HttpJspPage adds jspInit() and jspDestroy()
B. JspPage extends HttpJspPage and declares all three JSP lifecycle methods
C. HttpJspPage extends JspPage and declares _jspService() for HTTP requests
D. 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?

JSP API Hard
A. clear() can fail after a flush, while clearBuffer() cannot retract content already emitted
B. clearBuffer() retracts flushed bytes, whereas clear() only removes unflushed characters
C. clear() retracts flushed bytes, whereas clearBuffer() only resets the writer index
D. Both methods retract flushed bytes, but clear() also resets the response headers

47 Assuming no surrounding whitespace, what is produced by <% for (int i = 0; i < 2; i++) { %><%= i %><% } %>?

Scriptlet tag Hard
A. A compilation error, because a Java block cannot span multiple JSP tags
B. 0, because the expression terminates the generated Java loop body
C. 11, because the expression is evaluated after the loop increments i
D. 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?

Scriptlet tag Hard
A. The method becomes a local function available only during the request
B. Compilation fails because Java does not allow method declarations inside a method
C. The method becomes a private method of the generated servlet class
D. Translation succeeds, but calling the method throws IllegalAccessException

49 What is the result of placing <%= response.flushBuffer() %> in a JSP?

Expression tag Hard
A. Compilation fails because the expression has the type void
B. It flushes the response and prints an empty string
C. It flushes the response and prints the current buffer size
D. Translation fails because expressions cannot call response methods

50 Why does <%! String user = request.getParameter("u"); %> normally fail to compile?

Declaration tag Hard
A. request is typed as ServletRequest, which lacks getParameter()
B. Declaration initializers execute before the servlet class is loaded
C. getParameter() may be invoked only from an expression tag
D. 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?

Declaration tag Hard
A. The field is shared by concurrent requests, and hits++ can lose updates
B. Each request receives a separate hits field because _jspService() creates it
C. Each session receives a separate field because JSP instances are session-scoped
D. The field is automatically synchronized by the generated servlet container code

52 The attribute key exists in all four standard scopes with different values. What does pageContext.findAttribute("key") return?

JSP implicit objects Hard
A. The application-scope value, because it is shared by the entire web application
B. The page-scope value, because lookup order is page, request, session, application
C. The request-scope value, because request attributes override all other scopes
D. The session-scope value, because it persists longer than page and request scopes

53 A source JSP specifies <%@ page errorPage="failure.jsp" %>. Under what condition can failure.jsp directly use the implicit object exception?

JSP implicit objects Hard
A. It must declare <%@ page session="true" %>
B. It must declare <%@ page isErrorPage="true" %>
C. It must declare <%@ page isThreadSafe="false" %>
D. It must declare <%@ page autoFlush="false" %>

54 A JSP begins with <%@ page session="false" %> and later contains <%= session.getId() %>. What is the expected outcome?

JSP implicit objects Hard
A. The expression retrieves an existing session without creating one
B. The expression prints null when no session already exists
C. Translation or compilation fails because session is unavailable
D. A new session is created only when the expression executes

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?

JSP action tags Hard
A. <%@ include file="fragment.jsp" %> because it recompiles the fragment on each request
B. <jsp:useBean class="fragment.jsp" /> because it loads the fragment dynamically
C. <jsp:include page="fragment.jsp" /> because inclusion occurs during request processing
D. <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?

JSP action tags Hard
A. The uncommitted buffer is cleared, and the target produces the forwarded response
B. The existing content is sent first, and the target output is appended afterward
C. Forwarding always throws IllegalStateException once any content has been written
D. The target runs as an include, after which execution resumes in the original JSP

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>?

JSP action tags Hard
A. A second bean is created, and its count becomes 1
B. The existing bean is reused, and its count becomes 1
C. The existing bean is reused, and its count remains 9
D. A duplicate-bean exception is thrown before the body executes

58 No attribute named items exists in request scope. What happens with <jsp:useBean id="items" type="java.util.List" scope="request" />?

JSP action tags Hard
A. An InstantiationException occurs because no creation class was supplied
B. A null reference is bound to items and processing continues
C. An empty ArrayList is created because it implements the declared type
D. A dynamic proxy implementing 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?

JSP action tags Hard
A. name = null, age = 12, and active = false
B. name = "x", age = 12, and active = true
C. name = "Mira", age = 4, and active = true
D. 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?

JSP action tags Hard
A. new, because the action parameter takes precedence in the included resource
B. old, because original request parameters always have higher precedence
C. null, because duplicate parameter names cancel each other during inclusion
D. A container-dependent value because duplicate parameters have no defined order