Test: Branching (Selection) in a Program

Test 2 of 8 · Tests

On this page you can revise the statements that branch the flow of a program and check your understanding of conditions, the statements if, if–else, multiple selection and switch. The goal is not only to remember the syntax, but also to be able to tell which part of the program will run for given values of the variables.

How to practise: choose one group and solve it in 45 minutes, without looking at the solutions or your notes. Only after that compare your solution with the solution of the practice test.

What you need to know for the test

  • Conditional expressions: comparisons such as ==, !=, <, >, <= and >=.
  • Logical operators: && (and), || (or) and ! (not).
  • Single selection: the statement if, when a block runs only if the condition is met.
  • Double selection: if–else, when the program chooses one of two branches.
  • Multiple selection: a chain of if–else if–else statements and choosing with switch.
  • Order and boundaries: check what happens when a value is equal to the boundary of a condition, and in which order the conditions are tested.
  • Blocks of statements: using braces correctly and, in a switch statement, the labels case, break and default.

What the test looks like

  • Duration: one school lesson (45 minutes).
  • Tasks: 3 (sometimes 4) tasks; every group has tasks of the same difficulty, but with different data.
  • What is graded: whether the program works correctly for the example and for edge cases, whether the input and output are as in the text and whether the code is clear.
Suggested scoring (3 tasks of 10 points each)
Points0–1112–1617–2122–2627–30
Grade12345

The scale is a suggestion for self-assessment; the teacher can adapt it. (In Serbian schools grades go from 1 to 5, where 5 is the best.)

Practice test with solutions

The tasks from "Group 10" (PDF) are written out here as text, with an example, a hint and a solution. Set a timer for 45 minutes and try to solve them on your own; open the solutions only at the end.

TASK 01Group 10

The area of a circular ring

Write a program that computes the area of a circular ring for the entered radii of the outer and the inner circle, Ro and Ri. Check whether the data are valid: both numbers must be positive, and the outer radius must be greater than the inner one. If the data are valid, print the area to two decimal places, and if they are not – an error message.

Example 1

Input

5 3

Output

Ring area: 50.27

Example 2

Input

3 5

Output

Invalid data
TASK 02Group 10

Executing an if statement

We are given a part of a program (in Java, as in the original test):

if (a > 12 || !(b > 0)) {
    if (!(c > 5)) {
        System.out.println("True, the statement will be executed");
    }
}

For which of the following entered values a b c will the message be printed?

a) -1 5 -7   b) 2 8 6   c) 4 -6 5   d) 16 0 3   e) 5 6 3

TASK 03Group 10

Buying a computer

A shop has three computer configurations: 1 – 48000 dinars, 2 – 42000 dinars, 3 – 58000 dinars. The program lets the user choose a configuration, and then prints the price in euros (take the rate 1 € = 117 dinars) and whether the user chose the most expensive, the middle or the cheapest option. For a wrong choice it prints "Wrong choice".

Example 1

Input

1

Output

Price: 410.26 EUR
Middle price

Example 2

Input

3

Output

Price: 495.73 EUR
The most expensive

Example 3

Input

7

Output

Wrong choice

Tips for solving

  1. First write down the initial values of all variables.
  2. Check the conditions in order and for each one write whether it is true or false.
  3. Follow only the branch that really runs; do not run both the if and the matching else.
  4. Check the boundary values especially carefully, such as 0, 1, 18 or a value equal to the boundary in the condition.
  5. With switch check whether the right case labels and default are there, and where the break statements are.
  6. Before handing in the solution, test typical cases, boundary values and values that do not belong to any special case.

Check your knowledge: mini quiz

Answer the questions and check each answer right after choosing it.

Open the quiz

Mini quiz: if, if–else and switch

1. What will the following code print if x = 7?

if (x > 10) cout << "A";
else if (x > 5) cout << "B";
else cout << "C";




2. Which condition checks whether the integer x is even?





3. Which expression checks whether age is in the range from 18 to 65 years, including both boundaries?





4. In nested if statements, which if does an else belong to if braces are not used to separate the blocks?





5. What will this code print when n = 3?

switch (n) {
    case 1: cout << "One"; break;
    case 2: cout << "Two"; break;
    case 3: cout << "Three"; break;
    default: cout << "Other";
}




6. What will the result be if x = 12?

if (x > 10) cout << "A";
else if (x > 0) cout << "B";
else cout << "C";