You are on page 1of 12

Computer Programming Name: _________________

Midterm Study Guide


1. Many seasoned programmers start counter values at 1 because they are used to doing so when
working with arrays. Pg 308
a. True
b. False

2. A ____ is a structure that allows repeated execution of a block of statements. Pg 302


a. Body c. Loop
b. Boolean expression d. Loop control

3. Use a(n) ____ loop to execute a body of statements continually as long as the Boolean
expression that controls entry into the loop continues to be true. Pg 303
a. Empty c. Infinite
b. While d. Control

4. A loop that never ends is called a(n) ____ loop. Pg 303

5. Before entering a loop, the first input statement, or ____, is retrieved. Pg 311
a. Empty body c. Coop body
b. Posttest loop d. Priming read

6. It is important that the loop control ____ be altered within the body of the loop. Pg 303
a. Value c. Constant
b. Variable d. Argument

7. Which is an infinite loop? Pg 303-304


a. loopCount = 5; c. loopCount = 4;
while(loopCount > 3) while(loopCount < 3)
{ {
System.out.println("Hello"); System.out.println("Hello");
loopCount = loopCount - 1; loopCount = loopCount + 1;
} }
b. loopCount = 1; d. loopCount = 1;
while(loopCount < 3) while(loopCount < 3)
{ {
System.out.println("Hello"); System.out.println("Hello");
} loopCount = loopCount + 1;
}
8. An indefinite loop is a(n) ____ loop. Pg 308
a. Counter-controlled c. Initialized
b. Event-controlled d. Validating

9. A loop controlled by the user is a type of ____ loop. Pg 308-309


a. Indefinite c. Counter-controlled
b. Definite d. Incrementing

10. ____ is the process of ensuring that a value falls within a specified range. Pg 310
a. Value checking c. Validating data
b. Data integrity d. A range check
11. The process of repeatedly increasing a value by some amount is known as ____. Pg 314
a. Checking c. Accumulating
b. Adding d. Containing

12. When you want to increase a variable’s value by exactly 1, use the ____. Pg 314
a. Power Statement c. Binary operators
b. Accumulating operator d. Prefix increment operator

13. You use a unary minus sign preceding a value to make the value ____. Pg 315
a. Negative c. Valid
b. Positive d. Constant

14. A(n) ____ loop is a special loop that is used when a definite number of loop iterations is required.
Pg 319
a. While c. Else
b. For d. do...while

15. A for loop provides a convenient way to create a(n) ____ loop. Pg 319
a. Counter-controlled c. Posttest
b. While d. Infinite

16. You can initialize more than one variable in a for loop by placing a(n) ____ between the separate
statements. Pg 320
a. Semicolon c. Period
b. Equal sign d. Comma

17. The ____ loop checks the value of the loop control variable at the bottom of the loop after one
repetition has occurred. Pg 325
a. While c. For
b. Do...while d. Else

18. In a do…while loop, the loop will continue to execute until ____. Pg 302
a. The loop control variable is true c. The user types EXIT
b. The loop control variable is false d. The program terminates

19. How many times will outputLabel be called? Pg 330


for(customer = 1; customer <= 20; ++customer)
for(color = 1; color <= 3; ++color)
outputLabel();
a. 0 c. 20
b. 3 d. 60

20. The order of the conditional expressions in the following is most important within a(n) ____ loop.
Pg 334
while(requestedNum > LIMIT || requestedNum < 0)…
a. Nested c. Pretest
b. Posttest d. Indefinite
21. In the expressions b = 8 and c = --b, what value will be assigned to the variable c? Pg 316
a. 8 c. 9
b. 7 d. 10

22. When creating a for loop, which statement will correctly initialize more than one variable? Pg
320-321
a. For a=1, b=2 c. For (a=1; b=2)
b. For (a=1, b=2) d. For (a = 1 && b = 2)

23. Which of the following is NOT a valid method to increase a variable named score by 1? Pg
314-315
a. ++score c. ++score = score +1
b. score++ d. score = score + 1

24. One execution of any loop is called a(n) ____________________. Pg 302

25. A(n) ____________________ is a body with no statements in it. Pg 306

26. The ____________________ loop is the posttest loop used in Java. Pg 325

27. When loops are nested, each pair contains a(n) ____________________ loop and an outer loop.
Pg 328

Match each term with the correct statement below. Pg 314, 302, 303, 315, 336, 307, 319, 323
a. prefix ++ f. Loop fusion
b. Block g. Decrementing
c. definite loop h. For loop
d. loop control variable i. sleep() method
e. Binary operators

28. Multiple statements within curly braces

29. The value that determines whether loop execution continues

30. Subtracting 1 from a variable

31. A countered loop

32. A shortcut for incrementing and accumulating

33. Operate on two values

34. Within parentheses are three sections separated by exactly two semicolons

35. Part of the Thread class in the java.lang package

36. The technique of combining two loops into one


Subjective Short Answer
37. Describe how a loop is controlled by a Boolean expression. Pg 302

38. How could a programmer identify and escape from an infinite loop? Pg 304

39. What would happen if a semicolon is mistakenly placed at the end of a partial statement of a while
loop with a Boolean expression and with an empty body? Pg 306

40. Why would a loop with altered user input be considered a type of indefinite loop? Give an
example. Pg 308

41. What are some of the shortcuts Java provides programmers for incrementing and accumulating?
Give examples of statements. Pg 314

42. How does a for loop work? Pg 319

43. What are some unconventional uses of the three sections inside the parentheses of a for loop?
Show at least three examples using code. Pg 320-321

44. Why would a programmer use curly braces in the body of a do…while loop? Pg 327

45. How are nested loops implemented in a loop structure? Pg 328


46. How could you rewrite the following code to have the arithmetic performed only once, even if the
loop executes 1,000 times? Pg 333
while (x < a + b)
// loop body

47. Provide a code example of a pretest loop and an example of a posttest loop. Pg 325

48. Describe a counter-controlled loop. Explain the process of incrementing and decrementing, and
explain the best method for executing a loop a specific number of times. Pg 307-308

49. Explain why an infinite loop might not actually execute infinitely. Pg 304

50. How are indefinite loops used for validating data? Why is a loop structure typically the better
choice for validating data than an if statement? Pg 310-312

51. Using the flowchart and code above, describe the execution of the loop. Identify the loop control
variable and the value of the variable as the code is executed. Pg 303

52. while(10 > 1)


{
System.out.println("This prints forever.");
}
Identify the problem that exists in the above while loop. Pg 304
53. counterLoop = 1;
while(counterLoop < 10);
{
System.out.println("Hello");
counterLoop = counterLoop + 1;
}​
What is the problem in the above while loop? How would you correct the problem? Pg 307

54. Using the information provided above, write the while loop that will correctly follow the execution
of the flowchart. Pg 305

55. Write a definite while loop that initializes a loop control variable named decreaseOne to 10 and
continues while decreaseOne > 0. Decrement the loop control variable by 1 and include the println
output “Keep going” within the loop. Pg 307

56. Create an indefinite while loop that will validate that the loop control variable named userNum is
less than the constant MAXVALUE. While true, create a println statement to output “Please enter a
higher value”. Get user input of another value for userNum after each println statement. Once
MAXVALUE is reached, create a final println statement that will output “Max value reached”. Pg 311
57. public class IncrDemo
{
public static void main(String[] args)
{
int myVal, yourVal;
myVal = 10;
System.out.println("My initial value is " + myVal);
yourVal = ++myVal;
System.out.println("My new value is " + myVal);
System.out.println("Your value is " + yourVal):
}
}​
Using the above code, describe how the three println output commands will appear. Explain the
values stored in the variables during code execution. Pg 314-316

58. What is loop fusion? Write a code segment that uses loop fusion to call two methods, method1
and method2, 10 times each. Pg 336

59. while(count < getNumberOfEmployees())​


Examine the statement above. Write the code that will result in more efficient program execution
assuming the result of getNumberOfEmployees() stays the same. Pg 333
60. public class DoWhileExample
{
public static void main(String[] args)
{
int currentValue;
____
____
____
____
____
____
}
}
Complete the code above by writing a do…while posttest loop that will output the value of the variable
currentValue in a println statement. After the output statement, add a decrement statement to
decrease the value of currentValue by 1. Continue the loop until currentValue is equal to 0. Pg 327

61. ​What are the three sections inside the parentheses of a for loop typically used for? Pg 319
Answer Key

1. False 13. A. 25. empty body


2. C 14. B 26. do while
3. B 15. A 27. inner
4. Infinite 16. D 28. B
5. D 17. B 29. D
6. B 18. B 30. G
7. B 19. D 31. C
8. B 20. A 32. A
9. A 21. B 33. E
10. C 22. B 34. H
11. C 23. C 35. I
12. D 24. iteration 36. F

37. Within a looping structure, a Boolean expression is evaluated. If it is true, a block of statements
called the loop body executes and the Boolean expression is evaluated again. As long as the
expression is true, the statements in the loop body continue to execute. When the Boolean evaluation
is false, the loop ends.

38. You might suspect an infinite loop if the same output is displayed repeatedly, or if the screen
simply remains idle for an extended period of time without displaying expected output. If you think
your application is in an infinite loop, you can press and hold the Ctrl key, and then press C or the
Break key; the looping program should terminate.

39. If a semicolon is mistakenly placed at the end of the partial statement while (loopCount < 3);, the
loop is also infinite. This loop has an empty body, or a body with no statements in it. So, the Boolean
expression is evaluated, and because it is true, the loop body is entered. Because the loop body is
empty, no action is taken and the Boolean expression is evaluated again. Nothing has changed, so it
is still true, the empty body is entered, and the infinite loop continues.

40. An event-controlled loop is a type of indefinite loop because you don’t know how many times it will
eventually repeat. Perhaps you want to continue asking a user questions as long as the response is
correct. In this case, while you are writing the program, you do not know whether the loop eventually
will be executed two times or 200 times, or forever.

41. The statement count += 1; is identical in meaning to count = count + 1. The += is the add and
assign operator; it adds and assigns in one operation. Similarly, balance += balance * INT_RATE;
increases a balance by the INT_RATE percentage. Besides using the shortcut operator +=, you can
use the subtract and assign operator ( -= ), the multiply and assign operator ( *= ), the divide and
assign operator ( /= ), and the remainder and assign operator ( %= ). Each of these operators is used
to perform the operation and assign the result in one step.

42. A for loop is a special loop that is used when a definite number of loop iterations is required; it
provides a convenient way to create a counter-controlled loop. Although a while loop can also be
used to meet this requirement, the for loop provides you with a shorthand notation for this type of
loop. When you use a for loop, you can indicate the starting value for the loop control variable, the
test condition that controls loop entry, and the expression that alters the loop control variable—all in
one convenient place.

43. Initialization of more than one variable int the first section by placing commas between the
separate statements, as in the following:
for(g = 0, h = 1; g < 6; ++g)
Performance of more than one test using compound conditions in the second section, as in the
following:
for(g = 0; g < 3 && h > 1; ++g)
Decrementation or performance of some other task in the third section, as in the following:
for(g = 5; g >= 1; --g)
Performing multiple actions in the third section, separated by commas, as in the following:
for(g = 0; g < 10; ++g, ++h, sum += g)
You can leave one or more portions of a for loop empty, although the two semicolons are still required
as placeholders. For example, if x has been initialized in a previous program statement, you might
write the following:
for(; x < 10; ++x)
Declare a variable within a for statement, as in the following:
for(int val = 1; val < 11; ++val)
You might use method calls in any section of the for statement, as in the following example. Here, the
isFinished() method would be required to return a Boolean value and the alter() method would be
required to return a data type accepted by x.
for(x = initMethod(); isFinished(); x = alter(x))

44. When the body of a do…while loop contains a single statement, you do not need to use curly
braces to block the statement. Even though curly braces are not required in this case, many
programmers recommend using them. Doing so prevents the third line of code from looking like it
should begin a new while loop.

45. Just as if statements can be nested, so can loops. You can place a while loop within a while loop,
a for loop within a for loop, a while loop within a for loop, or any other combination. When loops are
nested, each pair contains an inner loop and an outer loop. The inner loop must be entirely contained
within the outer loop; loops can never overlap.

46. int sum = a + b;


while(x < sum)
// loop body

47. In a pretest loop, the control variable is evaluated at the top of the loop before the body is
executed:
for(x = 0; x < 100000; ++x)
{
... code here…
}

In a posttest loop, the loop control variable is tested after the loop body executes:
do
{
total += numberValue;
}while(total < 200);
48. In a counter-controlled loop, a loop control variable is changed by counting. Incrementing the
variable is a common way to alter the value of a loop control variable by adding 1 to it. In
decrementing, the loop is controlled by subtracting 1 from a loop control variable.

To execute a loop a specific number of times, the clearest and best method is to start
the loop control variable at 0 or 1, increment by 1 each time through the loop, and stop
when the loop control variable reaches the appropriate limit.

49. An infinite loop may perform tasks that will eventually lead to a computer running out of memory,
causing execution to stop. Also, it’s possible
that the processor has a time-out feature that forces the loop to end.

50. Programmers commonly use indefinite loops when validating input data. Validating data is
the process of ensuring that a value falls within a specified range. For example, suppose you
require a user to enter a value no greater than 3. If the user enters 3 or less at the first prompt, the
loop never executes. However, if the user enters a number greater than 3, the shaded loop executes,
providing the user with another chance to enter a correct value. While the user continues to enter
incorrect data, the loop repeats.

Novice programmers often make the mistake of checking for invalid data using a decision instead of a
loop. That is, they ask whether the data is invalid using an if statement; if the data is invalid, they
reprompt the user. However, they forget that a user might enter incorrect data multiple times. Usually,
a loop is the best structure to use when validating input data.

51. To write a definite loop, you initialize a loop control variable, a variable whose value determines
whether loop execution continues. While the Boolean value that results from comparing the loop
control variable and another value is true, the body of the while loop continues to execute. In the body
of the loop, you must include a statement that alters the loop control variable. The code segment
displays the series of integers 1 through 10. The variable lcv is the loop control variable and starts the
loop holding a value of 1; and while the value remains under 11, lcv continues to be output and
increased.

52. The above code is an infinite loop. The expression 10 > 1 will always evaluate to true. Since this
expression is part of a while loop, the body of the loop is entered and “Enter a new value” is
displayed. Next, the expression is evaluated again. The expression 10 > 1 is still true, so the body is
entered again. “Enter a new value” is displayed repeatedly. The loop never finishes because 10 > 1 is
never false.

53. The placement of the statement-ending semicolon is important when you work with the while
statement. In the code, a semicolon is mistakenly placed at the end of the partial statement
while(counterLoop < 10). This makes the loop infinite. This loop has an empty body, or a body with no
statements in it. The Boolean expression is evaluated; and because it is true, the loop body is
entered. Because the loop body is empty, no action is taken, and the Boolean expression is evaluated
again. Nothing has changed, so it is still true, the empty body is entered, and the infinite loop
continues.

54. loopCount = 1;
while(loopCount < 10)
{
System.out.println("Welcome");
loopCount = loopCount + 1;
}

55. decreaseOne = 10;


while(decreaseOne > 0)
{
System.out.println("Keep going");
decreaseOne = decreaseOne - 1;
}

56. while (userNum < MAXVALUE)


{
System.out.println("Please enter a higher value");
userNum = input.nextInt();
}
System.out.println("Max value reached");

57. The myVal variable is initialized to 10. The first println statement will display:
“My initial value is 10”.​
myVal is then incremented by 1 and the value is stored in yourVal.
At this point, both myVal and yourVal have a value of 11.​
The second println statement will display:“My new value is 11.”
And the third println statement will display:“Your value is 11.”

58. Loop fusion is the technique of combining two loops into one.
for(int x = 0; x < 10; ++x)
{
method1();
method2();
}

59. numEmployees =
getNumberOfEmployees();
while(count < numEmployees)

60. do
{
System.out.println("The value of currentValue is " +currentValue);
--currentValue;
}
while(currentvalue > 0);
(Other decrementing options are possible.)

61. You begin a for loop with the keyword for followed by a set of parentheses. Within the
parentheses are three sections separated by exactly two semicolons. The three sections are usually
used for the following.
Initializing the loop control variable - Testing the loop control variable - Updating the loop
control variable

You might also like