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
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
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/72
Play
Full screen (f)
Deck 6: Looping
1
Programmers rarely use indefinite loops when validating input data.
False
2
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
A
3
Many seasoned programmers start counter values at 1 because they are used to doing so when working with arrays.
False
4
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 72 flashcards in this deck.
Unlock Deck
k this deck
5
Shortcut operators are a programmer's only choice when incrementing or accumulating a variable's value.
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
6
Making a comparison to 0 is slower than making a comparison to any other value.
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
7
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 72 flashcards in this deck.
Unlock Deck
k this deck
8
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 72 flashcards in this deck.
Unlock Deck
k this deck
9
____ 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 72 flashcards in this deck.
Unlock Deck
k this deck
10
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 72 flashcards in this deck.
Unlock Deck
k this deck
11
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
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
12
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 72 flashcards in this deck.
Unlock Deck
k this deck
13
Which is an infinite loop?
A) loopCount = 5;
While(loopCount > 3);
{
System.out.println("Hello");
LoopCount = loopCount - 1;
}
B) loopCount = 1;
While(loopCount < 3);
{
System.out.println("Hello");
}
C) loopCount = 4;
While(loopCount < 3);
{
System.out.println("Hello");
LoopCount = loopCount + 1;
}
D) loopCount = 1;
While(loopCount < 3);
{
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 < 3);
{
System.out.println("Hello");
}
C) loopCount = 4;
While(loopCount < 3);
{
System.out.println("Hello");
LoopCount = loopCount + 1;
}
D) loopCount = 1;
While(loopCount < 3);
{
System.out.println("Hello");
LoopCount = loopCount + 1;
}
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
14
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
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
15
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 72 flashcards in this deck.
Unlock Deck
k this deck
16
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 72 flashcards in this deck.
Unlock Deck
k this deck
17
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 72 flashcards in this deck.
Unlock Deck
k this deck
18
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 72 flashcards in this deck.
Unlock Deck
k this deck
19
When nesting loops, the variable in the outer loop changes more frequently.
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
20
Before entering a loop, the first input, 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 72 flashcards in this deck.
Unlock Deck
k this deck
21
The ____________________ loop is the posttest loop used in Java.
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
22
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 72 flashcards in this deck.
Unlock Deck
k this deck
23
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 72 flashcards in this deck.
Unlock Deck
k this deck
24
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 72 flashcards in this deck.
Unlock Deck
k this deck
25
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 72 flashcards in this deck.
Unlock Deck
k this deck
26
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 72 flashcards in this deck.
Unlock Deck
k this deck
27
The order of the conditional expressions in the following is most important within a(n) ____ loop. while(requestedNum > LIMIT || requestedNum < 0)…
A) nested
B) posttest
C) pretest
D) indefinite
A) nested
B) posttest
C) pretest
D) indefinite
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
28
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 72 flashcards in this deck.
Unlock Deck
k this deck
29
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 72 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 72 flashcards in this deck.
Unlock Deck
k this deck
31
When loops are nested, each pair contains a(n) ____________________ loop and an outer loop.
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
32
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 72 flashcards in this deck.
Unlock Deck
k this deck
33
One execution of any loop is called a(n) ____________________.
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
34
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 72 flashcards in this deck.
Unlock Deck
k this deck
35
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 72 flashcards in this deck.
Unlock Deck
k this deck
36
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 72 flashcards in this deck.
Unlock Deck
k this deck
37
How many times will outputLabel be called? for(customer = 1; customer <= 20; ++customer)
For(color = 1; color <= 3; ++color)
OutputLabel();
A) 0
B) 3
C) 20
D) 60
For(color = 1; color <= 3; ++color)
OutputLabel();
A) 0
B) 3
C) 20
D) 60
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
38
A(n) ____________________ is a body with no statements in it.
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
39
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 72 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 72 flashcards in this deck.
Unlock Deck
k this deck
41
Why would a programmer use curly braces in the body of a do…while loop?
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
42
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 72 flashcards in this deck.
Unlock Deck
k this deck
43
Match each term with the correct statement below.
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
A shortcut for incrementing and accumulating
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
A shortcut for incrementing and accumulating
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
44
Match each term with the correct statement below.
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
The value that determines whether loop execution continues
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
The value that determines whether loop execution continues
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
45
Describe the purpose of a loop control variable. How are Boolean values and a while loop involved?
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
46
Besides initializing, testing, and incrementing, you can also perform other tasks with a for loop. What are some of these tasks? Use code samples as examples.
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
47
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 72 flashcards in this deck.
Unlock Deck
k this deck
48
Match each term with the correct statement below.
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
Subtracting 1 from a variable
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
Subtracting 1 from a variable
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
49
Match each term with the correct statement below.
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
Part of the Thread class in the java.lang package
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
Part of the Thread class in the java.lang package
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
50
How does a for loop work?
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
51
Match each term with the correct statement below.
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
Within parentheses are three sections separated by exactly two semicolons
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
Within parentheses are three sections separated by exactly two semicolons
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
52
How are nested loops implemented in a loop structure?
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
53
How could a programmer identify and escape from an infinite loop?
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
54
Match each term with the correct statement below.
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
Multiple statements within curly braces
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
Multiple statements within curly braces
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
55
Match each term with the correct statement below.
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
The technique of combining two loops into one
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
The technique of combining two loops into one
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
56
How could you rewrite the following code to have the arithmetic performed only once, even if the loop executes 1,000 times?
while (x < a + b)
// loop body
while (x < a + b)
// loop body
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
57
Match each term with the correct statement below.
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
Operate on two values
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
Operate on two values
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
58
What are some of the shortcuts Java provides programmers for incrementing and accumulating? Give examples of statements.
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
59
Describe how a loop is controlled by a Boolean expression.
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
60
Match each term with the correct statement below.
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
A counter-controlled loop
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
A counter-controlled loop
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
61
Explain why an infinite loop might not actually execute infinitely.
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
62
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 72 flashcards in this deck.
Unlock Deck
k this deck
63
Write a definite while loop that initializes a loop control variable named decreaseOne to 10 and continues until 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 72 flashcards in this deck.
Unlock Deck
k this deck
64
counterLoop = 1;
while(counterLoop < 10);
{
System.out.println("Enter a new value");
counterLoop = counterLoop + 1;
}
What is the problem in the above while loop? How would you correct the problem?
while(counterLoop < 10);
{
System.out.println("Enter a new value");
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 72 flashcards in this deck.
Unlock Deck
k this deck
65
public class CompareValues
{
public static void main(String[] args)
{
int a = 10, b = 10, c = 11;
boolean compare1 = (++b == a);
boolean compare3 = (b++ == c);
System.out.println("Compare 1 value = " + compare1);
System.out.println("Compare 2 value = " + compare2);
}
}
Using the above code, what values will appear in compare1 and compare2 when the println commands execute? Describe how the values of compare1 and compare2 change as the statements are executed.
{
public static void main(String[] args)
{
int a = 10, b = 10, c = 11;
boolean compare1 = (++b == a);
boolean compare3 = (b++ == c);
System.out.println("Compare 1 value = " + compare1);
System.out.println("Compare 2 value = " + compare2);
}
}
Using the above code, what values will appear in compare1 and compare2 when the println commands execute? Describe how the values of compare1 and compare2 change as the statements are executed.
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
66
while(10 > 1)
{
System.out.println("Enter a new value");
}
Identify the problem that exists in the above code.
{
System.out.println("Enter a new value");
}
Identify the problem that exists in the above code.
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
67
public class CaseDemo
{
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 72 flashcards in this deck.
Unlock Deck
k this deck
68
Loop control variables can be evaluated at the start or the end of the loop. Describe both pretest loops and posttest loops. How do do…while loops execute?
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
69
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 72 flashcards in this deck.
Unlock Deck
k this deck
70
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". Once MAXVALUE is reached, create a final println statement that will output "Max value reached".
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck
71
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 72 flashcards in this deck.
Unlock Deck
k this deck
72
Write a for loop that will initialize the variables value1 to 11 and value2 to 19. Create a test section of the for statement that will test if value1 is greater than 10 and value2 is less than 20. Finally, end the for statement by incrementing value1 by 1. In the loop body, write a println statement that will display the contents of value1.
Unlock Deck
Unlock for access to all 72 flashcards in this deck.
Unlock Deck
k this deck