Staredit Network

Staredit Network -> Computers and Technical -> Wooo! Java!
Report, edit, etc...Posted by saibaman8 on 2006-03-12 at 15:35:06
Last Friday in our AP Computer Science class (which is where we're learning Java), my teacher gave us a sample from last year's AP exam. The first question confused almost everyone.

It had a code segment:
CODE
for (int k = 0; k < 20; k = k + 2)
{
 if (k % 3 == 1)
   System.out.print(k + " ");
}

Then we had to determine what would be outputted. Here were the choices:
A) 4 16
B) 4 10 16
C) 0 6 12 18
D) 1 4 7 10 13 16 19
E) 0 2 4 6 8 10 12 14 16 18

Everyone (who wasn't too lazy to work it out) chose D. Which, according to the answer key, was wrong. B was the correct answer, but it had no explanation. This isn't helping me, and it's bringing me closer to giving up on learning Java. Anyone have an explanation?
Report, edit, etc...Posted by Gradius on 2006-03-12 at 16:00:14
for loop says this
0 2 4 6 ...18

then divide by 3 and find the remainder
0 - 0
2 - 2
4 - 1
6 - 0
8 - 2
10 - 1
12 - 0
14 - 2
16 - 1
18 - 0
Report, edit, etc...Posted by CheeZe on 2006-03-12 at 16:01:06
It's B because:

4 % 3 = 1

10 % 3 = 1

16 % 3 = 1


It's not A because it doesn't display enough. (10 is also displayed)

It's not C because 6 % 3 != 1.

It's not D because it has odd numbers in it.

It's not E because 6 % 3 != 1.

(SEN died. tongue.gif)
Report, edit, etc...Posted by saibaman8 on 2006-03-12 at 17:48:49
Oh wait... the loop increases by two...

Damnit.
Next Page (1)