• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Problem in mock exam

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i thought this tricky stuff was reserved for SCJP
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic