Chapter 1.4: Flowcharts

Chapter 1.4: Flowcharts · Study Questions
FlowchartSymbolsDrawing rulesSequential flowConditional flowNested decisionsRepeated flowBoundary errorsFrom flowchart to Java
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · Flowcharts

Question 1: A Limitation of Flowcharts

Which of the following is a flowchart limitation?

A: As detail grows, the number of symbols grows and the flowchart becomes unwieldy; in that case pseudocode is more practical. A flowchart is language-independent (B), shows branching and loops with decisions and back arrows (C), and makes errors easier to see before coding (D).

Multiple Choice · Symbols and Rules

Question 2: Which Symbol?

Which symbol is used to draw the step “Read the user's age” in a flowchart?

B: Reading data from the keyboard is an input operation: parallelogram. The rectangle is for computation and assignment, the diamond for a Yes/No question, and the hexagon for a fixed number of repetitions. Drawing a read step as a rectangle is a common mistake.

Multiple Choice · Symbols and Rules

Question 3: Drawing Rules

Which of the following goes against the flowchart drawing rules, that is, is not allowed?

C: A decision symbol must have two exits, both labeled Y/N; otherwise it is unclear what happens in the “no” case. The rule of a single Start and at least one End allows two Ends (D).

Coding Question · Symbols and Rules

Question 4: From Flowchart to Java

Convert the flowchart into a Java program. What is the output if the user enters 2?

StartRead rarea = 3.14 × r × rPrint areaEnd
Show answer
Scanner scanner = new Scanner(System.in);
double r = scanner.nextDouble();
double area = 3.14 * r * r;
System.out.println(area);

Parallelogram → nextDouble(), rectangle → assignment, print → println.

Start/End is the body of the main method. r = 2 → 3.14 · 2 · 2 = 12.56. The flow is sequential: no decisions or repetition, every box runs once.

Multiple Choice · Control Structures

Question 5: Three Structures

According to Böhm and Jacopini's structured programming theorem, every algorithm can be written by nesting which three structures?

A: Sequence (statements one after another), selection (if/else) and repetition (while/for) are enough. B describes a program's data flow, D describes concepts of object-oriented programming; they are not control structures.

Spot the Bug · Sequential Flow

Question 6: A Flowchart in the Wrong Order

The flowchart was drawn to print the sum of two numbers. What is the bug? What happens if it is translated into Java in the same order (after declaring int a, b;)?

Startsum = a + bRead a, bPrint sumEnd
Show answer

The addition is done before the numbers are read: if a step uses the result of a previous step, its position cannot be changed. The process box must be moved below “Read a, b”.

int a, b;
int sum = a + b;
a = scanner.nextInt();
b = scanner.nextInt();

Does not compile: error: variable a might not have been initialized (the same error for b). Java does not allow a variable that has no value to be used.

Step-by-Step Trace · Conditional Flow

Question 7: Nested Decisions: Letter Grade

The flowchart and its Java equivalent are run first with input 72, then 45. What is printed and which decisions are tested?

YNYNYNStartRead gradegrade ≥ 85?grade ≥ 70?grade ≥ 50?Print APrint BPrint CPrint FEnd

Start; the user enters 72.

YNYNYNStartRead gradegrade ≥ 85?grade ≥ 70?grade ≥ 50?Print APrint BPrint CPrint FEnd

Decision 1: 72 ≥ 85 is false → N exit, on to the next decision.

YNYNYNStartRead gradegrade ≥ 85?grade ≥ 70?grade ≥ 50?Print APrint BPrint CPrint FEnd

Decision 2: 72 ≥ 70 is true → Y exit.

YNYNYNStartRead gradegrade ≥ 85?grade ≥ 70?grade ≥ 50?Print APrint BPrint CPrint FEnd

“B” is printed and the flow merges at End. The third decision was never tested.

YNYNYNStartRead gradegrade ≥ 85?grade ≥ 70?grade ≥ 50?Print APrint BPrint CPrint FEnd

Second run: the user enters 45.

YNYNYNStartRead gradegrade ≥ 85?grade ≥ 70?grade ≥ 50?Print APrint BPrint CPrint FEnd

45 ≥ 85, 45 ≥ 70, 45 ≥ 50: all three are false, always the N exit.

YNYNYNStartRead gradegrade ≥ 85?grade ≥ 70?grade ≥ 50?Print APrint BPrint CPrint FEnd

“F” is printed. 2 decisions were tested for 72 and 3 for 45; in each run only one Print box executes.

int grade = scanner.nextInt();
if (grade >= 85) {
    System.out.println("A");
} else if (grade >= 70) {
    System.out.println("B");
} else if (grade >= 50) {
    System.out.println("C");
} else {
    System.out.println("F");
}

Output Question · Conditional Flow

Question 8: Negative Odd Number

In the odd/even flowchart the decision box is set up as “number mod 2 = 1?” and translated into Java as below. What is the output for the inputs −3, 7 and 0, in that order?

if (number % 2 == 1) {
    System.out.println(number + " odd");
} else {
    System.out.println(number + " even");
}
Show answer
-3 even
7 odd
0 even

In Java the remainder takes the sign of the dividend: −3 % 2 = −1; since −1 == 1 is false, −3 is taken to be “even”. 7 % 2 = 1, 0 % 2 = 0.

Fix: write the condition as number % 2 == 0 (even); then both −1 and 1 fall into the “odd” branch.

Multiple Choice · Conditional Flow

Question 9: Java Equivalent of a Flowchart

Which is the Java equivalent of the flowchart?

YNStartRead xx > 0?Print xEnd

D: Only the Yes branch of the decision box has work; the No branch goes straight to the merge point: a one-way decision, i.e. an if without else. There is no back arrow, so it is not a loop (A, C); the No branch has no “x = 0” box (B).

Step-by-Step Trace · Repeated Flow

Question 10: Repeated Flow: Multiplying by Three

What do the flowchart and its Java equivalent print? How many times does the loop body run?

YNStartnumber = 1number < 100?number = number × 3Print numberEnd

Initial value of the loop variable: number = 1.

YNStartnumber = 1number < 100?number = number × 3Print numberEnd

1 < 100 is true → Y: number = 1 × 3 = 3; the back arrow returns to the decision.

YNStartnumber = 1number < 100?number = number × 3Print numberEnd

3 < 100 is true → Y: number = 3 × 3 = 9; the back arrow returns to the decision.

YNStartnumber = 1number < 100?number = number × 3Print numberEnd

9 < 100 is true → Y: number = 9 × 3 = 27; the back arrow returns to the decision.

YNStartnumber = 1number < 100?number = number × 3Print numberEnd

27 < 100 is true → Y: number = 27 × 3 = 81; the back arrow returns to the decision.

YNStartnumber = 1number < 100?number = number × 3Print numberEnd

81 < 100 is true → Y: number = 81 × 3 = 243; the back arrow returns to the decision.

YNStartnumber = 1number < 100?number = number × 3Print numberEnd

243 < 100 is false → N: 243 is printed. The body ran 5 times and the decision 6 times; the final value is the first value that exceeds the limit.

int number = 1;
while (number < 100) {
    number = number * 3;
}
System.out.println(number);

Output Question · Repeated Flow

Question 11: Boundary Conditions

What do the three loops print? How many times does each run?

for (int i = 0; i < 10; i += 2)
    System.out.print(i + " ");
System.out.println();
for (int i = 0; i <= 10; i += 2)
    System.out.print(i + " ");
System.out.println();
for (int i = 10; i > 0; i -= 3)
    System.out.print(i + " ");
Show answer
0 2 4 6 8 
0 2 4 6 8 10 
10 7 4 1 

Loop 1 runs 5 times: when i = 10, 10 < 10 is false. Loop 2, because of <=, also prints 10: 6 times.

Loop 3 runs 4 times: 1 − 3 = −2 > 0 is false. Testing the condition with the final value catches an off-by-one error.

Multiple Choice · Repeated Flow

Question 12: Semicolon in a Loop Header

What happens when the code is run?

int counter = 1;
while (counter <= 10);
{
    System.out.print(counter + " ");
    counter++;
}

B: while (counter <= 10); is a loop with an empty body; the counter never increases, so the condition stays true and the program hangs. The code compiles (C is wrong); the braced block is not part of the loop and never runs.

Multiple Choice · Repeated Flow

Question 13: The Back Arrow in the Wrong Place

In the flowchart that prints 1 to 10, the back arrow is connected not to the decision box but to the top of the “counter = 1” box. What happens?

C: In every round the initialization box runs again and the counter is reset to 1; 1 ≤ 10 stays true forever: an infinite loop. The back arrow must return to the decision box, after the initial value.

Coding Question · Repeated Flow

Question 14: From the Loop Symbol to for

The three parts of the hexagon loop symbol are initialization, condition and update. Convert the flowchart into Java; what is the output?

doneStarti = 5; i ≥ 1; i--Print i × iEnd
Show answer
for (int i = 5; i >= 1; i--) {
    System.out.print(i * i + " ");
}

Output: 25 16 9 4 1. The initialization runs once, the condition runs before every round (6 times), the update runs after every round (5 times). The same loop with while: int i = 5; while (i >= 1) { … i--; }

Step-by-Step Trace · Practice

Question 15: Sum Until 0 Is Entered

The user enters 5, 8, −3 and 0 in turn. What does the program print?

input:58-30

Before the loop the first number is read: 5.

input:58-30

5 ≠ 0 → sum = 5, count = 1; the next number is read: 8.

input:58-30

8 ≠ 0 → sum = 13, count = 2; the next number is read: -3.

input:58-30

-3 ≠ 0 → sum = 10, count = 3; the next number is read: 0.

input:58-30

0 ≠ 0 is false: the loop ends. 0 is not added to the sum; while was chosen because the number of repetitions depends on the input.

int sum = 0, count = 0;
int number = scanner.nextInt();
while (number != 0) {
    sum += number;
    count++;
    number = scanner.nextInt();
}
System.out.println(sum + " / " + count);

Coding Question · Practice

Question 16: Multiples of Three

Write the program (algorithm first) that prints the sum and the count of the numbers from 1 to n that are divisible by 3. What is the output for n = 20?

Show answer

sum = 0, count = 0 → for i = 1 to n: if i mod 3 = 0 then sum += i, count += 1 → print. There is a decision inside the loop.

int sum = 0, count = 0;
for (int i = 1; i <= n; i++) {
    if (i % 3 == 0) {
        sum += i;
        count++;
    }
}
System.out.println(sum + " " + count);

n = 20: 3 + 6 + 9 + 12 + 15 + 18 = 63, count 6; the output is 63 6. A shorter way: start at i = 3 and step with i += 3.

Multiple Choice · Practice

Question 17: Starting Value of the Accumulator

In the flowchart that sums 1 to n, the “sum = 0” box was mistakenly written as “sum = 1”. What is the output for n = 4?

D: The loop still adds 1 + 2 + 3 + 4 = 10, but since it starts at 1 the result is 11. The accumulator starts at 0 for addition and at 1 for multiplication; the compiler does not catch this logic error.

The End

Chapter 1.4: Flowcharts: Study Questions
Lecture slides: Chapter 1.4: Flowcharts · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.