• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

SYBEX OCA/OCP Java SE 8 Programmer Practice Tests Chapter5 Question#9 Errata?

 
Ranch Hand
Posts: 299
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a for-each loop, since there is no such loop iteration variable that programmers can print to tell which iteration is currently going on (despite the later compiled conversion), I answered it accordingly.  

1)The errata does not mention this number.  Is this answer (really) correct?
2)If so, A)Isn't the whole point of the for-each to rid non-business rule "plumbing"? B)If I saw something really similar on the exam, how should it be answered?
PracticeTest_Ch5_Q9.png
[Thumbnail for PracticeTest_Ch5_Q9.png]
 
author
Posts: 4172
29
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, I appreciate your interested and feedback in this topic.  We always appreciate feedback in our books!

For the exam, you need to know that a for-each loop (also known as an enhanced for loop) iterates in sequential order.  For this reason, I is correct and II is incorrect, making A the correct answer.  The question doesn't state that there is a loop iteration variable (aka a counter) in a for-each loop.  It simply states that if you iterating over an array, then it starts with the value stored in index 0, so it is correct as is.

A good portion of both OCA8 and OCP8 exams is knowing the "plumbing" of Java.  For example, you need to know when/if the compiler will add default constructors to classes, or implicit modifiers to interface members.  Knowing this helps answer questions where invalid or incompatible calls/modifiers are present.
 
Charles O'Leary
Ranch Hand
Posts: 299
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Scott Selikoff wrote:First off, I appreciate your interested and feedback in this topic.  We always appreciate feedback in our books!

We appreciate you and Jeanne more!

Scott Selikoff wrote:For the exam, you need to know that a for-each loop (also known as an enhanced for loop) iterates in sequential order.  For this reason, I is correct and II is incorrect, making A the correct answer.  The question doesn't state that there is a loop iteration variable (aka a counter) in a for-each loop.  It simply states that if you iterating over an array, then it starts with the value stored in index 0, so it is correct as is.

Loop iteration variable (aka a counter) versus indexing ... would that be considered splitting hairs?  Here, I have to admit, I thought that you were throwing me a trick question.  My broader concern was if I would be able to process "these types" of questions on the exam.   At least in my mind, maybe using the words "sequential order" as you have just explained, rather than "index" would possibly better disambiguate this question... just a thought.

Thanks Scott for the excellent book and support you constantly provide!  I'm looking forward to OCP Oracle Certified Professional Java SE 11 Programmer II Study Guide: Exam 1Z0-816 2nd Edition.    
 
author & internet detective
Posts: 39675
785
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The practice questions book has more "English type" questions than the real exam. We did that so you'd have a solid handle on all the concepts. This one wasn't meant to be a trick question! Sorry if it turned out to be one.

You do understand the point of that question though - for-each can only go forward, one at a time, in order.
 
Charles O'Leary
Ranch Hand
Posts: 299
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:The practice questions book has more "English type" questions than the real exam. We did that so you'd have a solid handle on all the concepts. This one wasn't meant to be a trick question! Sorry if it turned out to be one.

Well one of my most favorite quotes throughout your book, OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z0-808, is all the forms of "try to trick you" ... so you could say my antennas are always up and its been very helpful.

Jeanne Boyarsky wrote:You do understand the point of that question though - for-each can only go forward, one at a time, in order.

 
Marshal
Posts: 66637
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's see what the JLS (=Java® Language Specification) has to say about for‑each loops, apart from always calling the enhanced for loops:-

That JLS section wrote:If the type of Expression is a subtype of Iterable

   ...

Otherwise, the Expression necessarily has an array type, T[].

Let L1 ... Lm be the (possibly empty) sequence of labels immediately preceding the enhanced for statement.

The enhanced for statement is equivalent to a basic for statement of the form:

T[] #a = Expression;
L1: L2: ... Lm:
for (int #i = 0; #i < #a.length; #i++) {
   {VariableModifier} TargetType Identifier = #a[#i];
   Statement
}

. . .

So there does appear to be an index, only it is hidden from the user, and is here hidden behind #i. So Q1 is correct.
 
Charles O'Leary
Ranch Hand
Posts: 299
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Let's see what the JLS (=Java® Language Specification) has to say about for‑each loops, apart from always calling the enhanced for loops:-

That JLS section wrote:If the type of Expression is a subtype of Iterable

   ...

Otherwise, the Expression necessarily has an array type, T[].

Let L1 ... Lm be the (possibly empty) sequence of labels immediately preceding the enhanced for statement.

The enhanced for statement is equivalent to a basic for statement of the form:

T[] #a = Expression;
L1: L2: ... Lm:
for (int #i = 0; #i < #a.length; #i++) {
   {VariableModifier} TargetType Identifier = #a[#i];
   Statement
}

. . .

So there does appear to be an index, only it is hidden from the user, and is here hidden behind #i. So Q1 is correct.



Campbell,

Thanks for helping me to make my point.  #i is a loop iteration variable (aka a counter) that Scott mentioned that the question doesn't state.  Also, let's see what the Java Tutorials has to say about for‑each loops:

...The iterator is just clutter. Furthermore, it is an opportunity for error. The iterator variable occurs three times in each loop: that is two chances to get it wrong. The for-each construct gets rid of the clutter and the opportunity for error.
...
So when should you use the for-each loop? Any time you can. It really beautifies your code. Unfortunately, you cannot use it everywhere. Consider, for example, the expurgate method. The program needs access to the iterator in order to remove the current element. The for-each loop hides the iterator, so you cannot call remove. Therefore, the for-each loop is not usable for filtering. Similarly it is not usable for loops where you need to replace elements in a list or array as you traverse it. Finally, it is not usable for loops that must iterate over multiple collections in parallel. These shortcomings were known by the designers, who made a conscious decision to go with a clean, simple construct that would cover the great majority of cases.



How can the clutter both be rid of, yet the clutter is also present, at the same time?  Furthermore, my broader point is, how would I need to answer this question if it were on the actual exam:

Which of the following statements contain an index?
A. The for Statement
B. The For-Each Loop
C. None
D. Both A and B
 
Campbell Ritchie
Marshal
Posts: 66637
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back to the JLS (same section as I linked earlier) where we find that the for‑each loop for an Iterable is equivalent to this sort of thing:-

The enhanced for statement is equivalent to a basic for statement of the form:

for (I #i = Expression.iterator(); #i.hasNext(); ) {
   {VariableModifier} TargetType Identifier =
       (TargetType) #i.next();
   Statement
}

That ain't got any indices, so asking, “Does a for‑each/enhanced for loop contain an index?” can only get the answer, “It depends.” They should never ask that sort of question because it is ambiguous, and it is also different from the original question, so I have digressed from the proper theme of this discussion (sorry). Remember that the Oracle exams will almost certainly always say, “enhanced for loop,” rather than, “for‑each loop.”
 
Charles O'Leary
Ranch Hand
Posts: 299
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That ain't got any indices, so asking, “Does a for‑each/enhanced for loop contain an index?” can only get the answer, “It depends.” They should never ask that sort of question because it is ambiguous...


Thanks Campbell.
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!