• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

JSP Question

 
Ranch Hand
Posts: 53
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm studying SWCD by Charles Lyons (2nd edition) and i'm reviewing some questions about JSP. On page 262, here's the following question:

Which of the following cause the value 'yes' to be written to the response if the state scripting variable is true and 'no' otherwise?

A. <%=
if (state) {
"yes";
} else {
"false";
}
%>

B. <%=state? "yes" : "no"%>
C. <% if (state) %>yes<%else%>no
D. <% if (state) out.write("yes");
else out.write("no");%>
E. <% state? out.write("yes") : out.write("no");%>

Answer is B & D. I do understand the explanation to the answer given. However nothing is mention about why A is incorrect? I don't see why A is incorrect.
Any help is appreciated.
 
Bartender
Posts: 4107
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would better sit on the SCWCD forum as it's about a mock exam question.

In JSPs code inside scriptlet expressions (i.e. <%=%>) become the argument to the print method of the JspWriter hence using the option A wouldn't even compile the JSP. You can't use statements inside the print method.
 
Kim Ming Yap
Ranch Hand
Posts: 53
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. I think A will return either "yes" or "no". I don't see any difference between A and B since both are statements returning strings.
 
Vijitha Kumara
Bartender
Posts: 4107
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think A will return either "yes" or "no". I don't see any difference between A and B since both are statements returning strings.


Code inside the if statement in option A is not a valid Java code. In option B it's an expression which returns either values defined. So the two options are completely different.
 
Ranch Hand
Posts: 2107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kim,
Review the difference between:


and

 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic