# Studying for Finals

• 01-12-2013, 09:50 PM
hockey101
Studying for Finals
Alright guys, I am trying to study for my Computer Programming 1 final on tuesday and I have a question. My question is you can pass an int as a double but you can't pass a double as an int? I want to know if that statement is correct.
• 01-12-2013, 10:05 PM
Fubarable
Re: Studying for Finals
It might be quicker to test this yourself first with your handy Java compiler. Then check out the JLS or Java Language Specification on Widening Primitive Conversion. as well as the next chapter on Narrowing Primitive Conversion.
• 01-12-2013, 10:06 PM
hockey101
Re: Studying for Finals
All I wanted to know if that statement was correct or not
• 01-12-2013, 10:07 PM
hockey101
Re: Studying for Finals
But i think i figured it out on my own...thanks
• 01-12-2013, 10:18 PM
Fubarable
Re: Studying for Finals
Quote:

Originally Posted by hockey101
But i think i figured it out on my own...thanks

I'm of the "teach a man to fish" school. You may wish to bookmark the JLS link as it may come in handy. I know that it's helped me out quite a bit!
• 01-12-2013, 10:27 PM
hockey101
Re: Studying for Finals
Ok now i have another question that I cant seem to solve...

Given the following code, where x=0, what is the resulting value of x after the for-loop terminates?

for (int i=0; i<5; i++)
x += i;
• 01-12-2013, 10:30 PM
Fubarable
Re: Studying for Finals
What happens when you write out what happens on paper? It will take at most 5 lines to do.
• 01-12-2013, 10:41 PM
hockey101
Re: Studying for Finals
Thats what I tried doing......
x i
0 0
0 1
1 2
3 3

a 0
b 4
c 5
d 10
e 15
• 01-12-2013, 11:00 PM
Fubarable
Re: Studying for Finals
You may be forgetting that += will add the current number the number already held by x.

So if x = 20, then x += 15 will make x == to 35.

Edit: Ah, you may be doing your math wrong. Not

0 0
0 1
1 2
3 3
6.... terminate

but rather

Code:

```i    x 0    0 1    1 2    3 3    6 4  10 terminate```
• 01-12-2013, 11:02 PM
hockey101
Re: Studying for Finals
I thought thats what I did?
• 01-12-2013, 11:04 PM
Fubarable
Re: Studying for Finals
• 01-13-2013, 04:21 PM
ekay90
Re: Studying for Finals
Quote:

Originally Posted by hockey101
Thats what I tried doing......
x i
0 0
0 1
1 2
3 3

a 0
b 4
c 5
d 10
e 15

for (int i=0; i<5; i++)
x += i;

Starting off with x = 0, and the formula is x += i
This formula means to keep adding the values of i on to the value of x.

Going through the values of 'i' up to 4 (because 4 is less than 5) here is what we get:
When i = 0:
0 + 0 = 0

When i = 1:
0 + 1 = 1

When i = 2:
1 + 2 = 3

When i = 3:
3 + 3 = 6

When i = 4:
6 + 4 = 10

Therefore the last value of x before the loop terminates is 10.