Self Assessment: Statements/Variable Assignment Quiz

Given the following statements, answer the questions below:

1) a = 15;

2) int b;

3) b = 12;

4) a = a + 5;

5) b - 2 = 10;

6) int a = b - 5;

7) b = 9;

8) int c = b * a;

9) b = c;

10) a = b;

11) c = 5;

12) c = 3.2;

13) float f = 121.23;

14) c = f;

1. After the code up to line 4 has run, b will indeed be equal to 12, but a will NOT be equal to 20. There actually is an error. What is wrong with what we have done with a up to line 4?

1a. What is the difference between declaration and initialization? (When do you need to say the type of variable, like int)

2. Line 5 also will make java unhappy and won’t do anything (and will actually prevent us from compiling). Why?

3. After line 7, what will the variables a and b have stored in them?

4. After line 8, what will the values of a, b and c be?

5. After line 9, what will the values of a, b and c be?

6. After line 10, what will the values of a, b and c be?

7. After line 11, what will the values of a, b and c be?

8. A student thinks that at the end of line 10, a= 63, b = 63, and c = 3969 (which is 63*63). What mistake are they making?

9. What is the result of line 12?

10. What is the result of line 14.

The answers are on the next page.

 

Navigation: