Flowchart to Code: Mapping Control Structures Quiz Quiz

Evaluate your ability to convert flowchart control structures into equivalent code, focusing on key elements like decisions, loops, and sequencing. Strengthen your understanding of programming logic and flowchart interpretation with these scenario-based questions.

  1. Sequencing Flowchart Steps to Code

    Given a flowchart that starts by initializing variable x to 5, then increments x by 2, and finally prints x, which code segment correctly maps these steps?

    1. int x = 5; x = x + 2; print(x);
    2. int x = 2; x = x + 5; print(x);
    3. int x = 7; x = x + 0; print(x);
    4. int x = 5; x = 5 + 2; print(x - 2);

    Explanation: The correct mapping must initialize x to 5, add 2, then print the new value—this is done in the first option. Option two initializes x incorrectly to 2 instead of 5. The third option prints x minus 2, which is not part of the flowchart. The fourth option initializes x to 7 from the start, so it does not reflect the intended sequence.

  2. Identifying the 'If-Else' Structure

    A flowchart shows a diamond decision box asking 'Is score u003E= 60?' with a 'Yes' line leading to 'Print Pass' and 'No' leading to 'Print Fail'. Which code best represents this control structure?

    1. if (score u003E 60) { print('Fail'); } else { print('Pass'); }
    2. if (score = 60) { print('Pass'); } else { print('Fail'); }
    3. if (score u003E= 60) { print('Pass'); } else { print('Fail'); }
    4. if (score u003C= 60) { print('Fail'); } else { print('Pass'); }

    Explanation: The correct answer checks for 'score greater than or equal to 60' and prints 'Pass' if true, as specified. The second option reverses the logic by checking less than or equal, which is incorrect. The third uses assignment instead of comparison, leading to errors. The fourth misassigns 'Fail' to scores above 60, which is not what the flowchart intended.

  3. Mapping a Loop in a Flowchart

    A flowchart depicts a loop where variable n starts at 1, increments by 1 each time, and repeats while n is less than or equal to 3. Which code segment accurately represents this flowchart?

    1. while (n = 1; n u003C 4; n++) { /* code */ }
    2. for (int n = 1; n u003C= 3; n++) { /* code */ }
    3. for (int n = 0; n u003C 3; n++) { /* code */ }
    4. for (int n = 3; n u003E 0; n--) { /* code */ }

    Explanation: The 'for' loop starting with n at 1 and running while n is less than or equal to 3 directly reflects the flowchart's logic. The second option starts at zero and excludes 3, which does not match. The third uses incorrect syntax; assignment replaces comparison. The fourth counts down instead of up, contradicting the described increment in the flowchart.

  4. Translating a Decision with Multiple Outcomes

    A flowchart checks the value of variable day and directs to: 'Print Weekend' if day is Saturday or Sunday, otherwise 'Print Weekday'. Which code structure correctly mirrors this logic?

    1. if (day == 'Saturday' || day == 'Sunday') { print('Weekend'); } else { print('Weekday'); }
    2. if (day = 'Saturday' || day = 'Sunday') { print('Weekday'); } else { print('Weekend'); }
    3. if (day != 'Saturday' || day != 'Sunday') { print('Weekend'); } else { print('Weekday'); }
    4. if (day == 'Saturday' u0026u0026 day == 'Sunday') { print('Weekend'); } else { print('Weekday'); }

    Explanation: The first option checks if day equals 'Saturday' or 'Sunday' and prints 'Weekend', reflecting the flowchart's structure. The second uses 'and', which can never be true for a single value. The third reverses the print statements and confuses assignment with equality. The fourth incorrectly uses 'not equals' with 'or', which results in 'Weekend' for every day.

  5. Understanding Nested Decisions in Flowcharts

    A flowchart tests if age is greater than 18 and, if true, checks if citizen is true before printing 'Eligible'. If either condition fails, it prints 'Not Eligible'. Which code matches this nested decision flow?

    1. if (age u003C 18) { print('Not Eligible'); } else { if (!citizen) { print('Eligible'); } else { print('Not Eligible'); } }
    2. if (age u003E 18) { if (citizen) { print('Eligible'); } else { print('Not Eligible'); } } else { print('Not Eligible'); }
    3. if (age u003E 18 || citizen) { print('Eligible'); } else { print('Not Eligible'); }
    4. if (age u003E= 18 u0026u0026 citizen) { print('Eligible'); } else { print('Not Eligible'); }

    Explanation: The correct answer mirrors the nested decision: check age, then citizenship, matching the flowchart. The second uses 'or', so it inaccurately grants eligibility if only one condition is met. The third confuses the criteria for eligibility within the nested logic. The fourth option misuses 'greater than or equal', and while similar, structurally it's not nested as described by the flowchart.