
-----------------------------------
snowball
Sat Jun 14, 2008 8:10 pm

Please help with the question, many thanks!
-----------------------------------
What is the output of the following Java code fragment? 

PrintStream output = System.out;
for(int i = 13; i > 0; i=i%5 ){
if(i++ == 4){
output.printf("first=%d, second=%d\n", i-5, i*i );
} else {
output.printf("first=%d, second=%d\n", i-2, 7*i );
}
}

-----------------------------------
Tony
Sat Jun 14, 2008 8:15 pm

RE:Please help with the question, many thanks!
-----------------------------------
Have you considered simply compiling and executing the program?

Though it looks like an exercise in traversing the code on your own...

-----------------------------------
snowball
Sat Jun 14, 2008 8:37 pm

RE:Please help with the question, many thanks!
-----------------------------------
It's a question in the exam. I know the answer is 
first=12, second=98
first=0, second=25. 
But I don't understand why.

-----------------------------------
Tony
Sat Jun 14, 2008 9:08 pm

RE:Please help with the question, many thanks!
-----------------------------------
Because when you run the code on a computer, that's what it would output. You just have to do the same in your head (or on paper). There's only one variable to keep track of -- i, so it should be simple.

You need to be able to read code to write code.

for(int i = 13; i > 0; i=i%5 ){ 
i = 13

if(i++ == 4){ 
false. i is now 14

output.printf("first=%d, second=%d\n", i-2, 7*i ); 
prints the first line. 14-2 = 12. 7*14=98

for(int i = 13; i > 0; i=i%5 ){ 
i=i%5; 14%5 = 4

if(i++ == 4){ 
true. i is now 4+1 = 5

...

-----------------------------------
snowball
Sat Jun 14, 2008 9:27 pm

RE:Please help with the question, many thanks!
-----------------------------------
Thans a lot.

-----------------------------------
snowball
Sat Jun 14, 2008 9:28 pm

RE:Please help with the question, many thanks!
-----------------------------------
another question. Thanks!

What is the output of the following Java code fragment?

int k, y = 123;

for(k = 0; k > 0; k++){

y = y++ / 10;

}

System.out.println(k + " " + y);

-----------------------------------
Tony
Sat Jun 14, 2008 9:32 pm

RE:Please help with the question, many thanks!
-----------------------------------
it's an infinite loop.

-----------------------------------
snowball
Sat Jun 14, 2008 9:37 pm

RE:Please help with the question, many thanks!
-----------------------------------
The answer is:

0 123

Why?

-----------------------------------
Reality Check
Sat Jun 14, 2008 9:42 pm

Re: Please help with the question, many thanks!
-----------------------------------
It is not an infinite loop tony.  It says k and y = 123.  It reaches the for statement and makes k = 0 and the loop condition is IF k > 0 and not k >= 0.  Therefore it never goes into the for loop, equates k to 0 while y retains it's 123 value.  This is pretty simply stuff though, what grade is this?

-----------------------------------
Tony
Sat Jun 14, 2008 9:46 pm

RE:Please help with the question, many thanks!
-----------------------------------
Oh yeah. Ops. I skipped over k = 0 part. (busy with calculus... midterms).

0 123 is correct then.

-----------------------------------
Zeroth
Sun Jun 15, 2008 5:00 pm

Re: Please help with the question, many thanks!
-----------------------------------
How about we stop doing people's homework for them? I dunno, personally, it doesn't help anyone to say what a piece of code, does, unless you take them step by step with it. Just my view.

-----------------------------------
syntax_error
Sun Jun 15, 2008 5:06 pm

Re: Please help with the question, many thanks!
-----------------------------------
How about we stop doing people's homework for them? I dunno, personally, it doesn't help anyone to say what a piece of code, does, unless you take them step by step with it. Just my view.

look 
     \
      |
     \/

Because when you run the code on a computer, that's what it would output. You just have to do the same in your head (or on paper). There's only one variable to keep track of -- i, so it should be simple.

You need to be able to read code to write code.

for(int i = 13; i > 0; i=i%5 ){ 
i = 13

if(i++ == 4){ 
false. i is now 14

output.printf("first=%d, second=%d\n", i-2, 7*i ); 
prints the first line. 14-2 = 12. 7*14=98

for(int i = 13; i > 0; i=i%5 ){ 
i=i%5; 14%5 = 4

if(i++ == 4){ 
true. i is now 4+1 = 5

...


-----------------------------------
Dragan
Sun Jun 15, 2008 5:59 pm

RE:Please help with the question, many thanks!
-----------------------------------
@snowball
You need firist to learn if and for than you can start with reading and writing code. If you don't know how to use if and for we can't help you.
