WorksheetsQuiz on Accumulators and Sentinel Values
Total questions: 10
Worksheet time: 10mins
A(n) _____ is a sum of numbers that accumulates with each iteration of a loop. The variable used to keep the running total is called a(n) _____.
gross; net
net; gross
accumulator; running total
earnings; balance
running total; accumulator
A(n) _____ value is a special value that cannot be mistaken as a member of the list, and signals that there are no more values to be entered. When the user enters the sentinel value, the loop terminates.
sentinel
bullseye
signal
sentry
sargent
The logic for calculating a sum of numbers generally requires the accumulator variable to be set to _____ otherwise the total may not be correct when the loop finishes.
1
10
-1
2
0
A sentinel value is always included (summed) in the running total of an app that accumulates.
True
False
The best choice for a sentinel used in a program that sums the number of free throws made in an NBA playoff basketball game is 3 (assume Shaq is not playing).
True
False
-1 is not an ideal choice for a sentinel used to sum the number of days it is hot in the Sahara desert.
True
False
Write a for loop that repeats four times, asking the user to enter a lottery ticket powerball number. Use the variable powerball in your code/response that stores the input value from keyboard.nextInt(). The loop should also calculate the sum of the value numbers entered (like the sum of 32 + 6 + 23 + 19). Assume all related code not provided prior to Scanner is valid.
In the following program segment, which variable is the "loop control variable" (also known as the counter variable)? int a, r = 0, g = 0; while (r < 21) { g = r * 2; a += g; r++; } System.out.println("The sum is " + g);
a
r
g
In the following program segment, which is a VALID Java accumulator? int a, r, h = 1; g = 0; while (r < 21) { a = r * 2; g += a + h; r--; } System.out.println("The sum is " + g);
a
r
g
sum
args
Why should you be careful when choosing a sentinel value? Note: Do NOT provide a reason that includes the computer crashing or memory storage/overflow.
