aspose file tools*
The moose likes Beginning Java and the fly likes Code segment question #1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Code segment question #1" Watch "Code segment question #1" New topic
Author

Code segment question #1

Daniel Lucas
Ranch Hand

Joined: Apr 24, 2006
Posts: 48
OK this is another multiple choice collegeboard question (question number 1, I have already asked question #3 on this forum... I skipped around thinking I knew #1 and #2, but I'm going over #1 for the heck of it) that I'm answering and want to check. I find by typing out my thoughts I understand things a lot better.

Consider the following code segment.

for (int k = 0; k < 20; k = k + 2)
{
if (k % 3 == 1)
System.out.print(k + " ");
}

What is printed as a result of executing the code segment?

(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

I worked out the problem below without knowing the answer, but for your purposes the answer is B.

My analysis:

So we have a for loop where k is an integer equal to 0, where k is less than 20, and where k is defined as equal to k + 2. Next we have an if statement. If k modulus 3(the remainder when k is divided by 3) is equal to 1(so when the remainder is equal to 1, and I'll be looking at 3/3 = 1 in this case... I think), then the result printed out will be just k, since " " is just blank space. (NOTE* after answering the problem, let me restate this: If k modulus 3(the remainder left after division when k is divided by 3. So for instance 4/3 has a remainder left of 1.) is equal to 1, then the result printed out will be the new value of k after the initial value is plugged into the equation k = k + 2.

Alright, so I'm going to plug k, which equals 0, into the equation k = k + 2. That makes it 0 = 0 + 2, so k now equals 2. When k is divided by 3 (2 / 3), the answer is 2/3 and there is no remainder(if I got 4/3 then the remainder would be 1/3(NOTE* actually the remainder would be 1). So nothing will print when k = 0.

Since this is a for statement, I know that k is going to equal all the values from 0 up until 19. Thus:

When k = 1, k = 3. So 3 / 3 = 0. No remainder, nothing prints out.
When k = 2, k = 4. So 4 / 3 = 1 with a remainder of 1/3(remainder of 1, not 1/3, so k = 4 is printed out). The remainder is not equal to 1, so nothing will print out.
When k = 3, k = 5. So 5 / 3 = 1 with a remainder of 2/3(remainder of 2, not 1, so nothing prints out). Ditto as above. I'm thinking ahead by now that following this pattern the next value of k(6. 6 / 3 = 1 with a remainder of 1 will have a remainder of 1 and satisfy the condition, thus printing out a 4. I can now eliminate choices C, D, and E since they all start with a number other than 4. Well I'm lazy and don't have a whole lot of time now (timed environment on AP test!), so I'm going to skip to the value of k = 10.

When k = 10, k = 12. So 12 / 3 = 4. But it does not have a remainder of 1(remainder of 0, nothing prints out when k = 12. When I plug in 8, k will equal 10, so let's try that number).. Hmmm I seem to be missing something here, because there is only one number that will have a remainder of 1, and that is 4. Well let me go on to 16.

When k = 16, k = 18. So 18 / 3 = 5 and 2/3

MENTAL THOUGHT! I think I have the idea of a remainder wrong because I'm going to get 4 as an answer with this method. If I have 4/3 then the remainder must be 1, not 1/3. Note that I'll be putting parentheses around my initial thought with corrections...

So...........

When k = 8, k = 10. So 10 / 3 = 3 with a remainder of ONE! So B MUST BE TEH ANSWER! (*checks back at answer*) YESSSSSSSSS!!!11

Gosh that took a while.... lol. At least now I know I could eyeball that same question and get the answer in a minute.
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
The best way to learn from these sorts of problems is to trace out the code on paper. Then run it and see if there is any differences.

You are slightly misunderstanding integer division and modulus. For example: When k = 3, k = 5. So 5 / 3 = 1 with a remainder of 2/3. You did right after this, say that remainder is 2, which is correct. With integer division you will never get a fractional answer. So 5/3=1 amd 5%3 = 2(not 2/3). It might seem like nitpicking, but if you get used to thinking in terms of real numbers when manipulating integers you will get bit.

Try this slightly altered piece of code. It is a bit more subtle.


for (int k = 0; k < 20; k = k + 1)
{
if (k % 5 == 1)
System.out.print(k + " ");
}


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Code segment question #1