CS 1401, Quiz #4

Date: Tuesday, February 8, 2005
Name (please type legibly, ideally in block letters): ______________________________________________________________________

1. Declare a variable year of type long integer and assign it a value 2,005.

Answer:

long year = 2005;
2. Declare a constant named EL_PASO of type double and assign, to this constant, a value 700 thousand (use scientific notation).
Answer:

final double EL_PASO = 7.0E+05
3. What are the values of the following expressions:

(a) 8.4%2.2

Answer: 1.6

(b) (-20)%(-8)

Answer: -4

(c) 8.4%(-2.2)

 Answer: 1.6

4. Write down a code sequence that swaps the values of two integer variables a and b. Suppose that the initial value of a was 56, and the initial value of b was 65. Show, step by step, how the contents of these variables (and of all the necessary auxiliary variables) changes after each step of the swap sequences; draw boxes corresponding to the variables and show how their contents change.

Answer:
             
int aux = a;  
a = b;
b = aux; 

  ----    ----   
 | 56 |  | 65 |  
  ----    ----   
    a       b    

int aux = a;  

  ----    ----    ----
 | 56 |  | 65 |  | 56 |
  ----    ----    ----
    a       b      aux

a = b;

  ----    ----    ----
 | 65 |  | 65 |  | 56 |
  ----    ----    ----
    a       b      aux

b = aux; 

  ----    ----    ----
 | 65 |  | 56 |  | 56 |
  ----    ----    ----
    a       b      aux