Вы находитесь на странице: 1из 7

1)

The following code contains an error. At what stage is the error caught by the JSP container.
1.
2.
3.

<html><body>
<%! String extinctAnimal = "dodo" %>
The <%= extinctAnimal %> once inhabited the forests of the

4.
5.

island of Mauritius.
<body><html>

During the page translation phase.

During the JSP page compilation phase.

When the compiled class is loaded into memory.

When jspInit is invoked.

When _jspService is invoked

Answer 2 is correct. There is an error in the scripting language (Java) of the JSP
declaration at line 2. A semicolon at the end of the declaration is missing as defined
by the Java language specification. During the compilation phase, the Java compiler
will flag this as a compilation error.
Answer 1 is incorrect because the syntax of the tags are valid and the translation
phase is passed successfully. Answers 3, 4 and 5 are incorrect because these phases
will not be reached.
2)
If a JSP page overrides the jspInit() method using a declaration JSP tag, which phase of the
JSP page life-cycle generates the overridden method in the servlet?

page translation

JSP page compilation

call jspInit()

call _jspService()

call jspDestroy()

Answer 1 is correct. During the page translation phase of a JSP page life-cycle, the
JSP page is parsed and a Java file containing the corresponding servlet is created.
The servlet created will contain the declared jspInit() overriding the default jspInit()
method which is declared in a vendor-specific JSP page base class.
Answers 2,3,4 and 5 are incorrect. The Java file is compiled during the JSP page
compilation phase and initialised when the container makes a call to jspInit().
_jspService() is called by the container for each client request and finally the
container makes a call to jspDestroy() when it decides to take the servlet out of
service.
3)
Which of the following statements are valid declarations of a JSP Expression? [Check all
correct answers]

<%= (new java.util.Date()).toLocaleString() %>

<%= System.out.println("j2eecertificate.com") %>

<%= Math.random() * 10; %>

<%= "Variety is the spice of life" %>

<% out.println("j2eecertificate.com") %>

Answers 1 and 4 are correct. A JSP expression element is a Java expression that is evaluated and the result is
coerced to a String. The syntax for a JSP expression is
<%= expression %>.
Note that expressions should not be terminated with a semicolon.

Answer 1 is correct because a String object returned by a method call is a permissible Java expression.
Answer 4 is correct because a String literal is also a permissible Java expression.
Answer 2 is incorrect because the method call does not return an object. Answer 3 is incorrect because the
Java expression is terminated with a semicolon. Answer 5 is incorrect because the JSP tag is a scriptlet.

4)
Which of the following statement is a valid JSP Scriptlet?

<%! boolean scriptlet=true; %>

<%@ boolean scriptlet=true; %>

<% boolean scriptlet=true; %>

<%= boolean scriptlet=true; %>

<jsp:setProperty scriptlet= true;>

Answer 3 is correct. Scriptlets are code fragments for the scripting language
specified in the language directive (Java) and has the following syntax <% scriptlet
%>.
5)
Which of the following are valid JSP Declarations? [Check all correct answers]

1
2
3
4
5

<%! int isHandsome = false %>


<%! public boolean isMemberOfBand(String member)
{ return "Mike Bros".equals(member); } %>
<%! boolean hasCharm = false;
boolean isHandsome = true %>
<%! boolean hasCharm = false;
public boolean isMemberOfBand(String member)
{ return "Mike Bros".equals(member); } %>
<%! boolean hasCharm; %>

Answers 2, 4 and 5 are correct. Declarations are used to declare variables and methods in the scripting
language (Java) used in a JSP page. The syntax for a declaration is <%! declaration(s) %>. Since declarations
are defined in the Java language each variable declaration statement must be terminated with a semicolon.
Answer 2 is correct because a valid Java method has been declared. Answer 4 is correct because a JSP
declaration tag can declare both a variable and method in a single tag. Answer 5 is correct because a JSP
declaration tag can contain an uninitialised variable declaration.
Answers 1 and 3 are incorrect because the variable declarations are missing a terminating semicolon. This
will cause a compile-time error because of the incorrect declarations.
6)
Which of the following statements regarding the directive JSP tag are true? [Check all correct
answers]

The syntax of the directive JSP tag is <%! directive { attr="Value"}* %>

There are three types of directives: page, include and taglib

The tag names, attribute and their values are all case sensitive.

Values must be enclosed in double quotes only.

Directives do not produce any output into the current out stream.

Answers 2, 3 and 5 are correct. Directives provide information about the JSP page
to the JSP container. There are three types of directives: page, include and taglib
and the tag names, attribute and their values are all case sensitive. The directive JSP
tags also do not produce any output into the current out stream.
Answer 1 is incorrect because the syntax of a directive is <%@ directive
{ attr="value" }* %> (note the @ symbol). There may be optional white space after
the "<%@" and before "%>" but there cannot be any spaces between the equals
sign (=) and the value. Answer 4 is incorrect because values may be enclosed with a
pair of single or double quotes.
7)
Given the following JSP code, select the correct statements about the code. [Check all correct
answers]
1.

<html><body>

2.
3.
4.

1
2
3

<%! int hits = 0; %>


The number of hits = <%= hits++ %>
</body></html>

The JSP container will throw a compile error because the JSP declaration
tag is terminated with a semicolon.
Each time a client request is made to the page, the number of hits
displayed is incremented by 1.
Each time a client request is made to the page, the number of hits
displayed is always 0.

The declaration at line 2 is global to the page.

The statement at line 2 declares an integer variable which is


initialised only once when the page is first loaded.

Answers 2, 4 and 5 are correct. The JSP tag at line 2 declares an integer variable
which is global to the JSP page. When this page is first loaded the variable is
initialised only once to 0. Each time a client request is made to the page the variable
is incremented by 1 and displayed together with some other text. Since hits is
declared as a global variable, hits retains its value after each subsequent request.
Answer 1 is incorrect because declarations are defined in the Java language
therefore each variable declaration statement must be terminated with a semicolon.
Answer 3 is incorrect because the variable hits is declared as a global variable. If the
statement at line 2 was declared as <% int hits = 0; %> then the number of hits
displayed would always be 0.
8)
Which of the following statements are valid page directives? [Check all correct answers]

<%@ page language="java" %>

<%@ page buffer="none" isThreadSafe="true"


errorPage="/error.jsp" %>

<%@ page include="privacy.html" %>

<%@ page sessionTimeout="1" %>

<%@ page pageEncoding="ISO-8859-1" %>

Answers 1, 2 and 5 are correct. The page directive defines a number of page dependent properties and
communicates these to the JSP container.
Answer 1 is correct because language is a valid page directive attribute. In JSP 1.2, the only supported
scripting language value for this attribute is "java". Answer 2 is correct because multiple attributes can be
defined for an instance of a page directive. This page directive requests no buffering, indicates that the page is
thread safe, and provides an error page. Answer 5 is correct because pageEncoding is a valid page directive.
This page directive specifies the character encoding of the JSP page.
Answer 3 is incorrect because include is not a valid page directive attribute. include is a separate directive
from the page directive. Answer 4 is incorrect because sessionTimeout is not a valid page directive attribute.
session, however is valid.

9)
Which of the following statements regarding this scriptlet is true? [Check all correct answers]
<% int count=0;
count++; %>

The scriptlet is executed each time the page is accessed.

The count variable retains its value between subsequent client requests.

3
4
5

The Java code in the scriptlet is validated during the translation phase of
the servlet life-cycle.
The Java code in the scriptlet becomes part of the _jspService()
method during the translation phase of the servlet life-cycle.
The Java code in the scriptlet is invalid because there is a semicolon after
the declaration of the count variable.

Answers 1 and 4 are correct. Scriptlets are Java code fragments that are embedded in the JSP page which are
executed each time the page is accessed. In this script the count is declared, initialised to 0 and then
incremented to 1 each time the page is accessed. During the translation phase of the servlet life-cycle the Java
code in the scriptlet become part of the _jspService() method.
This process declares the variable count as a local variable of the _jspService() method.
Answer 2 is incorrect because each time the page is accessed the _jspService() method is executed. Since count
is declared as a local variable in this method, when the method is terminated count goes out of scope and
becomes eligible for garbage collection. Answer 3 is incorrect because the Java code in the scriptlet is validated
during the compilation phase of the servlet life-cycle.
Answer 5 is incorrect because all statements should be separated by semicolons (like standard Java code).

10)
Which of the following JSP page life-cycle phases occur before the container initialises a
servlet instance? [Check all correct answers]

call _jspService()

load class

create instance

page translation

call jspDestroy()

Answers 2, 3, and 4 are correct. Before a container initialises a servlet, the following
life-cycle phases occur; page translation, load class, and create instance, that is, a
servlet is created as a Java file, compiled, loaded into memory and then instantiated.
Answers 1 and 5 are incorrect because call _jspService() and call jspDestroy() occur
after a container initialises a servlet instance.
Note: page compilation also occurs after the page translation and before load class
life-cycle phases