The majority of the AP Computer Science A multiple-choice questions ask you to trace the code, so it is imperative that you master this concept. Tracing is when you read through the code line by line and keep track of the variables and their values, so you know exactly what is going on in your program. Tracing code is useful for debugging as well. Fortunately, the AP exam will only ask you to trace short snippets of code.
How do you trace code? You use a table to keep track of the values and variables. Let's walk through an example with a for loop:
for (int i = 1; i <= 5; i++) {
System.out.println(i + 10);
}
The first line of this for loop creates a loop counter variable i
and assigns it the value 1. It limits the loop to run only while i
is less than or equal to 5 and increments i
by 1 each time the loop is run. Let's create a table with this information.
We listed the iteration number (the number of times the loop has run) and the value of the loop counter variable i. This is the first iteration, and i was initialized with the value 1.
The body of the loop contains a print statement that prints ten more than the value of i. Let's add this to our table.
The first iteration of this loop will print 11 to the console. Now we can go back to the top of the loop and repeat this process until the loop condition becomes false. Try this on your own with a pencil and paper, then look below to check your work. Here is the finished table:
Iteration # | i | i + 10 |
1 | 1 | 11 |
2 | 2 | 12 |
3 | 3 | 13 |
4 | 4 | 14 |
5 | 5 | 15 |
From our tracing table, we can tell that the output of this for loop is
11
12
13
14
15
Often, the multiple-choice questions will ask you how many times the loop runs or what the output of the loop is, so meticulous tracing of the code is important so you don't choose 6 iterations when the answer is 7 iterations.
This was a fairly simple example. Try this next one on your own, then read through the explanation below:
What is the output of this code?
for (int i = 0; i < 6; i += 2) {
for (int j = 3; j <= 6; j += 3) {
System.out.println(i+j);
}
System.out.println("*");
}
The first line declares an integer variable i
and initializes it to 0. It also starts a for
loop that will continue to execute as long as i
is less than 6. The for
loop will iterate 3 times because i
is increased by 2 at the end of each iteration. The second line declares an integer variable j
and initializes it to 3. It also starts a nested for
loop that will continue to execute as long as j
is less than or equal to 6. The inner for
loop will iterate 2 times for each iteration of the outer loop, because j
is increased by 3 at the end of each iteration. The number of times the inner loop iterates is equal to the number of times the outer loop iterates multiplied by the number of times the inner loop iterates, so 3*2=6. This is our tracing table so far:
Outer Loop Iteration # | Inner Loop Iteration # | i | j |
1 | 1 | 0 | 3 |
The third line prints the sum of i
and j
to the console on a new line.
Outer Loop Iteration # | Inner Loop Iteration # | i | j | i + j |
1 | 1 | 0 | 3 | 3 |
The fifth line is run only in the scope of the outside loop. In this line, an asterisk is printed to the console. Putting all these pieces together, our tracing table looks like this:
Outer Loop Iteration # | Inner Loop Iteration # | i | j | i + j | Asterisk? |
1 | 1 | 0 | 3 | 3 | no |
1 | 2 | 0 | 6 | 6 | no |
1 |
| 0 |
|
| * |
2 | 1 | 2 | 3 | 5 | no |
2 | 2 | 2 | 6 | 8 | no |
2 |
| 2 |
|
| * |
3 | 1 | 4 | 3 | 7 | no |
3 | 2 | 4 | 6 | 10 | no |
3 |
| 4 |
|
| * |
The two columns on the right track the output of the nested for loop, and combining them yields:
3
6
*
5
8
*
7
10
*
That may have been tedious, but it was worth it to make sure there were no errors in our tracing. Be sure to practice with the practice problems below to perfect your tracing skills and ace the multiple-choice portion of the AP exam.
1. Consider the following for
loop:
for (int i = 0; i < 5; i++) {
System.out.print(i + " ");
}
What is the output of this loop?
a) 0 1 2 3 4
b) 1 2 3 4 5
c) 0 1 2 3
d) 1 2 3 4
Answer: a
2. Consider the following for
loop:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
What is the output of this loop?
a)
*
**
***
b)
****
***
**
*
c)
*****
****
***
**
d)
*
**
***
****
*****
Answer: d
3. Consider the following for
loop:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 3; j++) {
System.out.print(j + " ");
}
System.out.println();
}
How many times does the inner loop run?
a) 5
b) 8
c) 15
d) 20
Answer: c
4. Consider the following while
loop:
int i = 1;
while (i <= 5) {
int j = 1;
while (j <= i) {
System.out.print(i + " ");
j++;
}
System.out.println();
i++;
}
What is the output of this code?
a) 12345
b)
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
c)
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
d)
1
22
333
4444
55555
Answer: c
5. Consider the following while
loop:
int i = 1;
while (i <= 5) {
int j = 5;
while (j >= i) {
System.out.print("*");
j--;
}
System.out.println();
i++;
}
What is the output of this code?
a)
*****
****
***
b)
*****
****
***
**
*
c)
*
**
***
d)
*
**
***
****
*****
Answer: b