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 session object is invalidated
B. The jspDestroy() method is called
C. The JSP is translated into a servlet
D. The generated response is discarded

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

10 Which syntax represents a JSP scriptlet tag?

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

11 What can be written inside a JSP scriptlet?

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

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 display an evaluated value
B. To configure the web server
C. To declare an instance method
D. To define multiple servlet mappings and request filters in the deployment descriptor

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. Fields and methods
B. Request parameters
C. Servlet mappings, filter chains, listeners, and application-level security constraints
D. HTML form controls

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

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

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

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

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

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

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

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

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

JSP action tags Easy
A. <jsp:include>
B. <jsp:useBean>, which locates or creates a JavaBean and associates it with a scope
C. <jsp:forward>
D. <jsp:getProperty>

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. Compile JSP, initialize servlet, translate it, service the request
B. Service the request, translate JSP, compile it, initialize it
C. Translate JSP, compile servlet, initialize it, service the request
D. Initialize JSP, translate servlet, compile 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 _jspService() and release it in jspDestroy()
B. Open it in jspInit() and release it in jspDestroy()
C. Open it in _jspService() and release it after each expression
D. Open it in jspDestroy() and release it in jspInit()

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. Execute the modified JSP without compiling it again
B. Call only jspInit() on the existing servlet instance
C. Ignore the modification until the server is restarted
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/classes/com/example/Cart.class
D. WEB-INF/jsp/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. WEB-INF/views/reports.jsp
D. resources/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. java.io.PrintWriter
B. javax.servlet.jsp.JspWriter
C. java.io.BufferedWriter
D. javax.servlet.ServletOutputStream

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 application scope
B. The value stored in session scope
C. The value stored in request scope
D. The value stored in page 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 JSP author calls it to translate JSP source code
C. The JSP author overrides it for one-time initialization
D. The container invokes it for each request to the JSP

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<% endif %>
C. <% if (Integer.parseInt(request.getParameter("age")) >= 18) { %>Adult<% } %>
D. <%= if (Integer.parseInt(request.getParameter("age")) >= 18) { %>Adult<% } %>

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 attribute in the application scope
C. As a local variable inside _jspService()
D. As an instance field of the generated servlet

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

Expression tag Medium
A. <% out.print(product.getPrice()); %>
B. <% return product.getPrice(); %>
C. <% pageContext.setAttribute("price", product); %>
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. The expression is skipped without producing an error
B. The literal text name is written to the response
C. An empty string is automatically written to the response
D. A NullPointerException occurs during request processing

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 reset whenever the response buffer is flushed
B. visits is automatically stored separately in each user session
C. visits is a shared servlet field updated without synchronization
D. visits is recreated as a local variable for every expression

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 session="true" %> on the error page
C. Set <%@ page autoFlush="true" %> on the error page
D. Set <%@ page isErrorPage="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. application
B. response
C. request
D. session

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. pageContext
B. session
C. request
D. application

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. <%@ include file="menu.jsp" %>
B. <jsp:forward page="menu.jsp" />
C. <jsp:include 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. The browser receives a redirect request to login.jsp
B. An IllegalStateException occurs because the response is committed
C. The output of login.jsp is appended to the existing response
D. The container clears the response and performs the forward normally

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. The existing session bean is reused
B. The existing bean is moved to page scope
C. A translation error is generated immediately
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. Load servlet class, translate JSP, call _jspService(), compile servlet, call jspInit()
B. Translate JSP, call jspInit(), compile servlet, instantiate it, then call _jspService()
C. Translate JSP, compile servlet, load and instantiate it, call jspInit(), then call _jspService()
D. Compile JSP directly, instantiate JspPage, call _jspService(), then 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 page fails translation or compilation because _jspService() is container-generated
B. The declared method runs immediately before the generated service method
C. The declaration is ignored because lifecycle methods cannot appear in JSP pages
D. The declared method replaces the container-generated service implementation

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 becomes directly accessible after being mapped in the welcome-file list
B. It is inaccessible both directly and through server-side dispatching
C. It is directly accessible only when the request uses HTTPS
D. It is protected from direct requests but reachable through a request dispatcher

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. WEB-INF/classes/com/acme/Report.class
B. classes/WEB-INF/com/acme/Report.class
C. META-INF/classes/com/acme/Report.class
D. 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. HttpJspPage extends JspPage and declares _jspService() for HTTP requests
C. JspPage extends HttpJspPage and declares all three JSP lifecycle methods
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. clearBuffer() retracts flushed bytes, whereas clear() only removes unflushed characters
B. Both methods retract flushed bytes, but clear() also resets the response headers
C. clear() retracts flushed bytes, whereas clearBuffer() only resets the writer index
D. clear() can fail after a flush, while clearBuffer() cannot retract content already emitted

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

Scriptlet tag Hard
A. 11, because the expression is evaluated after the loop increments i
B. 0, because the expression terminates the generated Java loop body
C. 01, because the Java loop spans the separated JSP scripting elements
D. A compilation error, because a Java block cannot span multiple JSP tags

48 What happens when a JSP contains <% int calculate() { return 42; } %> inside a scriptlet?

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

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

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

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. request is local to _jspService() and is unavailable in member scope
C. getParameter() may be invoked only from an expression tag
D. Declaration initializers execute before the servlet class is loaded

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. The field is automatically synchronized by the generated servlet container code
C. Each request receives a separate hits field because _jspService() creates it
D. Each session receives a separate field because JSP instances are session-scoped

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 request-scope value, because request attributes override all other scopes
C. The page-scope value, because lookup order is page, request, session, application
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 autoFlush="false" %>
B. It must declare <%@ page isErrorPage="true" %>
C. It must declare <%@ page isThreadSafe="false" %>
D. It must declare <%@ page session="true" %>

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 prints null when no session already exists
B. A new session is created only when the expression executes
C. The expression retrieves an existing session without creating one
D. Translation or compilation fails because 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?

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 target runs as an include, after which execution resumes in the original JSP
C. Forwarding always throws IllegalStateException once any content has been written
D. The existing content is sent first, and the target output is appended afterward

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. A duplicate-bean exception is thrown before the body executes
C. The existing bean is reused, and its count becomes 1
D. The existing bean is reused, and its 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" />?

JSP action tags Hard
A. An InstantiationException occurs because no creation class was supplied
B. An empty ArrayList is created because it implements the declared type
C. A null reference is bound to items and processing continues
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 = "Mira", age = 4, and active = true
C. name = "Mira", age = 12, and active = true
D. name = "x", 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. A container-dependent value because duplicate parameters have no defined order
B. new, because the action parameter takes precedence in the included resource
C. old, because original request parameters always have higher precedence
D. null, because duplicate parameter names cancel each other during inclusion