NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Automatic Generation of Programming Exercises and Code Explanations using Large Language Models

Sami Sarsa, Paul Denny, Arto Hellas, Juho LeinonenarXiv 2022 · DOI

TL;DR

This paper explores how large language models (LLMs)—specifically OpenAI Codex—can automate the creation of introductory programming exercises and line-by-line code explanations. The authors demonstrate that while Codex can reliably generate novel, sensible problems tailored to specific themes and programming concepts, human oversight remains essential to correct minor logical slips and broken test suites before classroom deployment.

What Problem Does This Paper Address?

Designing an introductory computer science course requires a massive repository of practice problems, sample solutions, and automated test suites. Writing these resources from scratch is incredibly time-consuming for educators. Furthermore, once an exercise is deployed, its solution is often quickly shared online, compromising the integrity of question banks for future cohorts.

At the same time, providing immediate, personalized feedback to students when they get stuck is a major scaling challenge. While auto-graders verify correctness, they rarely explain why or how a piece of code works. This paper addresses these challenges by shifting the LLM narrative: instead of focusing on how students might use AI to cheat, the authors investigate how instructors can leverage LLMs to rapidly generate novel learning materials and natural language code explanations.

Figure 1: Lifecycle of a programming exercise. Teacher creates programming exercises and learning materials. Stu- dents study the materials and the exercises, and create exercise attempts. Students receive feedback on their attempts. In this work, denoted by the solid arrows with a robot, we explore the use of OpenAI Codex for the creation of pro- gramming exercises and for providing feedback on students’ programming exercise attempts. The dashed arrow with a robot represents prior work by Finnie-Ansley et al. Finnie-Ansley et al. (2022) who explored how well Codex can solve introductory programming exercises.

Methods

The researchers utilized the code-davinci-001 version of OpenAI Codex. They evaluated its performance on two distinct tasks:

  1. Exercise Generation: The authors used a few-shot priming approach. They provided Codex with a prompt containing a sample exercise (consisting of keywords, a problem statement, a sample Python solution, and unit tests) and then appended a new set of target keywords. They systematically varied the programming concepts (e.g., functions vs. classes) and contextual themes (e.g., hiking, football, cooking) to see how well the model adapted.
  2. Code Explanation Generation: To generate line-by-line (multistructural) explanations, they primed the model with a completed code block followed by the text: """Step-by-step explanation of the above program: 1.

The generated exercises were evaluated using a mixed-methods approach: a manual qualitative review by four researchers using a structured rubric, alongside programmatic testing to check if the generated solutions actually ran and passed their corresponding generated test suites.

Datasets

The study was grounded in four classic introductory programming problems frequently featured in computing education research:

  • A variant of the speeding problem (focusing on conditionals and return values).
  • A variant of FizzBuzz (focusing on loops, lists, and conditional ordering).
  • A variant of the Rainfall Problem (a classic loop-and-conditional aggregation problem).
  • A currency converter application (focusing on object-oriented programming, methods, and dictionaries).

By systematically combining these base problems with 10 different contextual themes, 3 programming concept sets, and 2 temperature settings (0 and 0.75), they generated a total synthetic dataset of 240 programming exercises and 20 code explanations for evaluation.

Key Insights and Ideas

  • Remarkable Contextual Control: Codex proved highly adept at weaving arbitrary themes into logical programming problems. Supplying the keyword "football" naturally yielded problems involving Lionel Messi and Cristiano Ronaldo, while "relationships" generated problems about managing lists of friends. This suggests a future where teachers can instantly generate hyper-personalized assignments tailored to individual student interests.
  • The "Print vs. Return" Pitfall: The most common failure mode in the generated exercises was a mismatch between the sample solution and the test suite. Codex frequently wrote sample solutions that printed an output, while its own generated unit tests expected the function to return a value. Ironically, this mirrors one of the most common mistakes human novices make.
  • Line-by-Line Bias: When generating explanations, Codex naturally defaulted to line-by-line walkthroughs (multistructural explanations) and struggled to provide high-level, abstract summaries of a program's overall purpose (relational explanations), even when explicitly prompted to do so.

Key Results

The quantitative evaluation highlighted a clear gap between semantic plausibility and execution accuracy:

Qualitative Manual Review (Sample of 120 exercises): * 75.0% of the generated problem statements were deemed "sensible" and practical for students. * 81.8% were entirely novel, returning zero verbatim matches when searched across Google and GitHub. * 76.7% featured a sample solution that accurately matched the problem description.

Programmatic Review (All 240 exercises): * 84.6% successfully generated a sample solution, and 89.7% of those solutions ran without syntax or runtime errors. * While 70.8% generated automated test suites, only 30.9% of those exercises had test suites that passed their own sample solutions out-of-the-box. However, for the test suites that did pass, they achieved an outstanding average statement coverage of 98.0%.

Code Explanations (20 explanations): * 90.0% of the explanations successfully covered every part of the provided code. * 67.2% of the individual line-by-line explanation strings were completely accurate. The remaining erroneous lines typically contained minor logical inversions (e.g., explaining speed > 100 as "if speed is less than 100").

Limitations and Future Work

The authors acknowledge several limitations. The study was strictly confined to Python and English, and it focused entirely on small, isolated exercises typical of early CS1/CS2 courses; it remains unknown how well this approach scales to larger, multi-file software projects.

Furthermore, because only ~30% of the generated exercises were completely flawless out-of-the-box, these tools cannot yet replace human instructors. Instead, the authors propose a "human-in-the-loop" workflow.

Moving forward, they suggest developing automated validation layers that filter out broken test suites before presenting options to teachers. They also introduce the concept of "robosourcing"—a novel pedagogy where students are tasked with evaluating, debugging, and improving AI-generated exercises and explanations as an active learning activity.

Summary updated

BibTeX
@misc{sarsa2022automatic,
  author = {Sami Sarsa and Paul Denny and Arto Hellas and Juho Leinonen},
  title = {{Automatic Generation of Programming Exercises and Code Explanations using Large Language Models}},
  year = {2022},
  eprint = {2206.11861},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2206.11861},
}
Figure