Sunday, August 2, 2009

What output is produced by the following code fragment?

What output is produced by the following code fragment?


for (int nbr=200; nbr%26gt;=0; nbr-=1)


if(nbr%4 != 0)


System.out.println(nbr);





Choose one answer.


a. This code prints out nothing because of a syntax error.


b. This code prints out all the integers between 200 and 0, not including 200 or 0.


c. This code prints out all the integers from 200 to 0, inclusive.


d. This code prints out all the multiples of 4 between 199 to 0.


e. This code prints out all the numbers from 199 to 0, inclusive, skipping all multiples of 4.

What output is produced by the following code fragment?
Seeing the System.out.println(nbr) leads me to believe this is Java.





The loop runs once for each value of nbr from 200 down to 0, inclusive, so it runs 201 times.





So nbr starts at 200. We check if the number is divisible by 4 (that is, if the remainder when divided by 4 is zero). If it is not divisible by 4, then we print the number.





Since 200 is divisible by 4, we don't print it. Same for zero. Same for every fourth number.





Let's consider the options.





a.) This code contains no errors, so this is not the answer.


b.) This code does not print, for example, 4, so this is not the answer.


c.) This code does not print 200 or zero, so this is not the answer.


d.) This code does not print out any multiples of 4, so this is not the answer.


e.) This is the one. Note that it does not print zero (since the remainder when divide by 4 is zero).





Hope this helps!
Reply:Depending on the language, it could be a also.
Reply:e, but do you should try and do your homework yourself.


No comments:

Post a Comment