1. Member
Join Date
Dec 2012
Posts
44
Rep Power
0

## 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.

2. ## 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.
Last edited by Fubarable; 01-12-2013 at 10:07 PM.

3. Member
Join Date
Dec 2012
Posts
44
Rep Power
0

## Re: Studying for Finals

All I wanted to know if that statement was correct or not

4. Member
Join Date
Dec 2012
Posts
44
Rep Power
0

## Re: Studying for Finals

But i think i figured it out on my own...thanks

5. ## Re: Studying for Finals

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!

6. Member
Join Date
Dec 2012
Posts
44
Rep Power
0

## 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;

7. ## Re: Studying for Finals

What happens when you write out what happens on paper? It will take at most 5 lines to do.

8. Member
Join Date
Dec 2012
Posts
44
Rep Power
0

## 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

9. ## 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

Java Code:
```i     x
0    0
1    1
2    3
3    6
4   10
terminate```
Last edited by Fubarable; 01-12-2013 at 11:03 PM.

10. Member
Join Date
Dec 2012
Posts
44
Rep Power
0

## Re: Studying for Finals

I thought thats what I did?

12. Member
Join Date
Jan 2013
Posts
6
Rep Power
0

## Re: Studying for Finals

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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•