Java JSP Questions

A small note here – In the real exam, there will be less questions of type fill in the blanks, as compared to these set of questions. Also the real exam questions will probably be more difficult than these questions.

  1. Which of the following is legal JSP syntax to print the value of i. Select the one correct answer

A. <%int i = 1;%>
<%= i; %>

B. <%int i = 1;
i; %>

C. <%int i = 1%>
<%= i %>

D. <%int i = 1;%>
<%= i %>

E. <%int i = 1%>
<%= i; %>

 

2. A JSP page called test.jsp is passed a parameter name in the URL using

http://localhost/test.jsp?name=”John”. The test.jsp contains the following code.

<%! String myName=request.getParameter();%>
<% String test= “welcome” + myName; %>
<%= test%>

  1. The program prints “Welcome John”
  2. The program gives a syntax error because of the statement
    <%! String myName=request.getParameter();%>
  3. The program gives a syntax error because of the statement
    <% String test= “welcome” + myName; %>
  4. The program gives a syntax error because of the statement
    <%= test%>

 

3. Which of the following correctly represents the following JSP statement. Select the one correct answer.
<%=x%>

 

A. <jsp:expression=x/>

B. <jsp:expression>x</jsp:expression>

C. <jsp:statement>x</jsp:statement>

D. <jsp:declaration>x</jsp:declaration>

E. <jsp:scriptlet>x</jsp:scriptlet>

4. Which of the following correctly represents the following JSP statement. Select the one correct answer.
<%x=1;%>

  1. <jsp:expression x=1;/>
  2. <jsp:expression>x=1;</jsp:expression>
  3. <jsp:statement>x=1;</jsp:statement>
  4. <jsp:declaration>x=1;</jsp:declaration>
  5. <jsp:scriptlet>x=1;</jsp:scriptlet>

5. What gets printed when the following JSP code is invoked in a browser. Select the one correct answer.

<%= if(Math.random() < 0.5) %>
hello
<%= } else { %>
hi
<%= } %>
  1. The browser will print either hello or hi based upon the return value of random.
  2. The string hello will always get printed.
  3. The string hi will always get printed.
  4. The JSP file will not compile.

6. Which of the following are correct. Select the one correct answer.

  1. JSP scriptlets and declarations result in code that is inserted inside the _jspService method.
  2. The JSP statement <%! int x; %> is equivalent to the statement <jsp:scriptlet>int x;</jsp:scriptlet%>.
  3. The following are some of the predefined variables that maybe used in JSP expression – httpSession, context.
  4. To use the character %> inside a scriptlet, you may use %> instead.

 

7. What gets printed when the following is compiled. Select the one correct answer.

<% int y = 0; %>
<% int z = 0; %><% for(int x=0;x<3;x++) { %>
<% z++;++y;%>
<% }%><% if(z<y) {%>
<%= z%>
<% } else {%>
<%= z – 1%>
<% }%>
  1. 0
  2. 1
  3. 2
  4. 3
  5. The program generates compilation error.

8. Which of the following JSP variables are not available within a JSP expression. Select the one correct answer.

  1. out
  2. session
  3. request
  4. response
  5. httpsession
  6. page

9. A bean with a property color is loaded using the following statement

<jsp:usebean id=”fruit” class=”Fruit”/>

      Which of the following statements may be used to print the value of color property of the bean. Select the one correct answer.

 

  1. <jsp:getColor bean=”fruit”/>
  2. <jsp:getProperty id=”fruit” property=”color”/>
  3. <jsp:getProperty bean=”fruit” property=”color”/>
  4. <jsp:getProperty name=”fruit” property=”color”/>
  5. <jsp:getProperty class=”Fruit” property=”color”/>

10. A bean with a property color is loaded using the following statement

<jsp:usebean id=”fruit” class=”Fruit”/>

      Which of the following statements may be used to set the of color property of the bean. Select the one correct answer.
  1. <jsp:setColor id=”fruit” property=”color” value=”white”/>
  2. <jsp:setColor name=”fruit” property=”color” value=”white”/>
  3. <jsp:setValue name=”fruit” property=”color” value=”white”/>
  4. <jsp:setProperty name=”fruit” property=”color” value=”white”>
  5. <jsp:setProperty name=”fruit” property=”color” value=”white”/>
  6. <jsp:setProperty id=”fruit” property=”color” value=”white”>

 

11. A bean with a property color is loaded using the following statement

<jsp:usebean id=”fruit” class=”Fruit”/>

      What happens when the following statement is executed. Select the one correct answer.

<jsp:setProperty name=”fruit” property=”*”/>

  1. This is incorrect syntax of <jsp:setProperty/> and will generate a compilation error. Either value or param must be defined.
  2. All the properties of the fruit bean are initialized to a value of null.
  3. All the properties of the fruit bean are assigned the values of input parameters of the JSP page that have the same name.
  4. All the properties of the fruit bean are initialized to a value of *.

12. Is the following statement true or false. If the isThreadSafe attribute of the page directive is false, then the generated servlet implements the SingleThreadModel interface.

13. Which of the following represents a correct syntax for usebean. Select the two correct answers.

  1. <jsp:usebean id=”fruit scope =”page”/>
  2. <jsp:usebean id=”fruit type =”String”/>
  3. <jsp:usebean id=”fruit type =”String” beanName=”Fruit”/>
  4. <jsp:usebean id=”fruit class=”Fruit” beanName=”Fruit”/>

14. Name the default value of the scope atribute of <jsp:usebean>.

  1. page
  2. application
  3. session
  4. request

15. Which of the following statements are true for <jsp:usebean>. Select the two correct answers.

  1. The id attribute must be defined for <jsp:usebean>.
  2. The scope attribute must be defined for <jsp:usebean>.
  3. The class attribute must be defined for <jsp:usebean>.
  4. The <jsp:usebean> must include either type or class attribute or both.

16. Which of these are legal attributes of page directive. Select the two correct answers.

  1. include
  2. scope
  3. errorPage
  4. session
  5. debug

17. Which of the following represents the XML equivalent of this statement <%@ include file=”a.jsp”%> . Select the one correct statement

  1. <jsp:include file=”a.jsp”/>
  2. <jsp:include page=”a.jsp”/>
  3. <jsp:directive.include file=”a.jsp”/>
  4. There is no XML equivalent of include directive.

18. Assume that you need to write a JSP page that adds numbers from one to ten, and then print the output.

<% int sum = 0;
for(j = 0; j < 10; j++) { %>
// XXX — Add j to sum
<% } %>
// YYY — Display ths sum

Which statement when placed at the location XXX can be used to compute the sum. Select the one correct statement

  1. <% sum = sum + j %>
  2. <% sum = sum + j; %>
  3. <%= sum = sum + j %>
  4. <%= sum = sum + j; %>

 

19. Now consider the same JSP example as last question. What must be added at the location YYY to print the sum of ten numbers. Select the one correct statement

  1. <% sum %>
  2. <% sum; %>
  3. <%= sum %>
  4. <%= sum; %>

20. JSP pages have access to implicit objects that are exposed automatically. One such object that is available is request. The request object is an instance of which class?

 

  1. HttpRequest
  2. ServletRequest
  3. Request
  4. HttpServletRequest

 

21. JSP pages have access to implicit objects that are exposed automatically. Name the implicit object that is of type HttpSession.

 

  1. session
  2. application
  3. httpSession
  4. httpsession

22. A Java bean with a property color is loaded using the following statement

<jsp:usebean id=”fruit” class=”Fruit”/>

      What is the effect of the following statement.

<jsp:setproperty name=”fruit” property=”color”/>

      Select the one correct answer.

 

  1. An error gets generated because the value attribute of setAttribute is not defined.
  2. The color attribute is assigned a value null.
  3. The color attribute is assigned a value “”.
  4. If there is a non-null request parameter with name color, then its value gets assigned to color property of Java Bean fruit.

 

23. The page directive is used to convey information about the page to JSP container. Which of these are legal syntax of page directive. Select the two correct statement

  1. <% page info=”test page” %>
  2. <%@ page info=”test page” session=”false”%>
  3. <%@ page session=”true” %>
  4. <%@ page isErrorPage=”errorPage.jsp” %>
  5. <%@ page isThreadSafe=true %>

24. Is the following JSP code legal? Select the one correct statement.
<%@page info=”test page” session=”false”%>
<%@page session=”false”%>

  1. Yes. This is legal JSP syntax.
  2. No. This code will generate syntax errors.

25. A JSP page needs to generate an XML file. Which attribute of page directive may be used to specify that the JSP page is generating an XML file.

  1. contentType
  2. generateXML
  3. type
  4. outputXML

26. A JSP page uses the java.util.ArrayList class many times. Instead of referring the class by its complete package name each time, we want to just use ArrayList. Which attribute of page directive must be specified to achieve this. Select the one correct answer.

  1. extends
  2. import
  3. include
  4. package
  5. classpath

27. Which of these are true. Select the two correct answers.

  1. The default value of isThreadSafe attribute of page directive is true.
  2. If isThreadSafe attribute of page directive is set to true, then JSP container dispatches request for the page sequentially.
  3. When isThreadSafe attribute of page directive is set to true, a thread is created for each request for the page.
  4. Setting isThreadSage attribute to true for JSP pages, can lead to poor performance.

28. Which of the following are examples of JSP directive. Select the two correct answers.

  1. include
  2. exclude
  3. import
  4. taglibrary
  5. servlet
  6. page

29. Which of these is true about include directive. Select the one correct answer.

  1. The included file must have jspf extension.
  2. The XML syntax of include directive in <jsp:include file=”fileName”/> .
  3. The content of file included using include directive, cannot refer to variables local to the original page.
  4. When using the include directive, the JSP container treats the file to be included as if it was part of the original file.

30. Name the implicit variable available to JSP pages that may be used to access all the other implicit objects.

  1. page
  2. pageContext
  3. context
  4. object
  5. jspPave

Answers to questions on JSP

  1. d. When using scriptlets (that is code included within <% %>), the included code must have legal Java syntax. So the first statement must end with a semi-colon. The second statement on the other hand is a JSP expression. So it must not end with a semi colon.
  2. b. JSP declarations do not have access to automatically defined variables like request, response etc.
  3. b. The XML syntax for JSP expression is <jsp:expression>Java expression</jsp:expression>
  4. e. The XML syntax for JSP scriptlets is <jsp:scriptlet>Java code</jsp:scriptlet>
  5. d. The if statement, else statement and closing parenthesis are JSP scriptlets and not JSP expressions. So these should be included within <% } %>
  6. d. JSP declarations are inserted outside of _jspService method. Hence a is incorrect. The JSP statement <%!int a;%> is equivalent to <jsp:declaration>int x;</jsp:declaration>. Hence b is incorrect. The predefined variables that are available within the JSP expression are session and pageContext, and not httpSession and context. Hence c is incorrect.
  7. c. After the for loop z and y are both set to 3. The else satement gets evaluated, and 2 gets printed in the browser.
  8. e. There is no such variable as httpsession.
  9. jsp:getProperty takes two attributes – name and property. The name attribute must match the id attribute of jsp:usebean.
  10. e. The jsp:setProperty takes three attributes – name, property and value. Also the jsp:setProperty must end with />.
  11. c. Using * for property is legal syntax. Bean properties are associated with identically named input parameters.
  12. true. The page directive is defined as <%@page isThreadSafe=”false”%>
  13. b,c.
  14. a. The default scope of the declared bean is page.
  15. a,d. The scope and class attributes are not required. But either class or type must be defined.
  16. c,d. The following are legal attributes of page directive – import, isThreadSafe, session, contentType, autoFlush, extends, info, errorPage, isErrorPage, language.
  17. c. <jsp:directive.include> is the XML equivalent of include directive.
  18. b. As this is a a Java statement it needs to be included with <% and %>, and it needs to end in semi-colon.
  19. c. As this is a a Java expression it needs to be included with <%= and %>, and it should not end in semi-colon.
  20. d. request is an instance of HttpServletRequest
  21. a. Implicit object session is of type HttpSession.
  22. d. This is a legal syntax to set a property of JavaBean. The value attribute of setProperty is optional.
  23. b,c. The option a is incorrect because page directive must be included within <%@ . d is incorrect because the value of isErrorPage attribute must be boolean. e is incorrect because, the value true is not within quotes.
  24. b. Except the import attribute of page directive, all the other attributes of page directive cannot be specified more than once. In this example session attribute is specified twice.
  25. a. contentType attribute is used to generate XML. The syntax will look like –
    <%@page contentType=”text/xml”/>
  26. B. The syntax will look like –
    <%@page import=”java.util.ArrayList”/>
  27. a,c. The default value of isThreadSafe attribute is true. So a is correct. If isThreadSafe is set to false, then JSP container processes request sequentially, and this leads to poor performance. Hence b and d are incorrect.
  28. a,f. include, taglib and page are examples of JSP directives. The JSP directives have this syntax –
    <%@directive attribute=”value” %>
  29. d. It is not required that the included file has jspf extension. Hence a is incorrect. The XML syntax of include directive is <jsp:directive.include>
  30. b. This pageContext object is an instance of type javax.servlet.jsp.PageContext, and provides methods like getPage(), getRequest(), etc. to access other input variables.

1. Online Compilers - Compile & Run your C/C++ programs:

✅ Online Compilers

2. Ask your questions or clarify your/others doubts from,

✅ Online Discussion Forums by Fresh2Refresh.com





Share this website !!!
Facebook Twitter Linkedin

Post Your Thoughts

Close
We Support JALLIKATTU !!!
We Support JALLIKATTU!
By Fresh2Refresh.com
Show Your Support as well. Thanks!