This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Pg. 420 - Q.6 of HF book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Pg. 420 - Q.6 of HF book" Watch "Pg. 420 - Q.6 of HF book" New topic

Pg. 420 - Q.6 of HF book

Jayanthi Mani

Joined: Dec 08, 2000
Posts: 25
Given this question:
11. <% java.util.List list=new java.util.ArrayList();
12. list.add("a");
13. list.add("2");
14. list.add("c");
15. request.setAttribute("list",list);
16. request.setAttribute("listIdx","1");
17. %>
18. <%-- insert code here --%>

Which inserted at line 18 are valid and evaluate to c? (Choose all that apply)


The answers are given as B,D,F.

Why is 'E' also not considered a correct answer? I don't understand the explanation that has been given. Can anybody please explain why option 'E' is not an answer?

Roger Yates
Ranch Hand

Joined: Aug 31, 2004
Posts: 118
'listIdx' with quotes round it is just the string 'listIdx'.
We can assume that list is of type List and so can be accessed using [] with numbers only. So list[1] is fine, and list[someAttributeThatCanConvertToANumber] is also fine. But list["someAttributeThatCanConvertToANumberButWontAsItIsInQuotes"] will not work.

Actually as it stands F also does not work (exception gets thrown for same reason as E).
Here's the error you get for F as it is now:
The EL expression ${list[list['listIdx']]} throws a ServletException
(The "[]" operator was supplied with an index value of type "java.lang.String" to be
applied to a List or array, but that value cannot be converted to an integer.)

I've logged 'F' as an errata, and it's currently in the 'unconfirmed' bucket here:

Cheers,<br />Roger<br />SCJP 1.2, 1.4, SCBCD 1.3, SCWCD 1.4
Jayanthi Mani

Joined: Dec 08, 2000
Posts: 25
Thanks for the explanation.That does clarify my doubt quite nicely

Jessica White
Ranch Hand

Joined: Sep 24, 2004
Posts: 30
Roger, I so agree with you.
I had quite a difficult time to accept F as a correct answer.

F. ${list[list['listIdx']]}

'listIdx' would be treated as a String, since it has quotes around it, thus it should not be evaluted. At runtime, EL will try to coerce String 'listIdx' to an int, but of course that would not succeed, unless it is a string like '32'. So, I agree with you that F is not a correct answer.

I agree. Here's the link:
subject: Pg. 420 - Q.6 of HF book
It's not a secret anymore!