• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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
 
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic