• 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic