Deck 6: Looping
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Match between columns
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/66
Play
Full screen (f)
Deck 6: Looping
1
An indefinite loop is a(n) ____ loop.
A) counter-controlled
B) event-controlled
C) initialized
D) validating
A) counter-controlled
B) event-controlled
C) initialized
D) validating
B
2
When nesting loops, the variable in the outer loop changes more frequently.
False
3
A loop controlled by the user is a type of ____ loop.
A) indefinite
B) definite
C) counter-controlled
D) incrementing
A) indefinite
B) definite
C) counter-controlled
D) incrementing
A
4
Which is an infinite loop?
A) loopCount = 5; while(loopCount > 3);
{
System.out.println("Hello");
LoopCount = loopCount - 1;
}
B) loopCount = 1; while(loopCount
{
System.out.println("Hello");
}
C) loopCount = 4; while(loopCount
{
System.out.println("Hello");
LoopCount = loopCount + 1;
}
D) loopCount = 1; while(loopCount
{
System.out.println("Hello");
LoopCount = loopCount + 1;
}
A) loopCount = 5; while(loopCount > 3);
{
System.out.println("Hello");
LoopCount = loopCount - 1;
}
B) loopCount = 1; while(loopCount
{
System.out.println("Hello");
}
C) loopCount = 4; while(loopCount
{
System.out.println("Hello");
LoopCount = loopCount + 1;
}
D) loopCount = 1; while(loopCount
{
System.out.println("Hello");
LoopCount = loopCount + 1;
}
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
5
A statement that will alter the value of the loop control variable is included in the body of a loop.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
6
A loop that never ends is called a(n) ____ loop.
A) definite
B) infinite
C) while
D) for
A) definite
B) infinite
C) while
D) for
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
7
____ is the process of ensuring that a value falls within a specified range.
A) Value checking
B) Data integrity
C) Validating data
D) A range check
A) Value checking
B) Data integrity
C) Validating data
D) A range check
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
8
In order to improve loop performance, it's important to make sure the loop does not include unnecessary operations or statements.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
9
Shortcut operators are a programmer's only choice when incrementing or accumulating a variable's value.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
10
Making a comparison to 0 is slower than making a comparison to any other value.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
11
Altering a variable within both a for statement and within the block it controls can produce errors that are difficult to find.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
12
Before entering a loop, the first input statement, or ____, is retrieved.
A) empty body
B) posttest loop
C) loop body
D) priming read
A) empty body
B) posttest loop
C) loop body
D) priming read
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
13
On many keyboards, the Break key is also the ____ key.
A) Pause
B) Esc
C) Delete
D) Ctrl
A) Pause
B) Esc
C) Delete
D) Ctrl
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
14
You can use virtually any number of nested loops; however, at some point, your machine will no longer be able to store all the necessary looping information.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
15
The statements that make up a loop body will continue to execute as long as the expression value remains false.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
16
It is important that the loop control ____ be altered within the body of the loop.
A) value
B) variable
C) constant
D) argument
A) value
B) variable
C) constant
D) argument
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
17
Programmers rarely use indefinite loops when validating input data.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
18
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 .
A) empty
B) while
C) definite
D) control
A) empty
B) while
C) definite
D) control
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
19
A ____ is a structure that allows repeated execution of a block of statements.
A) body
B) Boolean expression
C) loop
D) loop control
A) body
B) Boolean expression
C) loop
D) loop control
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
20
Many seasoned programmers start counter values at 1 because they are used to doing so when working with arrays.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
21
How many times will outputLabel be called? for(customer = 1; customer
for(color = 1; color
outputLabel();
A) 0
B) 3
C) 20
D) 60
for(color = 1; color
outputLabel();
A) 0
B) 3
C) 20
D) 60
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
22
As long as methods do not depend on one another, ____ is a technique that can improve loop performance by combining two loops into one.
A) loop fusion
B) prefix incrementing
C) short-circuit evaluation
D) do-nothing looping
A) loop fusion
B) prefix incrementing
C) short-circuit evaluation
D) do-nothing looping
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
23
A(n) ____________________ is a body with no statements in it.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
24
If a(n) ____________________ is altered both within the for statement and within the block it controls, it can be very difficult to follow the program's logic.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
25
A for loop provides a convenient way to create a(n) ____ loop.
A) counter-controlled
B) posttest
C) while
D) infinite
A) counter-controlled
B) posttest
C) while
D) infinite
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
26
When loops are nested, each pair contains a(n) ____________________ loop and an outer loop.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
27
The ____________________ loop is the posttest loop used in Java.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
28
You use a unary minus sign preceding a value to make the value ____.
A) negative
B) positive
C) valid
D) constant
A) negative
B) positive
C) valid
D) constant
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
29
The process of repeatedly increasing a value by some amount is known as ____.
A) checking
B) adding
C) accumulating
D) containing
A) checking
B) adding
C) accumulating
D) containing
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
30
In a do…while loop, the loop will continue to execute until ____.
A) the loop control variable is true
B) the loop control variable is false
C) the user types EXIT
D) the program terminates
A) the loop control variable is true
B) the loop control variable is false
C) the user types EXIT
D) the program terminates
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
31
The ____ loop checks the value of the loop control variable at the bottom of the loop after one repetition has occurred.
A) while
B) do…while
C) for
D) else
A) while
B) do…while
C) for
D) else
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
32
When creating a for loop, which statement will correctly initialize more than one variable?
A) for a=1, b=2
B) for(a=1; b=2)
C) for(a=1, b=2)
D) for(a = 1&& b = 2)
A) for a=1, b=2
B) for(a=1; b=2)
C) for(a=1, b=2)
D) for(a = 1&& b = 2)
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
33
A(n) ____ loop is one that performs no actions other than looping.
A) nested
B) do-nothing
C) indefinite
D) posttest
A) nested
B) do-nothing
C) indefinite
D) posttest
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
34
The order of the conditional expressions in the following is most important within a(n) ____ loop. while(requestedNum > LIMIT || requestedNum
A) nested
B) posttest
C) pretest
D) indefinite
A) nested
B) posttest
C) pretest
D) indefinite
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
35
One execution of any loop is called a(n) ____________________.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
36
You can initialize more than one variable in a for loop by placing a(n) ____ between the separate statements.
A) semicolon
B) equal sign
C) period
D) comma
A) semicolon
B) equal sign
C) period
D) comma
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
37
Which of the following is NOT a valid method to increase a variable named score by 1?
A) ++score
B) score++
C) ++score = score + 1
D) score = score + 1
A) ++score
B) score++
C) ++score = score + 1
D) score = score + 1
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
38
When you want to increase a variable's value by exactly 1, use the ____.
A) power statement
B) accumulating operator
C) binary operators
D) prefix increment operator
A) power statement
B) accumulating operator
C) binary operators
D) prefix increment operator
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
39
In the expressions b = 8 and c = --b , what value will be assigned to the variable c ?
A) 8
B) 7
C) 9
D) 10
A) 8
B) 7
C) 9
D) 10
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
40
A(n) ____ loop is a special loop that is used when a definite number of loop iterations is required.
A) while
B) for
C) else
D) do…while
A) while
B) for
C) else
D) do…while
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
41
Match between columns
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
42
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".
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
43
What are some of the shortcuts Java provides programmers for incrementing and accumulating? Give examples of statements.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
44
How could you rewrite the following code to have the arithmetic performed only once, even if the loop executes 1,000 times?
while (x
// loop body
while (x
// loop body
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
45
How does a for loop work?
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
46
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?
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
47
counterLoop = 1;
while(counterLoop
{
System.out.println("Hello");
counterLoop = counterLoop + 1;
}
What is the problem in the above while loop? How would you correct the problem?
while(counterLoop
{
System.out.println("Hello");
counterLoop = counterLoop + 1;
}
What is the problem in the above while loop? How would you correct the problem?
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
48
Describe how a loop is controlled by a Boolean expression.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
49
Why would a loop with altered user input be considered a type of indefinite loop? Give an example.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
50
Provide a code example of a pretest loop and an example of a posttest loop.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
51
How could a programmer identify and escape from an infinite loop?
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
52
What is loop fusion? Write a code segment that uses loop fusion to call two methods, method1 and method2 , 10 times each.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
53

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.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
54
Why would a programmer use curly braces in the body of a do…while loop?
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
55
What are some unconventional uses of the three sections inside the parentheses of a for loop? Show at least three examples using code.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
56
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.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
57
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.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
58
What are the three sections inside the parentheses of a for loop typically used for?
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
59
Explain why an infinite loop might not actually execute infinitely.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
60
How are nested loops implemented in a loop structure?
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
61
while(count
Examine the statement above. Write the code that will result in more efficient program execution assuming the result of getNumberOfEmployees() stays the same.
Examine the statement above. Write the code that will result in more efficient program execution assuming the result of getNumberOfEmployees() stays the same.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
62
while(10 > 1)
{
System.out.println("This prints forever.");
}
Identify the problem that exists in the above while loop.
{
System.out.println("This prints forever.");
}
Identify the problem that exists in the above while loop.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
63
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.
{
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.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
64
How are indefinite loops used for validating data? Why is a loop structure typically the better choice for validating data than an if statement?
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
65

Using the information provided above, write the while loop that will correctly follow the execution of the flowchart.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
66
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.
{
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.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck