View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Sum2000 Midterm Review: Reading Code

When I ran the 2nd of snippet of code it executed forever- surely that can't be right?

Guess it depends on what you mean by "right". Yes, that is what happens. (And don't call me 'Shirley' :-) Now, why? Mark Guzdial

The first snippet of code:

1. The code is concatenating the array of strings so you end up printing out 'abcdefghijkl'.

2. At first, it would seem that the loop would run 4 times, but the last two strings are concatenated in the definition of the array and therefore become one string in the array. So each: will evaluate 3 strings and the loop will run 3 times.

The second snippet of code:

3. The loop will run infinitely. This is because 'test' is never getting its value changed. It was set before the loop by i, and i is changed within the loop, but since i never gets a chance to change test, it will remain the same value forever and the loop will continue to execute.

How would you fix #3? (Hint: It's a two character change.) Mark Guzdial

Link to this Page