Interview Questions

Considering the code below, which of the lines of code...

BEA WebLogic Questions and Answers


(Continued from previous question...)

Considering the code below, which of the lines of code...

Considering the code below, which of the lines of code (given in the choices) should be placed at line 4 to make this JSP prints "My score is : 100"? Please ignore the line numbers for the purpose of validity of the JSP code.
1:
2:
3:
My Progress Report
4:
5: <% score++; %>
6: <%= "My score is : " + score %>
7:
8:
a. <%! int score = 99; %>
` b. <% int score; %> `
c. <%@ int score = 99; %>
` d. < int score = 99; />


A is the correct choice. The above JSP will work on declaring and initializing the variable score. The syntax for declaring and initializing a variable in JSP is as follows:

<%! variable=variable_value ; %>

Thus A is the correct choice. The <%@ ... %> tag is used to declare directives like include directive. Thus choice C is incorrect. The <% ... %> code is used to insert scriptlets (lines of code in java) like the one at line 5. The code written inside the scriptlet becomes part of the service() method of the generated servlet. Thus 'score' becomes the local variable of the service method. And for this JSP to compile properly, the variable 'score' should have been initialized. If "<% int score; %>" is replaced by "<% int score=99; %>" , the choice B would also be correct. In the present scenario, the choice B will give compilation error saying "Variable score may not have been initialized". Choice D is incorrect as it's not a valid tag.

(Continued on next question...)

Other Interview Questions