Schedule‎ > ‎

03a Exercise 3.1 Variable Assignment Tracing

Lesson 3: Exercise 3.1  Variable Assignment Tracing Exercises

NAME:

Partner’s Name:

Note:  Each student must turn in a complete Exercise 3.1 to the Instructor

1.      What are the final values stored in x and y after the following code fragment has executed?

int x = 5;

int y = 7;

x = y;

The final value of x is _________. The final value of y is _________.

2. What are the final values stored in x and y after the following code fragment has executed?

int x = 5;
int y = 7;
y = x;

The final value of x is _________. The final value of y is _________.

3. What are the final values stored in x and y after the following code fragment has executed?

int y = 7;
int x = 5;
y = x;

The final value of x is _________. The final value of y is _________.

4. What are the final values stored in x and y after the following code fragment has executed?

int y = 7;
int x = 5;
y = x;
x = 3;

The final value of x is _________. The final value of y is _________.

 

 

5.  What are the final values in x, y and z after the following code fragment has executed?

int x = 1;
int y = 2;
int z = 3;
y = x;
x = y;

The final value of x is _________. The final value of y is _________. The final value of z is _________.

6. What are the final values in one, two and three after the following code fragment has executed?

int one = 1;
int two = 3;
int three = 2;
one = two;
two = three;

The final value of one is _________. The final value of two is _________. The final value of three is _________.

7. What are the final values in a, b and c after the following has executed?

int a = 9;
int b = 7;
int c = 8;
a = c;
b = a;
c = b;

The final value of a is _________. The final value of b is _________. The final value of c is _________.

8. What are the final values in a, b and c after the following has executed?

int a = 1;
int b = 2;
int c = 3;
c = b;
c = a;
a = b;

The final value of a is _________. The final value of b is _________. The final value of c is _________.

9. What are the final values in x, y and z after the following has executed?

int x = 1;
int y = 2;
int z = 1;
z = y;
y = x;
x = z;

The final value of x is _________. The final value of y is _________. The final value of z is _________.

10. Check Understanding

a.     True or false: A variable still holds its original value after a new value is assigned.

b.     True or false: Assignment works by copying right to left.

c.     True or false: A sequence of assignments all happen together at once.

d.     True or false: A sequence of assignments can happen in any order with no change to the outcome.

e.     True or false: variables on both the left hand side and right hand side of an assignment change.

f.      True or false: A variable must be on the left side of the equals sign (=) in an assignment statement to receive a new value.

g.     True or false: an assignment is like a mathematical equality, making both sides the same so that future changes to one change the other.

h.     True or false: A variable can hold all the values ever assigned to it at the same time.

 

 

Comments