• 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Pg. 420 - Q.6 of HF book  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

A.${list.2}
B.${list[2]}
C.${list.listIdx+1}
D.{list[listIdx+1]}
E.{list['listIdx'+1]}
F.{list[list['listIdx']]}

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?

Thanks,
Jayanthi.
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'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:
http://www.oreilly.com/catalog/headservletsjsp/errata/
 
Jayanthi Mani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation.That does clarify my doubt quite nicely

Jayanthi.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

--Jessic
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!