අන්තර්ගතයට යන්න
mcqdinethlive
SPARK · UNIT 9

Trace tables for Python loops

A Python loop MCQ is solved on paper with a trace table, not in the head. One past paper question shows why the wrong options exist.

සිංහලෙන් කියවන්න

A Python loop question is solved with a trace table, not in the head. The table has one column for each changing variable, one for the condition and one for the output, and one row for each change. Control structures (Syllabus 9.8, page 39) (විෂය නිර්දේශය 9.8, පිටුව 36) appear in all eight new-syllabus papers, with 17 MCQs and 59 Paper II marks, and most of them start with a trace.

2023 (held 2024) Paper I, Q43 shows why. i starts at 7, and the loop subtracts 3, prints a star, then either breaks (when i is 2 or less) or prints a second star. The trace has two rows. Row 1: i = 4, one star, 4 is not 2 or less, so a second star. Row 2: i = 1, a third star, then break. The answer is 3 stars, option (2), our own worked answer. A trace in the head often drops the star printed just before the break, or the star from the else. The table keeps every line that ran, so nothing is dropped.

So draw the table, even for a short loop. The full method, with range(), sentinel loops, break, continue, nested loops and the loop else, is in Python loop tracing with trace tables.

මේ Spark එක බෙදාගන්න

A/L ICT කරන යාළුවෙකුට යවන්න.

තවත් Sparks

  1. English

    Layer 2 and Layer 3 switches 101

    A Layer 2 switch forwards frames by MAC address inside one LAN, and a Layer 3 switch also routes packets by IP address between networks. Why both exist, where they work today, and the past paper questions that test them.

  2. English

    Planning an A/L ICT second attempt

    A low 2026 A/L ICT result is a map of gaps, not an end. Six units carry 73.4% of the marks, and every paper for the plan is free on this site.

  3. English

    Sequential search in Python 101

    A sequential search returns inside the loop on a match and gives the not found value only after the loop. 2026 Paper II Q9(c) shows where that line goes.

All Sparks