Total Pageviews

Sunday, 24 January 2016

THE JSP TECHNOLOGY MODEL—THE BASICS

Q.1 Consider the following code and select the correct statement about it from the options below. (Select one)
<html><body>
    <%! int aNum=5 %>
        The value of aNum is <%= aNum %>
</body>
</html>
a. It will print "The value of aNum is 5" to the output.
b. It will flag a compile-time error because of an incorrect declaration.
c. It will throw a runtime exception while executing the expression.
d. It will not flag any compile time or runtime errors and will not print anything to the
output.
Answer: b Explanation
It will flag a compile-time error because the variable declaration <%! int aNum=5 %> is missing a ; at the end. It should be
<%! int aNum=5; %>
Q.2 Which of the following tags can you use to print the value of an expression to the
output stream? (Select two)
a. <%@ %>
b. <%! %>
c. <% %>
d. <%= %>
e. <%-- --%>
Answers: c and d Explanation
You can use a JSP expression to print the value of an expression to the output stream. For example, if the expression is x+3, you can write <%= x+3 %>. Answer d is a JSP expression and is therefore the correct answer. But you can also use a scriptlet to print the value of an expression to the output stream as <% out.print(x+3); %>. Answer c is a scriptlet and is therefore also correct. If the exam asks you to select one correct option, then select the expression syntax, as in answer d. But if the exam asks for two correct answers, then select the scriptlet syntax as well.
Q.3 Which of the following methods is defined by the JSP engine? (Select one)
a. jspInit()
b. _jspService()
c. _jspService(ServletRequest, ServletResponse)
d. _jspService(HttpServletRequest, HttpServletResponse)
e. jspDestroy()
Answer: d Explanation
The _jspService() method of the javax.servlet.jsp.HttpJspPage class is defined by the JSP engine. HttpJspPage is meant to serve HTTP requests, and therefore the _jspService() method accepts the javax.servlet. http.HttpServletRequest and javax.servlet.http.HttpServlet - Response parameters.
Q.4 Which of the following exceptions may be thrown by the _jspService() method? (Select one)
a. javax.servlet.ServletException
b. javax.servlet.jsp.JSPException
c. javax.servlet.ServletException and javax.servlet.jsp.JSPException
d. javax.servlet.ServletException and java.io.IOException
e. javax.servlet.jsp.JSPException and java.io.IOException
Answer: d Explanation
The _jspService() method may throw a javax.servlet.ServletException, a java. io. IOException, or a subclass of these two exception classes. Note that the _jspService() method does not define javax.servlet.jsp. JspException in its throws clause.
Q.5 Write the name of the method that you can use to initialize variables declared in a JSP declaration in the space provided. (Write only the name of the method. Do not write the return type, parameters, or parentheses.)
a [_____________]
Answer: jspInit Explanation
The jspInit() method is the first method called by the JSP engine on a JSP page. It is called only once to allow the page to initialize itself. You can use this method to initialize variables declared in JSP declarations (<%! %>).
6. Which of the following correctly declares that the current page is an error page and also enables it to take part in a session? (Select one)
a. <%@ page pageType="errorPage" session="required" %>
b. <%@ page isErrorPage="true" session="mandatory" %>
c. <%@ page errorPage="true" session="true" %>
d. <%@ page isErrorPage="true" session="true" %>
e. None of the above.
Answer: d Explanation
The isErrorPage attribute accepts a Boolean value (true or false) and indicates whether the current page is capable of handling errors. The session attribute accepts a Boolean value (true or false) and indicates whether the current page must take part in a session. Therefore, answer d is correct. Since the pageType attribute is not a valid attribute for a page directive, answer a is not correct. The mandatory value is not a valid value for the session attribute, which means answer b is not correct. The errorPage attribute is a valid
attribute, but it is used for specifying another page as an error handler for the current
page. Therefore, answer c is also incorrect.
THE JSP TECHNOLOGY MODEL—ADVANCED TOPICS
1. What will be the output of the following code? (Select one)
<html><body>
<% x=3; %>
<% int x=5; %>
<%! int x=7; %>
x = <%=x%>, <%=this.x%>
</body></html>
a. x = 3, 5
b. x = 3, 7
c. x = 5, 3
d. x = 5, 7
e. Compilation error
Answer: c Explanation
The above code will translate to servlet code similar to the following:
public class ... {
    int x = 7;
    public void _jspService(…) {
        ...
        out.print("<html><body>");
        x = 3;
        int x = 5;
        out.write("x = "); out.print(x);
        out.write(","); out.print(this.x);
        out.print("</body></html>");
    }
}
The declaration will create a member variable x and initialize it to 7. The first scriptlet, x=3, will change its value to 3. Then, the second scriptlet will declare a local variable x and initialize it to 5. The first expression refers to the local variable x and will therefore print 5. The second expression uses the keyword this to refer to the member or instance variable x, which was set to 3. Thus, the correct answer is c, x = 5, 3.
Q.2 What will be the output of the following code? (Select one)
<html><body>
    The value is <%=""%>
</body></html>
a. Compilation error
b. Runtime error
c. The value is
d. The value is null
Answer: c Explanation
The expression is converted to
out.print("");
Thus, the correct answer is c.
Q.3 Which of the following implicit objects is not available to a JSP page by default?
(Select one)
a application
b session
c exception
d config
Answer: c Explanation
The implicit variables application and config are always available to a JSP page. The implicit variable session is available if the value of the page directive’s session attribute is set to true. Since it is set to true by default, the implicit variable session is also available by default. The implicit variable exception is available only if the value of the page directive’s isErrorPage attribute is set to true. It is set to false by default, so the implicit variable exception is not available by default. We have to explicitly set it to true:
    <%@ page isErrorPage="true" %>
The correct answer, therefore, is c.
Q.4 Which of the following implicit objects can you use to store attributes that need to be accessed from all the sessions of a web application? (Select two)
a. application
b. session
c. request
d. page
e. pageContext
Answers: a and e Explanation
To store attributes that are accessible from all the sessions of a web application, we have to put them in the application scope. To achieve this, we have to use the implicit object application. If the exam asks you to select one answer, then select application. If the exam asks for two correct answers, then read the question carefully. It says, “Which of the following implicit objects can you use to store attributes that need to be accessed from all the sessions of a web application?” We can also use pageContext to store objects in the application scope as pageContext.setAttribute("name", object, ageContext.APPLICATION_
SCOPE); and pageContext.getAttribute("name", PageContext. APPLICATION_SCOPE);.
Q.5 The implicit variable config in a JSP page refers to an object of type: (Select one)
a. javax.servlet.PageConfig
b. javax.servlet.jsp.PageConfig
c. javax.servlet.ServletConfig
d. javax.servlet.ServletContext
Answer: c
The implicit variable config in a JSP page refers to an object of type javax.servlet.ServletConfig.
Q.6 A JSP page can receive context initialization parameters through the deployment descriptor of the web application. 
a True
b False
Answer: a Explanation
Context initialization parameters are specified by the <context-param> tags in web.xml. These parameters are for the whole web application and not specific to any servlet or JSP page. Thus, all components of a web application can access context initialization parameters.
7. Which of the following will evaluate to true? (Select two)
a. page == this
b. pageContext == this
c. out instanceof ServletOutputStream
d. application instanceof ServletContext
Answers: a and d Explanation
The implicit variable page refers to the current servlet, and therefore answer a will evaluate to true. The application object refers to an object of type ServletContext, which means answer d will also evaluate to true. The pageContext object refers to an object of type PageContext and not to the servlet, which means answer b will evaluate to false. The out implicit variable refers to an instance of javax.servlet.jsp.JspWriter and not to an instance of javax.servlet.ServletOutputStream, so answer c evaluates to false. Note that JspWriter is derived from java.io.Writer, while Servlet-OutputStream is derived from java.io.OutputStream.
Q.8 Select the correct statement about the following code. (Select one)
<%@ page language="java" %>
<html><body>
    out.print("Hello ");
    out.print("World ");
</body></html>
a. It will print Hello World in the output.
b. It will generate compile-time errors.
c. It will throw runtime exceptions.
d. It will only print Hello.
e. None of above.
Answer: e Explanation
The lines out.print("Hello ") and out.print("World ") are not contained in a scriptlet (<%...%>). The JSP engine assumes they are a part of the template text and sends them to the browser without executing them on the server. Therefore, it will print the two statements in the browser window: out.print("Hello ");out.print("World ");
Q.9 Select the correct statement about the following code. (Select one)
<%@ page language="java" %>
<html><body>
    <%
        response.getOutputStream().print ("Hello ");
        out.print("World");
    %>
</body></html>
a. It will print Hello World in the output.
b. It will generate compile-time errors.
c. It will throw runtime exceptions.
d. It will only print Hello.
e. None of above.
Answer: c Explanation
As explained in chapter 4, “The Servlet model,” the OutputStream of a response object is used for sending binary data to the client while the Writer object is used for sending character data. However, we cannot use both on the same response object. Since the JSP engine automatically gets the JspWriter from the response object to output the content of the JSP as character data, the call to getOutputStream() throws a java.lang.IllegalStateException. Thus, the correct answer is c.
Q.10 Which of the following implicit objects does not represent a scope container? (Select one)
a. application
b. session
c. request
d. page
e. pageContext
Answer: d Explanation
The implicit objects application, session, and request represent the containers for the scopes, application, session, and request, respectivelyThe implicit object page refers to the generated Servlet and does not represent any scope container. The implicit object pageContext represents the page scope container, so the correct answer is d.
Q.11 What is the output of the following code? (Select one)
<html><body>
<% int i = 10 ;%>
<% while(--i>=0) { %>
out.print(i);
<% } %>
</body></html>
a. 9876543210
b. 9
c. 0
d. None of the above.
Answer: d Explanation
The statement out.print(i) is not inside a scriptlet and is part of the template text. The above JSP page will print 
out.print(i);out.print(i);out.print(i);......
ten times.
When in doubt, always convert a JSP code to its equivalent servlet code step by step:
out.write("<html><body>");
int i = 10;
while (--i>=) {
    out.write("out.print(i); ");
}
out.write("<html><body>");
Q.12 Which of the following is not a valid XML-based JSP tag? (Select one)
a. <jsp:directive.page />
b. <jsp:directive.include />
c. <jsp:directive.taglib />
d. <jsp:declaration></jsp:declaration>
e. <jsp:scriptlet></jsp:scriptlet>
f. <jsp:expression></jsp:expression>
Answer: c Explanation
The tag <jsp:directive.taglib> is not a valid XML-based tag. Remember that tag library information is provided in the <jsp:root> element.
Q.13 Which of the following XML syntax format tags do not have an equivalent in JSP syntax format? (Select two)
a. <jsp:directive.page/>
b. <jsp:directive.include/>
c. <jsp:text></jsp:text>
d. <jsp:root></jsp:root>
e. <jsp:param/>
Answers: c and d Explanation
The equivalent of <jsp:directive.page/> is <%@ page %>. The equivalent of <jsp:directive.include/> is <%@ include %>. The <jsp:param/> tag is the same for both the syntax formats. Thus, the correct answers are c and d. The tags <jsp:text> and <jsp:root> have no equivalent in the JSP syntax format.
Q.14 Which of the following is a valid construct to declare that the implicit variable
session should be made available to the JSP page? (Select one)
a. <jsp:session>true</jsp:session>
b. <jsp:session required="true" />
c. <jsp:directive.page>
        <jsp:attribute name="session" value="true" />
    </jsp:directive.page>
d. <jsp:directive.page session="true" />
e. <jsp:directive.page attribute="session" value="true" />
Answer: d Explanation
The correct way to declare that the implicit variable session should be made available to the JSP page in XML format is shown in answer d: <jsp:directive. page session="true" />.

No comments: