# Variable Assignment Tracing Exercises

Complete each exercise problem. After completing a problem and before continuing to the next, compare your answers with another student. If there is a disagreement, ask the instructor for clarification. After verifying your answer, record the final values of all variables. After finishing all the exercise problems, answer the Check Understanding questions to verify your understanding.

### Two Variables, x Gets y

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

Solve this by doing a dry run, filling in the value in the boxes.

After `int x = 5;` has executed the variables hold the following values:

 x

After `int y = 7;` has executed the variables hold the following values:

 x y

After `x = y;` has executed the variables hold the following values:

 x y

### Two Variables, y Gets x

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

Solve this by doing a dry run, filling in the value in the boxes.

After `int x = 5;` has executed the variables hold the following values:

 x

After `int y = 7;` has executed the variables hold the following values:

 x y

After `y = x;` has executed the variables hold the following values:

 x y

### Two Variables, Different Declaration Order

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

Solve this by doing a dry run, filling in the value in the boxes.

After `int y = 7;` has executed the variables hold the following values:

 y

After `int x = 5;` has executed the variables hold the following values:

 x y

After `y = x;` has executed the variables hold the following values:

 x y

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

Solve this by doing a dry run, filling in the value in the boxes.

After `int y = 7;` has executed the variables hold the following values:

 y

After `int x = 5;` has executed the variables hold the following values:

 x y

After `y = x;` has executed the variables hold the following values:

 x y

After `x = 3;` has executed the variables hold the following values:

 x y

True or false: A variable can hold all the values ever assignment to it.

### Three Variables, Two Assignments

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

Solve this by doing a dry run, filling in the value in the boxes.

After `int x = 1;` has executed the variables hold the following values:

 x

After `int y = 2;` has executed the variables hold the following values:

 x y

After `int z = 3;` has executed the variables hold the following values:

 x y z

After `y = x;` has executed the variables hold the following values:

 x y z

After `x = y;` has executed the variables hold the following values:

 x y z

### Three Variables, Sequential Assignment

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

Solve this by doing a dry run, filling in the value in the boxes.

After `int one = 1;` has executed the variables hold the following values:

 one

After `int two = 3;` has executed the variables hold the following values:

 one two

After `int three = 2;` has executed the variables hold the following values:

 one two three

After `one = two;` has executed the variables hold the following values:

 one two three

After `two = three;` has executed the variables hold the following values:

 one two three

### Three Variables, Three Assignments

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

Solve this by doing a dry run, filling in the value in the boxes.

After `int a = 9;` has executed the variables hold the following values:

 a

After `int b = 7;` has executed the variables hold the following values:

 a b

After `int c = 8;` has executed the variables hold the following values:

 a b c

After `a = c;` has executed the variables hold the following values:

 a b c

After `b = a;` has executed the variables hold the following values:

 a b c

After `c = b;` has executed the variables hold the following values:

 a b c

### Three Variables Changed

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

Solve this by doing a dry run, filling in the value in the boxes.

After `int a = 1;` has executed the variables hold the following values:

 a

After `int b = 2;` has executed the variables hold the following values:

 a b

After `int c = 3;` has executed the variables hold the following values:

 a b c

After `c = b;` has executed the variables hold the following values:

 a b c

After `c = a;` has executed the variables hold the following values:

 a b c

After `a = b;` has executed the variables hold the following values:

 a b c

### Three Variables, First Two Exchanged

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

Solve this by doing a dry run, filling in the value in the boxes.

After `int x = 1;` has executed the variables hold the following values:

 x

After `int y = 2;` has executed the variables hold the following values:

 x y

After `int z = 1;` has executed the variables hold the following values:

 x y z

After `z = y;` has executed the variables hold the following values:

 x y z

After `y = x;` has executed the variables hold the following values:

 x y z

After `x = z;` has executed the variables hold the following values:

 x y z

### 10. Check Understanding

1. True or false: A variable still holds its original value after a new value is assigned.
2. True or false: Assignment works by copying right to left.
3. True or false: A sequence of assignments all happen together at once.
4. True or false: A sequence of assignments can happen in any order with no change to the outcome.
5. True or false: variables on both the left hand side and right hand side of an assignment change.
6. True or false: A variable must be on the left side of the equals sign (=) in an assignment statement to receive a new value.
7. True or false: an assignment is like a mathematical equality, making both sides the same so that future changes to one change the other.
8. True or false: A variable can hold all the values ever assigned to it at the same time.