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.
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?
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.
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?
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.
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?
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.
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?
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.
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?
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.