Practice Selection in CS Thinking

Use these practice problems to test your method after reviewing the concept explanation and worked examples.

Quick Recap

Choosing which block of code to execute based on whether a condition is true or false. Selection allows programs to make decisions, following one path when a condition holds and a different path otherwise, using constructs like if, else-if, and else.

If this is true, do that path. If it is false, take a different path instead.

Showing a random 20 of 50 problems.

Example 1

medium
Trace: x=10,y=20x=10,y=20; if x>5x > 5 and y>25y > 25: 'A' else 'B'. What prints?

Example 2

easy
In most programming languages, which symbol tests equality (not assignment)?

Example 3

easy
Trace: g=85g=85; if g90g \ge 90: 'A' elif g80g \ge 80: 'B' else 'C'. What prints?

Example 4

medium
Write pseudocode using IF-ELIF-ELSE to assign a grade: 90+ = A, 80–89 = B, 70–79 = C, below 70 = F.

Example 5

easy
`n = 5`; `if n % 2 == 0: print('even') else: print('odd')`. What prints?

Example 6

medium
`x = 100`; `if x > 10: print('A') elif x > 50: print('B') elif x > 90: print('C')`. What prints, and why is B/C dead?

Example 7

medium
A chain uses 'if x>0: A elif x>10: B'. For x=15x=15, which prints, and what design flaw exists?

Example 8

hard
A function returns 'tween' if 10 <= age <= 12, 'teen' if 13 <= age <= 19, else 'other'. What does it return for age = 13?

Example 9

easy
In code, what symbol compares two values for equality (not assignment)?

Example 10

medium
Trace: temp=30temp=30; if temp>25temp > 25: ac='on'; else: ac='off'. What is ac?

Example 11

medium
Trace: score=72score=72; if score60score \ge 60: pass=true; if score90score \ge 90: honors=true. What are pass and honors?

Example 12

challenge
Convert this to a single condition: 'if x>0: if y>0: print P'. Write the equivalent one-line condition.

Example 13

challenge
A grading chain checks 90\ge 90 then 80\ge 80 then 70\ge 70. A bug orders them 70\ge 70 first. For score 95, what grade results and why is it wrong?

Example 14

easy
Is 'if temp = 100' a correct condition in a language that distinguishes = and ==? Yes or no.

Example 15

hard
Bug: a chain checks `if score >= 70 elif score >= 80 elif score >= 90`. For score=95 what grade results, and why is it wrong?

Example 16

medium
Identify whether this is selection or iteration: 'while x < 10: x = x + 1'. Answer with one word.

Example 17

medium
`x = 10, y = 5`; `if x > 0 and y > 0: print('Q1')`. Does Q1 print?

Example 18

easy
Which clause in an `if`/`elif`/`else` chain handles every case not matched by an earlier condition?

Example 19

hard
`x = 4`; `if x % 2 == 0 and x > 0: r='ep' elif x % 2 == 0: r='en' else: r='o'`. What is r?

Example 20

easy
Which control structure lets a program take a different path when a condition is false?