Computer Program Reading Comprehension Project
U of T Computer Program Reading Comprehension Project
Swap


The following code excerpt is taken from a program originally written by a csc108 student as part of his assignment.

Download files: Swap.java
Areas: Primitive types.


Swap.java
1  int a;
2  int b;
3  a = 5;
4  b = 7;
5  int temp = a;
6  a = b;
7  b = temp;
8


Question 1
What does the following statement do?
int a;
Solution

Question 2
What happens when the statement a=b; is executed?
Solution

Question 3
What exactly is the student trying to do in this program?
Hints Solution

Question 4
What is the purpose of the variable 'temp'?
Solution

Question 5
The swapping function that this code excerpt does can be performed in another way, without the use of temp. Can you think of how this can be done?
Solution