THINK FIRST·CODE LATER

← Java Programming
Chapter 2 · Week 2

Algorithm Design: Pseudocode, Flowcharts, and Tracing

Answered 0/39 Correct 0
Sign in to save progress across devices
Q1

Which property must every algorithm have?

Q2

In a flowchart, a decision is represented by a:

Q3

Which flowchart symbol represents reading a value from the keyboard?

Q4

The oval (rounded) symbol in a flowchart indicates:

Q5

How many flow lines leave a decision symbol in a standard flowchart?

Q6

Which of the following is not one of the three fundamental control structures?

Q7

In pseudocode, total <- total + mark means:

Q8

What is printed by this fragment?

a <- 4
b <- 7
a <- b
b <- a
print a, b
Q9

Which sequence correctly swaps the contents of x and y?

Q10

Trace the fragment and give the output (/ is integer division).

count <- 0
n     <- 20
while n > 1 do
    n     <- n / 2
    count <- count + 1
endwhile
print count
Q11

What is the final value of sum?

sum <- 0
for i <- 1 to 5 do
    if i mod 2 = 0 then
        sum <- sum + i
    endif
endfor
Q12

Assume that for i <- 1 to 10 behaves like the equivalent while loop: i starts at 1, is tested against 10 before each pass and is increased by 1 at the end of each pass. After the loop finishes, the value of i is:

Q13

A loop that must repeat exactly 30 times is best described as:

Q14

You ask the user for marks and stop when the user types −1. This is:

Q15

Which value would be a bad choice of sentinel for a list of Celsius temperatures?

Q16

A boolean variable found, set inside a search loop and tested by the loop condition, implements:

Q17

Which fragment is an infinite loop?

Q18

How many times does the inner body execute in total?

for i <- 1 to 4 do
    for j <- 1 to 3 do
        print i, j
    endfor
endfor
Q19

What is the output of this nested loop?

for i <- 1 to 3 do
    for j <- 1 to i do
        print "*"
    endfor
    newline
endfor
Q20

Where should the accumulator total be initialised to zero?

Q21

Consider average <- total / count. What must be checked before this statement?

Q22

What is the output?

x <- 1
while x <= 16 do
    print x
    x <- x * 2
endwhile
Q23

A loop whose body must execute at least once before the condition is tested is drawn with the decision:

Q24

Which of these is a logic error rather than a notation error?

Q25

Stepwise refinement means:

Q26

What is printed?

n <- 5
result <- 1
while n > 0 do
    result <- result * n
    n <- n - 1
endwhile
print result
Q27

Which algorithm does the previous fragment implement?

Q28

What does this algorithm compute?

best <- first value
while more values remain do
    input v
    if v > best then best <- v endif
endwhile
print best
Q29

In a trace table, each row usually represents:

Q30

Which change makes the loop print only the even numbers from 2 to 10?

Q31

What is the output?

count <- 0
for i <- 1 to 3 do
    for j <- 3 downto 1 do
        if i = j then count <- count + 1 endif
    endfor
endfor
print count
Q32

Off-by-one errors most often come from:

Q33

An algorithm is said to be definite when:

Q34

Which statement about pseudocode is true?

Q35 Short answer

Build a complete trace table for this fragment and state the output.

a <- 2
b <- 3
for k <- 1 to 3 do
    a <- a + b
    b <- b + 1
endfor
print a, b
Q36 Short answer

Write pseudocode that reads marks one at a time until the user enters −1, then prints how many marks were entered and their average. Use a sentinel-controlled loop and guard against division by zero.

Q37 Short answer

Describe, symbol by symbol, the flowchart of an algorithm that reads a number and prints whether it is positive, negative or zero.

Q38 Short answer

Explain the difference between a counter-controlled and a user-query loop, and give one situation where each is the better choice.

Q39 Programming

Translate your pseudocode from question 36 into a complete Java program. (If you have not yet studied Scanner and while loops, return to this question after Chapter 7.)