• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HFSJ mock exam 2 Question -Standard Actions

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Which of the following lines of code, if inserted independently at Line 5, will cause the text within the c therwise tag to display? (Choose all that apply)

A. books.add("");
B. books.add(null);
C. books.clear();
D. books.add("Head First");
E. books = null;

Answer please.
[ August 07, 2008: Message edited by: deepa raj ]
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi deepa raj,
somethig is missing in the question.

Add mos more clarity to the question if you haveinfo.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,what is your question?
 
deepa raj
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check it now. :-)
 
Amruth Puppala
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ans: E. books = null
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my take is also E option
I choose so because not Empty check if the variable itself is null or not

correct if I am wrong
 
deepa raj
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what about books.clear() ?
 
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when you try each option in your code? That is the best way to know for sure what is correct.
 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A. books.add("");

This answer is wrong. As books is a arraylist. Even if we add blank String to it. Its size will be 1. Because it will be like this books[0]="".

B. books.add(null);

This answer is wrong. As books is a arraylist. Even if we add null to it. Its size will be 1. Because it will be like this books[0]=null.

C. books.clear();

This is the right answer because it will clear the arraylist which will eventually make it empty.


D. books.add("Head First");

This answer is wrong. As books is a arraylist. Even if we add String to it. Its size will be 1. Because it will be like this books[0]=Head First.

E. books = null;

This is wrong because here we are making the object book itself as null and adding as a null object as an attribute.

So trying to use the empty operator with it will cause FatalException.
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you - very nice explanation. I am suprised that the last option with the null did not work. I found this on the sun site regarding the empty operator and the EL

The language offers the following operators (parentheses here are used only for grouping):

binary logical operators
(<, <=, ==, >=, >, lt, le, eq, ge, gt)


binary arithmetic operators
(+, -, *, /, div, %, mod)


indexing operators
(.) and ([])


(equivalent) unary negation operators
(!, not)


arithmetic unary minus
(-)


(empty) operator, which evaluates to true if the expression evaluates to null, or evaluates to a container object that contains no items.

ternary operator for simple if-then-else
(?
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
option E is correct. I tried it and it jumps to the otherwise condition. 'empty' works for empty lists and also if the object as such evaluates to null, as Micheal said.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I checked with JSP2.0 Specification (Page: 1-73 Point: JSP.2.3.7). This is what I got from it.


JSP.2.3.7 Empty Operator - empty A
The empty operator is a prefix operator that can be used to determine if a value is null or empty.
To evaluate empty A,
� If A is null, return true,
� Otherwise, if A is the empty string, then return true.
� Otherwise, if A is an empty array, then return true.
� Otherwise, if A is an empty Map, return true,
� Otherwise, if A is an empty Collection, return true,
� Otherwise return false.



I also ran the example provided.
For answers C and E, I got output as "I have not selected any favorite books."
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That is the best way to know for sure what is correct.


No. Containers sometimes have their own way of doing things.
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks - I did not realize that containers can implement different behavior for the EL 'empty' operator. I thought they were allowed to implement any way they choose but be required to meet the spec.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I thought they were allowed to implement any way they choose but be required to meet the spec.


They do, but sometimes don't. Of course, they won't blunder such simple things like 'empty'. I didn't encounter much non-spec friendly behavior in SCWCD, but more in SCBCD. So trying something with a container is good, but does not guarantee 100% correctness.
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Understood. I just had noticed that some posts were giving incorrect answers/information and thought that it would be better to have these posters discover the error for themselves. I tried to give a gentle nudge in the right direction :-).
 
The knights of nee want a shrubbery. And a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic