This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Display Each Element of the Array  RSS feed

 
joel rainey
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,

I'm currently studying up for my OCA7 and ran into this question "Given the blablbalb, which statement will display each element of the Array?" .

To display each element within an array I can use a "For-Loop", and I must start at Zero if my desire is to Display Each Element; however the correct answers initialized "i" to a value of 1 instead of Zero. If the value of "i" starts at 1 and I print each element of the Array, then the value of Zero will not be displayed correct?. The code is below:





I decided to test it out to see if I was right so I constructed the following:



My out put is:
run:
A's
B's
C's
D's
E's

and when I initialize "i" to 1 I get the following output:
run:
B's
C's
D's
E's


 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
joel rainey wrote:however the correct answers initialized "i" to a value of 1 instead of Zero...

In which case, either:
1. They're wrong.
or
2. You haven't supplied us with the exact text that was in the study guide.

And I hate to say, but I suspect that the latter is probably true, since your first TWO "answers" won't compile.

Winston
 
joel rainey
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Below is the exact text for the question, in question. My bad if the first post was confusing. So what do you think, did I just swing/miss?

Given the following declaration of an array, which statement will display
each element of the array?

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
joel rainey wrote:

Below is the exact text for the question, in question. My bad if the first post was confusing. So what do you think, did I just swing/miss?

Given the following declaration of an array, which statement will display
each element of the array?



So the two questions for you now are:

1. What do you think, and why?

2. What happened when you tried each of them?
 
joel rainey
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured that if "i" was initialized to 1 at the start of the "for-loop" then once the for-loop runs it will skip over element Zero and zero would not be printed; because an Array's index starts at Zero not 1.

I did not test the question code, rather I just made my own version to see if it would work. but maybe I should test that code out and report back.
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
joel rainey wrote:I figured that if "i" was initialized to 1 at the start of the "for-loop" then once the for-loop runs it will skip over element Zero and zero would not be printed; because an Array's index starts at Zero not 1.


What you initialize i to does not matter, how you use that i , matters.You have to only remember that array index starts from 0 and goes till array.length-1.
For eg. you can still access the first element of array if i is initialized to 1.
for(int i=1;i<=arr.length;i++)
System.out.println(arr[i-1]);


Best of luck for OCAJP7.I am giving this exam tomorrow, Super excited about it.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
joel rainey wrote:I figured that if "i" was initialized to 1 at the start of the "for-loop" then once the for-loop runs it will skip over element Zero and zero would not be printed; because an Array's index starts at Zero not 1.


Correct. And what do you think will happen at the end of the loop?

I did not test the question code, rather I just made my own version to see if it would work. but maybe I should test that code out and report back.


I'd certainly advise it. Predict what will happen in each of the cases, and then test those predictions. That's a good way to see for yourself how well you understand the topic at hand and identify the bits where your understanding is still shaky.
 
joel rainey
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
joel rainey wrote:I figured that if "i" was initialized to 1 at the start of the "for-loop" then once the for-loop runs it will skip over element Zero and zero would not be printed; because an Array's index starts at Zero not 1.


Correct. And what do you think will happen at the end of the loop?

I did not test the question code, rather I just made my own version to see if it would work. but maybe I should test that code out and report back.


I'd certainly advise it. Predict what will happen in each of the cases, and then test those predictions. That's a good way to see for yourself how well you understand the topic at hand and identify the bits where your understanding is still shaky.



At the end of the loop I figured the value of "i" is 5.
 
joel rainey
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rohan Deshmkh wrote:
joel rainey wrote:I figured that if "i" was initialized to 1 at the start of the "for-loop" then once the for-loop runs it will skip over element Zero and zero would not be printed; because an Array's index starts at Zero not 1.


What you initialize i to does not matter, how you use that i , matters.You have to only remember that array index starts from 0 and goes till array.length-1.
For eg. you can still access the first element of array if i is initialized to 1.
for(int i=1;i<=arr.length;i++)
System.out.println(arr[i-1]);


Best of luck for OCAJP7.I am giving this exam tomorrow, Super excited about it.


Good luck on the exam and thanks for the tip
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
joel rainey wrote:
At the end of the loop I figured the value of "i" is 5.


At the entry to the last pass through the loop body, yes, it will be 5, and if it completes the body normally, it will be incremented to 6, which will cause the condition to evaluate to false, which will transfer control to the next statement after the loop.

My point, though, was what do you think will happen on that last pass when we execute

?
 
joel rainey
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
joel rainey wrote:
At the end of the loop I figured the value of "i" is 5.


At the entry to the last pass through the loop body, yes, it will be 5, and if it completes the body normally, it will be incremented to 6, which will cause the condition to evaluate to false, which will transfer control to the next statement after the loop.

My point, though, was what do you think will happen on that last pass when we execute

?


We should get an exception because index [5] does not exist, so it would be an outOfBounds exception.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
joel rainey wrote:
We should get an exception because index [5] does not exist, so it would be an outOfBounds exception.


Yup!

ArrayIndexOutOfBoundsException, to be precise (I think).
 
joel rainey
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
joel rainey wrote:
We should get an exception because index [5] does not exist, so it would be an outOfBounds exception.


Yup!

ArrayIndexOutOfBoundsException, to be precise (I think).



Awesome. So with the question in mind displaying each element, both choices "c" and "d" are correct even though they won't display element 0 and rather give an exception; Does this mean that an exception would still be valid when displaying all elements within an Array?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
joel rainey wrote:
Awesome. So with the question in mind displaying each element, both choices "c" and "d" are correct even though they won't display element 0


Well, no, they're not correct, precisely because they don't display element 0. The question was, "which statement will display each element of the array?"

Does this mean that an exception would still be valid when displaying all elements within an Array?


It's never valid to get most unchecked exceptions such as ArrayIndexOutOfBoundsException in real code, since most unchecked exceptions by definition indicate a bug in the code.

In this particular case, if C and D went 0..5 instead of 1..5, then whether they're considered "correct answers" would depend on the person administering the test. As worded, I would take them as correct in that case, because they would still meet the exact wording of the question. All elements would get displayed, and the exception wouldn't come until after that. Some teachers might have an implied "... and all code runs correctly without generating any exceptions" clause though.
 
joel rainey
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gotcha, man this question drove me crazy. Thanks for your help.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!