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

Arrays doubt  RSS feed

 
a alph
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This question is taken from Inquisition.



Choose one option:
A) Compile error
B) Prints 0 0 0 0
C) Prints 0 1 2 3
D) Prints 0 2 0 0
E) Prints 0 0 3 0
F) Runtime exception occurs.

The answer is option E.

I thought the answer was option B. Can anyone please explain this to me.

Thanks.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a alph wrote:
The answer is option E.

I thought the answer was option B. Can anyone please explain this to me.


This question come up many times on the Ranch -- just do a search. Short answer -- even though i is used as an index, it isn't set to the indexes of the array by the loop.

Henry
 
a alph
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry.
I did a search and found the answer.
Thanks for your reply.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!