Chapter 4.2: Flowcharts
Multiple Choice · Symbols
Question 1: Symbol and Task
Which of the following symbol – content pairings is wrong?
B: sum = a + b is a computation/assignment and is written in the rectangle (process) symbol. The parallelogram is only for input/output (e.g. “print the sum”). A question goes in the decision symbol, a command in the process symbol.
Multiple Choice · Symbols
Question 2: Readability
A student wrote “compare a with b and print the larger” inside a decision symbol and did not label the two arrows leaving the decision. Which is the correct fix?
A: The rules: one basic action per symbol, decision exits with meaningful labels (Yes/No), and flow shown with arrows. With an unlabeled decision it stays unclear which path is chosen under which condition.
Output Question · Sequential Flow
Question 3: Swapping Without a Temporary
What is the output if the flowchart below is run with the inputs a = 4, b = 7?
Show answer
Output: 7 4
| step | a | b |
|---|---|---|
| read | 4 | 7 |
| a = a + b | 11 | 7 |
| b = a − b | 11 | 4 |
| a = a − b | 7 | 4 |
The sum stores both values together; the swap is done without a temporary variable. In sequential flow each box runs exactly once.
Step-by-Step Trace · Sequential Flow
Question 4: Trace a Sequential Flow
The diagram is traced with the input a = 3. What are the variables after each box?
Start. There are no variables yet.
Input: a = 3.
b = 3 × 3 = 9.
c = 9 − 3 = 6.
a = 6 + 9 = 15. The old value of a (3) is gone; b and c do not change.
Output: 15 9 6 is printed.
End. There is no branching or repetition: each box ran once.
Start read a b = a × a c = b - a a = c + b print a, b, c End
Output Question · Conditional Flow
Question 5: Letter Grade
The required table: 90–100 A, 80–89 B, 70–79 C, 60–69 D, 50–59 E, 0–49 F. The diagram of the proposed code is on the side. If the diagram is run, what does it print for grade = 85, 60 and 55? Does the diagram match the table?
Show answer
85 → B · 60 → D · 55 → F
- 85: 85 ≥ 90 No, 85 ≥ 80 Yes → B.
- 60: the first three decisions are No, 60 ≥ 60 Yes → D (the boundary value is on the right branch).
- 55: all four decisions are No → F.
Bug: the “50–59: E” branch was forgotten; E should have been printed for 55. Fix: add the decision grade >= 50 ? on the No branch of the “grade ≥ 60 ?” decision; Yes → print "E", No → print "F".
Because the decisions are asked from largest to smallest, each decision checks only the lower bound.
Multiple Choice · Conditional Flow
Question 6: Boundary Value Test
In the grade diagram the decision “grade >= 60 ?” was mistakenly written as “grade > 60 ?” (Yes → “Pass”, No → “Fail”). Which test value exposes this bug?
D: 60 is the boundary value: in the correct diagram 60 >= 60 is true → “Pass”; in the faulty diagram 60 > 60 is false → “Fail”. For 72, 45 and 100 the two diagrams give the same result. That is why boundary values are tested separately.
Step-by-Step Trace · Conditional Flow
Question 7: The Smallest of Three Numbers
The diagram is traced for a = 8, b = 3, c = 5. Which paths are followed and what is printed?
The three numbers are read.
The first candidate is the smallest: min = a = 8.
Decision: 3 < 8 → true → Yes.
min = b = 3. The path rejoins the main flow at the connector.
Decision: 5 < 3 → false → No; min does not change.
3 is printed.
End. Each decision was asked only once; the method stays the same even if there are more numbers.
Start read a, b, c min = a b < min ? Yes → min = b c < min ? Yes → min = c print min End
Output Question · Conditional Flow
Question 8: Nested Decision
The diagram is run in turn for a = 6, a = 0, a = −3 and a = 7. What is printed in each run?
Show answer
| a | a > 0 ? | a % 2 == 0 ? | output |
|---|---|---|---|
| 6 | Yes | Yes | Even |
| 0 | No | not asked | None |
| −3 | No | not asked | None |
| 7 | Yes | No | Odd |
- The second decision exists only on the Yes branch of the first: when a ≤ 0, odd/even is never asked.
- Although 0 is even, “None” is printed, because it is not positive.
- At least three inputs are needed to try all three output boxes (the four branches of the two decisions), such as 6, 7 and 0.
Multiple Choice · Conditional Flow
Question 9: Exits of a Decision Symbol
What is the output of the flowchart if the input is x = 7?
D: 7 mod 2 = 1 (the remainder of 7 divided by 2) → the answer to "x mod 2 = 0 ?" is No → left branch: odd. Two paths leave the decision (diamond) symbol; only one is followed in each run. If x were 12, "even" would be printed.
Output Question · Repeated Flow
Question 10: A Counter Increasing by Three
What is the output of the diagram? How many times is the decision asked, and what is i when the loop ends?
Show answer
Output: 1 4 7 10
| i | 1 | 4 | 7 | 10 | 13 |
|---|---|---|---|---|---|
| i ≤ 10 ? | Yes | Yes | Yes | Yes | No |
| printed | 1 | 4 | 7 | 10 | — |
The body runs 4 times and the decision is asked 5 times (the last attempt is false). The loop is left with i = 13. Since 10 ≤ 10 is true, 10 is printed as well.
Multiple Choice · Repeated Flow
Question 11: A Counter That Is Not Incremented
What happens when the diagram below is run?
A: There is no action on the repeat path that changes the condition; i stays 1 and 1 ≤ 10 is always true → an infinite loop. Fix: add a process box i = i + 1 after the “print i” box.
Output Question · Repeated Flow
Question 12: Initial Value 10
The flowchart of the for loop is on the side. What is the output?
Show answer
Output: 25
| i | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| x (end of round) | 11 | 13 | 16 | 20 | 25 | exit |
- x is an accumulator, but it starts from 10, not 0: 10 + (1+2+3+4+5) = 10 + 15 = 25.
- The print box is outside the loop (on the No branch): only the final value is printed, once.
If it had started with x = 0, the output would be 15.
Output Question · Repeated Flow
Question 13: Until Zero Is Entered
The user enters 5, 8, −2, 0 in turn. What does the diagram print?
Show answer
Output: 11 3
| x read | x == 0 ? | sum | count |
|---|---|---|---|
| 5 | No | 5 | 1 |
| 8 | No | 13 | 2 |
| −2 | No | 11 | 3 |
| 0 | Yes | leave the loop | |
- The end marker (0) does not take part in the sum and the count: the decision is asked before the addition.
- The returning arrow goes back to the box read, not to the decision: a new number is read in each round.
Step-by-Step Trace · Repeated Flow
Question 14: Condition at the End: Number of Digits
The diagram of the number-of-digits method written with do-while is traced for number = 4096 (number is an integer; / is integer division, the fraction is discarded).
Input 4096; the counter digits = 0.
The body runs without asking the condition: digits = 1, number = 4096 / 10 = 409.
409 > 0 → Yes: go back to the start of the body.
digits = 2, number = 40. 40 > 0 → Yes.
digits = 3, number = 4. 4 > 0 → Yes.
digits = 4, number = 4 / 10 = 0. 0 > 0 → No.
4 is printed. Because the body comes before the condition, 1 would be printed even if number = 0 were entered (0 has one digit).
Start read number digits = 0 digits = digits + 1 number = number / 10 number > 0 ? Yes → go back to line 4 print digits End
Multiple Choice · Repeated Flow
Question 15: Decision First or Last?
Which statement is true for the diagrams I and II below?
B: In I the decision is asked before the body (while): if the condition is false from the start, the body never runs. In II the body runs first and the decision is asked at the end (do-while): the body runs at least once. In the lecture, input validation (a number from 1 to 5) had form II.
Output Question · Repeated Flow
Question 16: A Flowchart with a Loop
What is printed on the screen when the algorithm in the flowchart is run? How many times does the loop run?
Show answer
Output: 9 · the loop body runs 3 times.
| i ≤ 5 ? | s ← s + i | i ← i + 2 |
|---|---|---|
| 1 ≤ 5 Yes | 0 + 1 = 1 | 3 |
| 3 ≤ 5 Yes | 1 + 3 = 4 | 5 |
| 5 ≤ 5 Yes | 4 + 5 = 9 | 7 |
| 7 ≤ 5 No | — | — |
- The returning arrow shows the loop; since i grows by 2 in each round, the condition eventually becomes false.
- s is the sum of the odd numbers up to 5: 1 + 3 + 5.
Step-by-Step Trace · Repeated Flow
Question 17: Factorial with while
The factorial solution (pseudocode) is run with n = 4. What are result and i in each round?
The starting value for a product is 1 (if it were 0, the result would always be 0).
1 <= 4 → round 1.
result = 1 × 1 = 1, i = 2.
2 <= 4 → round 2.
result = 1 × 2 = 2, i = 3.
3 <= 4 → round 3.
result = 2 × 3 = 6, i = 4.
4 <= 4 → round 4.
result = 6 × 4 = 24, i = 5.
5 <= 4 is false → the loop ends.
4! = 24. The body ran 4 times and the condition 5 times.
result = 1 i = 1 while i <= n result = result * i i = i + 1 print(result)
Output Question · Repeated Flow
Question 18: How Many Times Does the Loop Run?
How many times does the loop body run, and what is the output?
i = 1 count = 0 while i <= 20 i = i * 3 count = count + 1 print(count, i)
Show answer
Output: 3 27
| condition | i (after) | count |
|---|---|---|
| 1 <= 20 ✓ | 3 | 1 |
| 3 <= 20 ✓ | 9 | 2 |
| 9 <= 20 ✓ | 27 | 3 |
| 27 <= 20 ✗ | exit | |
Because the loop variable grows in each round, the condition eventually becomes false. Even though i exceeds 20, the last round is completed; the condition is checked only at the start of a round.
Multiple Choice · Repeated Flow
Question 19: Odd Numbers from 1 to 100
The pseudocode of the odd-number solution is below. How many numbers are printed, and which is the last one printed?
for i = 1 to 100
if i mod 2 == 1 then
print(i)B: i mod 2 == 1 is true only for odd numbers: 1, 3, …, 99 → 50 numbers. 100 is even and is not printed. The loop runs 100 times, but the condition is true in only half of them. (In the even-number version the condition is i mod 2 == 0; 2…100, again 50 numbers.)
Step-by-Step Trace · Nested Loops
Question 20: continue and break Together
The outer loop has continue and the inner loop has break. Which (i, j) pairs are printed?
i = 1: 1 == 3 is false, so the inner loop is entered.
j = 1: 1 == 3 is false → printed.
j = 2 → printed.
j = 3 → break: the inner loop ends completely, j = 4 is never tried.
i = 2: in the same way j = 1 and j = 2 are printed.
j = 3 → break.
i = 3 → continue: the inner loop does not run at all in this round, and execution goes directly to i = 4.
i = 4: j = 1 and 2 are printed, and the loop is exited at j = 3.
i = 5 > 4 → the outer loop ends. 6 lines.
for i = 1 to 4 if i == 3 then continue for j = 1 to 4 if j == 3 then break print "i: " + i + " j: " + j
Multiple Choice · Nested Loops
Question 21: How Many Times Does the Inner Body Run?
How many * are printed in total in the nested loop below?
for i = 1 to 4 for j = 1 to i print "*" newline
D: The bound of the inner loop depends on the outer counter: 1 time when i = 1, 2 when i = 2, 3 when i = 3, 4 when i = 4 → 1 + 2 + 3 + 4 = 10 (a triangle pattern). 16 = 4 × 4 would be correct if the inner loop ran 4 times every time.
Output Question · Nested Loops
Question 22: Star and Dash Pattern
The pseudocode of the method draw() is below. What is the output?
for i = 0 to 4 for j = 0 to i print "*" for j = 0 to 4-i print "-" newline
Show answer
*----- **---- ***--- ****-- *****-
- The first inner loop, j = 0…i: i + 1 stars.
- The second inner loop, j = 0…4−i (in Java
j < 5 - i): 5 − i dashes. - Each line has 6 characters; 5 lines.
Multiple Choice · Translating to Code and Testing
Question 23: From Diagram to Code
Which is the correct Java equivalent of the diagram on the side? (Assume n has been read and sum = 0, i = 1 have been assigned.)
C: A decision + a returning arrow = a while condition and its body; the two boxes on the Yes path are inside the body. A runs only once because there is no way back. In B the increment of i stays outside the loop, so it becomes an infinite loop. In D the condition is <, so n is not added.
Multiple Choice · Translating to Code and Testing
Question 24: Inputs for Testing a Loop
Which values of n should be chosen to test the “sum from 1 to n” diagram with cases where the loop runs zero, one and many times?
A: For n = 0, 1 ≤ 0 is false and the body never runs (expected 0); for n = 1 it runs once (expected 1); for n = 5 many times (expected 15). B and C test only the “many” case; D has no case with many repetitions.
Output Question · Translating to Code and Testing
Question 25: Number Guessing Game
In the number guessing game from the lecture the secret number is 37; the player enters 20, 40, 37 in turn. What appears on the screen?
Start secret = 37, attempts = 0 read guess attempts = attempts + 1 guess == secret ? Yes → print "Correct (attempt " attempts ")", End guess < secret ? Yes → print "Higher" No → print "Lower" go back to line 3 (read)
Show answer
Higher Lower Correct (attempt 3)
| guess | attempt | == 37 | < 37 |
|---|---|---|---|
| 20 | 1 | No | Yes → higher |
| 40 | 2 | No | No → lower |
| 37 | 3 | Yes | not asked |
The hint is given relative to the secret number: if the guess is too small, a “Higher” number must be entered. The number of rounds is not known in advance; the loop ends when the condition becomes true.