What is output to the web page on the second access to the same instance of the following JSP? (Choose one.) <%@ page language="java" %> Chapter 6 Question 2
Chapter 6 Question 2
<%! int x = 0; %> <%! public void jspInit() { System.out.println(x++); } %> <%= x++ %> <% System.out.println(x); %> <% jspInit(); %>
-
0
-
1
-
2
-
3
-
4
-
Page does not translate.
D
Correct answer
Explanation
This JSP declares an instance variable x initialized to 0. On first access: x starts at 0, x++ prints 0 then becomes 1, jspInit() prints x++ (1) then x becomes 2. System.out.println(x) outputs 2. End of first request: x=2. On second access: x starts at 2, x++ prints 2 then becomes 3. jspInit() prints x++ (3) then x becomes 4. System.out.println outputs 4. The expression <%= x++ %> outputs 3 (current value before increment). So second access shows 3 in the web page.