• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in mock exam

 
Gurukant Desai
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I found this problem in java beat's mock exam no.6
--------------------------------------------------------------------------
22. Which statement is TRUE about the following jsp code snippet?
Please select one correct answer.
<%
String theKey = "key";
String theValue = "value";
session.removeAttribute(theKey); //1
%>

session.setAttribute("<%= theKey %>",
"<%= theValue %>"); //2

session.getAttribute("<%= theKey %>"); //3

<%= session.getAttribute(theKey) %> //4


A. The code compiles but might have runtime NullPointerException at //1
B. There will have compilation error at //2 and //3.
C. There will have output as null at //4.
D. There will have output as theValue at //4
--------------------------------------------------------------------------
Correct answer is C but I think answer B is right.
Reason :- I think answer B is right because at the time of code conversion It will evaluate line no.2 as out.print("session.setAttribute("theKey","theValue");");
Which will result into compilation error...

Can someone please reply whether I am right/wrong?
 
Musab Al-Rawi
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lines 2 & 3 are text templates, they will be printed as they are to the response.
theKey is not added to the session and so printing is will cause null to be printed.
 
Abi Ted
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lines 2 and 3 access the attribute as defined by the string. The enclosing string is perfect attribute variable name.

Oh! i get it now. You read the attribute as scriplet!!! be very careful!!
 
Ali Khalfan
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i thought this tricky stuff was reserved for SCJP
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic