How to study for a CS exam: trace, explain, practice

Short answer: To study for an intro programming exam, practice three things in order: trace code by hand with a memory table, explain what code does in plain English, then write programs from scratch under exam conditions. Reading your notes or rewatching lectures skips all three.

What the research found

Researchers at several universities gave students an exam after one semester of introductory programming and compared how they did on different kinds of questions. In Lopez et al. (2008), published at ICER, students' scores on code tracing questions and on "explain in plain English" questions were both related to their scores on code writing questions. A regression model built from the data suggested a hierarchy. Knowledge of basic constructs sits at the bottom. Explaining code, solving Parsons puzzles (reordering scrambled lines), and tracing loops sit in the middle. Writing code sits on top.

A second study tested whether tracing can be taught quickly. Xie et al. (2018) taught a strategy to CS1 students that used a memory table: one table per method call, each variable in its own row, and old values crossed out when a variable changes. Teaching it took about 5 to 10 minutes. The 24 students were split into 13 who learned the strategy and 11 who did not. The strategy group scored higher on six output-prediction problems (mean 30.0 out of 36, versus 24.6). Their midterm average was also higher (91.5 versus 84.5), but that gap was not statistically significant, and only some students reported their grades.

Where the research stops

The first study looked at relationships in one exam. It cannot show that practicing tracing causes better code writing. The second study was small, drew on volunteers, and used Java programs from early in the course. Its authors warn the table method may not carry over to arrays or objects. Treat both as good reasons to try the method, not proof that it works for everyone.

Why it works

Writing a program means holding a picture of what each line does to your variables. If you cannot follow code that already exists, you have little chance of building new code that works. Tracing on paper makes that picture visible, so mistakes show up as a wrong number in a table instead of a failed test you do not understand.

Explaining code in plain English tests something different. You have to say what a loop is for, which goes beyond what each line does. That is the skill you need when you plan a solution before typing it.

How to do it

  1. Trace on paper. Pick a short program from your homework or lecture slides with a loop or an if statement. Draw a table for each function call, with one row per variable. Run the code one line at a time. When a value changes, cross out the old one and write the new one beside it. Predict the output, then run the real code and compare. Where your answer differs, find the exact line where your table went wrong.
  2. Explain in plain English. Take code you just traced and describe its purpose in one sentence, without reading the lines aloud. "It returns the second largest value in the list" is an explanation. "It loops through i and compares to max" is not. Try it with no notes open, the same way as the blank page method.
  3. Write from scratch. Take a past exam or homework prompt and close the solution. Set a timer for the exam's real pace, and write the answer on paper or in a plain text editor with no autocomplete. Afterward, trace your own code on a small input to find bugs, then check it against the solution.
  4. Repeat in rounds. Do several trace-and-explain problems before each writing problem, and come back to the ones you missed a few days later. A spaced study schedule helps here.

Here is what that looks like on a small example. Say the code is total = 0, then for i in range(1, 4): total = total + i. Your table has two rows, total and i. Start with total at 0. On the first pass i is 1, so you cross out 0 and write 1. On the second pass i is 2 and total becomes 3. On the third pass i is 3 and total becomes 6. The loop stops there because range(1, 4) never reaches 4. If you had guessed 10, the table shows exactly where the off-by-one slipped in.

Try it tonight: Pick one loop from your last assignment. Draw a memory table, trace it with a small input, and write one sentence saying what the loop is for. Then run it and see if your trace was right.

Common mistakes

Do not trace only code that you already understand. Pick the problems you got wrong. Do not skip the table and trace in your head either, because that is where small errors hide. Finally, do not write practice code with an IDE open if the exam is on paper. Autocomplete and error messages hide gaps you will meet on exam day.

Sources

  1. Lopez, M., Whalley, J., Robbins, P., & Lister, R. (2008). Relationships between reading, tracing and writing skills in introductory programming. Proceedings of the Fourth International Workshop on Computing Education Research (ICER '08), 101-111. https://doi.org/10.1145/1404520.1404531
  2. Xie, B., Nelson, G. L., & Ko, A. J. (2018). An explicit strategy to scaffold novice program tracing. Proceedings of the 49th ACM Technical Symposium on Computer Science Education (SIGCSE '18). https://doi.org/10.1145/3159450.3159527