SCWCD
Question 1
What will be the output of the following JSP code?
<% int a = 10; %>
<%! int a = 20; %>
<%! int b = 30; %>
The value of b multiplied by a is <%= b * a %>
· A) The code will not compile
· B) The value of b multiplied by a is 30
· C) The value of b multiplied by a is 300
· D) The value of b multiplied by a is 600
· E) The value of b multiplied by a is 0
Answer
C
Since the local variable "a" has precedence over the global variable "a", the expression (b * a) evaluates as (30 *10), which equals 300
Question 2
Consider the following code snippet of servlet code:
public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String dbValue = getValueFromDB ();
if (dbValue == null) response.sendError (HttpServletResponse.SC_NOT_FOUND, "DB Lookup Failed");
response.sendRedirect ("Form.jsp"); }
If the getValueFromDB () method returns null , which of the following statements are true?
· A) The code will work without any errors or exceptions
· B) An IllegalStateException will be thrown
· C) An IOException will be thrown
· D) A NullPointerException will be thrown
Answer
B
When the sendError() API is called, the response is considered committed and should not be written to subsequently. Once the response is considered committed, any subsequent redirect operations will throw an IllegalStateException.
In this example, the sendError() is invoked when the dbValue is null, which will cause the response to be committed. Hence, the subsequent response.sendRedirect () method will then cause an IllegalStateException to be thrown.
Question 3
Let's assume you are developing a custom tag handler using the BodyTag interface. The BodyTag interface extends the IterationTag interface by defining additional methods that allow a tag handler to evaluate its body content in a temporary buffer.
Upon execution, your tag handler returns the EVAL_BODY_BUFFERED return value from the doStartTag() method. In addition, your tag handler returns the EVAL_BODY_AGAIN value two times from the doAfterBody() method.
Based on this information, how many times will the setBodyContent() method be called by the JSP container?
· A) Zero
· B) One
· C) Two
· D) Three
Answer
B
Question 4
Let's assume you are deploying a legacy JSP page using a JSP container that complies with the JSP 2.0 specifications. In JSP 2.0, EL (Expression Language) constructs will be interpreted by default. However, you are concerned that this may cause problems for your legacy JSP application.
Which of the following can be used to configure the JSP container to ignore EL expressions?
· A) <%@ page isELIgnored="false" %>
· B) <%@ page isELIgnored="true" %>
· C) <%@ include isELIgnored="false" %>
· D) <%@ import isELIgnored="false" %>
Answer
B
When upgrading a web application to JSP 2.0, EL expressions will be parsed and interpreted by default. However, the isELIgnored page directive attribute can be used to deactivate EL for entire translation units. Alternatively, the escape sequence '\$' can be used to escape EL expressions that should not be interpreted by the container.
Question 5
Let's assume you have a customerBean which has an address property object. This address object has a city property.
Which of the following is a valid way to set the city property of the customerBean.address object using the appropriate JSTL tags?
· A) <c:set target="${customerBean.address}" property="city" value = "${city}" />
· B) <c:set property="customerBean.address" value="${city}" />
· C) <c:set target="${customerBean}" property="address.city" value = "${city}" />
· D) <c:set var="${customerBean.address}" property="city" value = "${city}" />
Answer
A