Selection Examples: 48 Problems with Answers

Worked examples of Selection to try before you open each solution, then practice problems with answers.

Worked Examples

Example 1

easy
Trace this code: SET age = 15. IF age >= 18 THEN OUTPUT 'Adult' ELSE OUTPUT 'Minor'.
Show solution

Answer

'Minor'

First step

1.
Step 1: age = 15.

Full solution

  1. 2.
    Step 2: Check condition: is 15 >= 18? No.
  2. 3.
    Step 3: Execute the ELSE branch: output 'Minor'.
Selection (IF-ELSE) allows a program to choose between two paths based on a condition. Only one branch executes depending on whether the condition is true or false.

Example 2

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

Example 3

medium
Write a one-line conditional expression in Python that assigns 'pass' if score >= 60 else 'fail'.

Example 4

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

Example 5

hard
Rewrite `if x > 0: return True; return False` as a single expression with no `if`.

Practice Problems

Try each one, then open the solution to compare your method.

Example 1

easy
What is the output? SET temp = 25. IF temp > 30 THEN OUTPUT 'Hot' ELIF temp > 20 THEN OUTPUT 'Warm' ELSE OUTPUT 'Cold'.

Example 2

medium
Trace this code: SET temp = 25. IF temp > 30 THEN fan = 'high' ELSE IF temp > 20 THEN fan = 'low' ELSE fan = 'off'. What is the value of fan?

Example 3

easy
Trace: x=7; if x>5: print 'big' else: print 'small'. What prints?

Example 4

easy
Trace: x=3; if x>5: print 'big' else: print 'small'. What prints?

Example 5

easy
How many of the two branches in a single if/else execute for one run?

Example 6

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

Example 7

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

Example 8

easy
Trace: n=4; if n%2==0: print 'even' else 'odd'. What prints?

Example 9

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

Example 10

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

Example 11

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

Example 12

medium
Trace: score=72; if score≥60: pass=true; if score≥90: honors=true. What are pass and honors?

Example 13

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

Example 14

medium
Trace: a=5; if a<0: s='neg' elif a==0: s='zero' else: s='pos'. What is s?

Example 15

medium
A function returns 'child' if age<13, 'teen' if age<20, else 'adult'. What does age=13 return?

Example 16

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

Example 17

medium
Trace nested selection: x=8; if x>5: if x>10: 'high' else 'mid' else 'low'. What prints?

Example 18

medium
Trace: x=0; if x>0: 'pos' elif x<0: 'neg' else: 'zero'. What prints?

Example 19

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

Example 20

challenge
A function: if x>0 return 'A'; if x>0 return 'B'. Which can ever be returned, and why is the second line dead code?

Example 21

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

Example 22

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

Example 23

easy
`x = 12`; `if x > 10: print('big') else: print('small')`. What prints?

Example 24

easy
`n = 0`; `if n: print('truthy') else: print('falsy')`. What prints in Python?

Example 25

easy
How many branches of a single `if`/`elif`/`else` chain run for one execution?

Example 26

easy
`age = 18`; `if age >= 18: print('adult') else: print('minor')`. What prints?

Example 27

easy
`x = 7`; `if x > 0: y = 1`. What is `y` after the code runs?

Example 28

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

Example 29

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

Example 30

medium
`score = 75`; `if score >= 90: g='A' elif score >= 80: g='B' elif score >= 70: g='C' else: g='F'`. What is g?

Example 31

medium
`x = 5`; `if x > 0 or 1/0 > 0: print('ok')`. Does the program crash?

Example 32

medium
`x = 0`; `if x: print('A') elif x == 0: print('B') else: print('C')`. What prints?

Example 33

medium
`temp = 30`; `if temp > 25: fan='high'; if temp > 35: fan='max' else: fan='medium'`. What is `fan` at the end?

Example 34

medium
A buggy line: `if x = 5: print('hit')` in a language that distinguishes `=` and `==`. What happens?

Example 35

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 36

medium
`a = True, b = False`; `if a and not b: print('X')`. Does X print?

Example 37

medium
`x = 7`; `if x > 5: pass; print('done')`. What prints?

Example 38

medium
`x = 10`; `if x > 5: print('A'); if x > 8: print('B')`. What prints?

Example 39

medium
`x = -2`; `if x > 0: s='pos' elif x == 0: s='zero' else: s='neg'`. What is s?

Example 40

hard
`x = 5, y = 10, z = 7`; `m = x if x > y and x > z else (y if y > z else z)`. What is m?

Example 41

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 42

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 43

challenge
A program: `if a and b or c`. With a=False,b=True,c=True, what is the value? (Use standard precedence: `not` > `and` > `or`.)

What to focus on

Core idea: Programs can choose between different execution paths depending on the current data values.

Common stuck point: Conditions must be unambiguous—what happens when temperature = 30?

Ask yourself: Does a true-or-false test decide which one of several blocks runs, with each block running at most once?

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

Selection works like a fork in a hiking trail with a sign: 'More than 2 hours of daylight left? Take the left path to the peak. Otherwise take the right path back to the car.' You read the sign once, take exactly one path, and never walk both. An else-if chain is a series of forks where you turn off at the first sign that applies to you.

Read the full lesson on Selection

Background knowledge

These ideas may be useful before you work through the harder examples.

sequence