Deck 6: More Conditionals and Loops

Full screen (f)
exit full mode
Question
Each case in a switch statement must terminate with a break statement.
Use Space or
up arrow
down arrow
to flip the card.
Question
The do loop differs from the while loop in that

A) the while loop will always execute the body of the loop at least once
B) the do loop will always execute the body of the loop at least once
C) the do loop will continue to loop while condition in the while statement is false and the while loop will continue to loop while the condition in the while statement is True
D) the while loop will continue to loop while condition in the while statement is false and the do loop will continue to loop while the condition in the while statement is True
E) none of the above, there is absolutely no difference between the two types of loops
Question
Consider the following paint method to answer the questions below:
public void paint(Graphics page)
{
int x, y = 200;
page.setColor(Color.blue);
for (x = 100; x < 200; x += 20)
page.fillRect(x, y, 10, y-x);
}
Which of the following statements are True about Java loops?

A) all three loop statements are functionally equivalent
B) while loops and do loops are essentially the same; but while loops always execute at least once
C) if you know the number of times that a loop is to be performed, the best loop statement to use is a while loop
D) loops may be replaced by an appropriate combination of if-else and switch statements
E) none of the above
Question
Consider the following paint method to answer the questions below:
public void paint(Graphics page)
{
int x, y = 200;
page.setColor(Color.blue);
for (x = 100; x < 200; x += 20)
page.fillRect(x, y, 10, y-x);
}
The size of each rectangle (bar)

A) increases in height while staying the same width
B) increases in width while staying the same height
C) increases in both width and height
D) stays the same size
E) decreases in height while staying the same width
Question
Given the following switch statement where x is an int, answer the questions below
switch (x)
{
case 3 : x += 1;
case 4 : x += 2;
case 5 : x += 3;
case 6 : x++;
case 7 : x += 2;
case 8 : x--;
case 9 : x++
}
If x is currently equal to 3, what will the value of x be after the switch statement executes?

A) 5
B) 6
C) 11
D) 10
E) 12
Question
Given the following switch statement where x is an int, answer the questions below
switch (x)
{
case 3 : x += 1;
case 4 : x += 2;
case 5 : x += 3;
case 6 : x++;
case 7 : x += 2;
case 8 : x--;
case 9 : x++
}
If x is currently equal to 5, what will the value of x be after the switch statement executes?

A) 8
B) 6
C) 11
D) 5
E) 10
Question
Given that s is a String, what does the following loop do?
For (int j = s.length( ); j > 0; j--)
System.out.print(s.charAt(j-1));

A) it prints s out backwards
B) it prints s out forwards
C) it prints s out backwards after skipping the last character
D) it prints s out backwards but does not print the 0ᵗʰ character
E) it yields a run-time error because there is no character at s.charAt(j-1) for j = 0
Question
It is possible to convert any type of loop (while, do, or for) into any other.
Question
Consider the following paint method to answer the questions below:
public void paint(Graphics page)
{
int x, y = 200;
page.setColor(Color.blue);
for (x = 100; x < 200; x += 20)
page.fillRect(x, y, 10, y-x);
}
This paint method will draw several bars (sort of like a bar graph). How many bars will be displayed?

A) 4
B) 5
C) 6
D) 10
E) 20
Question
Given the following switch statement where x is an int, answer the questions below
switch (x)
{
case 3 : x += 1;
case 4 : x += 2;
case 5 : x += 3;
case 6 : x++;
case 7 : x += 2;
case 8 : x--;
case 9 : x++
}
The statement if (x < 0) y = x; else y = 0; can be rewritten using a conditional operator as

A) y = (x < 0) ? x : 0;
B) x = (x < 0) ? y : 0;
C) (x < 0) ? y = x : y = 0;
D) y = (x < 0);
E) y = if (x < 0) x : 0;
Question
Control in a switch statement jumps to the first matching case.
Question
Given the following switch statement where x is an int, answer the questions below
switch (x)
{
case 3 : x += 1;
case 4 : x += 2;
case 5 : x += 3;
case 6 : x++;
case 7 : x += 2;
case 8 : x--;
case 9 : x++
}
Given the following code, where x = 0, what is the resulting value of x after the for-loop terminates?
For (int i=0;i<5;i++)
X += i;

A) 0
B) 4
C) 5
D) 10
E) 15
Question
A switch statement must have a default clause.
Question
You might choose to use a switch statement instead of nested if-else statements if

A) the variable being tested might equal one of several hundred int values
B) the variable being tested might equal one of only a few int values
C) there are two or more int variables being tested, each of which could be one of several hundred values
D) there are two or more int variables being tested, each of which could be one of only a few values
E) none of the above, you would never choose to use a switch statement in place of nested if-else statements under any circumstance
Question
The following nested loop structure will execute the inner most statement (x++) how many times?
For (int j = 0; j < 100; j++)
For (int k = 100; k > 0; k--)
X++;

A) 100
B) 200
C) 10,000
D) 20,000
E) 1,000,000
Question
How many times will the following loop iterate?
Int x = 10;
Do {
System.out.println(x);
X--;
} while (x > 0);

A) 0 times
B) 1 time
C) 9 times
D) 10 times
E) 11 times
Question
A continue statement

A) may be used within a while or a do-while loop, but not a for loop
B) is identical to a break statement within Java loops
C) may be used within any Java loop statement
D) may be used within a for loop, but not within a while or a do-while loop
E) none of the above
Question
If a switch statement is written that contains no break statements whatsoever,

A) this is a syntax error and an appropriate error message will be generated
B) each of the case clauses will be executed every time the switch statement is encountered
C) this is equivalent to having the switch statement always take the default clause, if one is present
D) this is not an error, but nothing within the switch statement ever will be executed
E) none of the above
Question
Consider the following paint method to answer the questions below:
public void paint(Graphics page)
{
int x, y = 200;
page.setColor(Color.blue);
for (x = 100; x < 200; x += 20)
page.fillRect(x, y, 10, y-x);
}
What will the following code do? Assume s is a String, x is an int initialized to 10, page is a Graphics object, and this is part of a paint method for an applet.
Boolean isVowel = false;
String vowels = "aeiou";
For (int j = 0; j < s.length( ); j++)
{
For (int k = 0; k < 5; k++)
If (s.charAt(j) == vowels.charAt(k)) isVowel = True;
If (isVowel) page.drawString(""+s.charAt(j), 10, 15*x++);
Else page.drawString(""+s.charAt(j), 110, 15*x++);
IsVowel = false;
}

A) The String s is printed down the applet in two columns, alternating each letter
B) The String s is printed across the applet with vowels printed in one row and all other characters printed in a row above the vowels
C) The String s is printed across the applet with vowels printed in one row and all other characters printed in a row below the vowels
D) The String s is printed down the applet in two columns with vowels appearing in the left column and all other characters in the right column
E) The String s is printed down the applet in two columns with vowels appearing in the right column and all other characters in the left column
Question
The following for-loop is an infinite loop.
for (int j = 0; j < 1000; ) i++;
Question
Write a "query-controlled" loop that will continue to input int values from the user, adding each to the int value sum, and then ask if the user has another value to input, until the user says that there are no more values. Assume that cs1.Keyboard has been imported.
Question
Write a set of code that outputs all of the int values between 1 and 100 with 5 values per line, and each of those 5 values spaced out equally. Use a single for-loop to solve this problem.
Question
Rewrite the following nested if-else statement as an equivalent switch statement.
if (letter == 'A' | | letter == 'a') System.out.println("Excellent");
else if (letter == 'B' | | letter == 'b') System.out.println("You can do better");
else if (letter == 'C' | | letter == 'c') System.out.println("Try harder");
else if (letter == 'D' | | letter == 'd') System.out.println("Try much harder");
else System.out.println("Try another major! ");
Question
In Java, it is possible to create an infinite loop out of while and do loops, but not for-loops.
Question
The following for-loop is odd in that the loop increment value changes during iterations of the loop. Determine the number of times the loop iterates and the final value of j after the loop terminates.
int k = 0;
for (j = 0; j < 20; j += k)
k++;
Question
A data verification loop is a loop that asks the user to input a value within a restricted range repeatedly until the user has input a value within the requested range. Write a data verification loop that that inputs an int value x within the range 0 and 100. Assume cs1.Keyboard has been imported.
Question
Write a paint method to draw out an American flag. The flag will comprise a blue square in the upper left corner and 13 red and white stripes, starting with red, and alternating. 6 of the 13 stripes appear to the right of the blue square and the last 7 stripes appear beneath the blue square extending as far right as the other stripes. Use a for-loop to draw the stripes. Do not try to draw any stars in the blue square.
Question
How many times will the System.out.println(*); statement execute inside of the following nested for-loops?
for (j=0; j<10; j++)
for (k=10;k>j;k--)
System.out.println("*");
Question
Write some code that inputs a set of int values and computes its average. Ask the user first how many int values will be input. Make sure that your code cannot produce a division by zero error. Assume that cs1.Keyboard has been imported.
Question
Describe a situation where you should use a do loop and a situation where you should use a while loop.
Question
The following code has a syntax error immediately before the word else. What is the error and why does it arise?
Fix the code so that this statement is a legal if-else statement.
if (x < 0) ; x++;
else x--;
Question
Given the following tax table information, write Java code to assign the double taxRate appropriately given the double pay. Select the selection statement (if, if-else, switch) that makes the most sense.
If pay is more than 100,000, tax rate is 40%
If pay is more than 60,000 and less than or equal to 100,000, tax rate is 30%
If pay is more than 30,000 and less than or equal to 60,000, tax rate is 20%
If pay is more than 15,000 and less than or equal to 30,000, tax rate is 10%
If pay is more than 5,000 and less than or equal to 15,000, tax rate is 5%
If pay is less than or equal to 5,000, tax rate is 0%
Question
A dialog box is a device which accepts voice commands.
Question
The following loop is syntactically valid.
for (int j = 0; j < 1000; j++) j--;
Question
Show the output that would occur from the following code, including proper spacing.
for (j = 0; j < 5; j++)
{
for (k = 0; k < 5; k++)
if (j!=k)
System.out.print(' ');
else
System.out.print('*');
System.out.println( );
}
Question
Rewrite the following if-else statement using a conditional operator.
if (x > y) z = x; else z = y;
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/36
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 6: More Conditionals and Loops
1
Each case in a switch statement must terminate with a break statement.
False
Explanation: They often do, but if the break statement is not present, the flow of control continues into the next case.
2
The do loop differs from the while loop in that

A) the while loop will always execute the body of the loop at least once
B) the do loop will always execute the body of the loop at least once
C) the do loop will continue to loop while condition in the while statement is false and the while loop will continue to loop while the condition in the while statement is True
D) the while loop will continue to loop while condition in the while statement is false and the do loop will continue to loop while the condition in the while statement is True
E) none of the above, there is absolutely no difference between the two types of loops
B
Explanation: B) Because the do loop does not test the condition until after the body of the loop executes, the body will always execute at least one time, but the while loop tests the condition first, and so if the condition is false the first time, the body does not execute even one time.
3
Consider the following paint method to answer the questions below:
public void paint(Graphics page)
{
int x, y = 200;
page.setColor(Color.blue);
for (x = 100; x < 200; x += 20)
page.fillRect(x, y, 10, y-x);
}
Which of the following statements are True about Java loops?

A) all three loop statements are functionally equivalent
B) while loops and do loops are essentially the same; but while loops always execute at least once
C) if you know the number of times that a loop is to be performed, the best loop statement to use is a while loop
D) loops may be replaced by an appropriate combination of if-else and switch statements
E) none of the above
A
Explanation: A) In Java, as in most languages, the looping statements are all essentially equivalent (and almost interchangeable). Their principal difference(s) relate to when the controlling condition is evaluated and whether there is syntax for incrementation/update.
4
Consider the following paint method to answer the questions below:
public void paint(Graphics page)
{
int x, y = 200;
page.setColor(Color.blue);
for (x = 100; x < 200; x += 20)
page.fillRect(x, y, 10, y-x);
}
The size of each rectangle (bar)

A) increases in height while staying the same width
B) increases in width while staying the same height
C) increases in both width and height
D) stays the same size
E) decreases in height while staying the same width
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
5
Given the following switch statement where x is an int, answer the questions below
switch (x)
{
case 3 : x += 1;
case 4 : x += 2;
case 5 : x += 3;
case 6 : x++;
case 7 : x += 2;
case 8 : x--;
case 9 : x++
}
If x is currently equal to 3, what will the value of x be after the switch statement executes?

A) 5
B) 6
C) 11
D) 10
E) 12
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
6
Given the following switch statement where x is an int, answer the questions below
switch (x)
{
case 3 : x += 1;
case 4 : x += 2;
case 5 : x += 3;
case 6 : x++;
case 7 : x += 2;
case 8 : x--;
case 9 : x++
}
If x is currently equal to 5, what will the value of x be after the switch statement executes?

A) 8
B) 6
C) 11
D) 5
E) 10
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
7
Given that s is a String, what does the following loop do?
For (int j = s.length( ); j > 0; j--)
System.out.print(s.charAt(j-1));

A) it prints s out backwards
B) it prints s out forwards
C) it prints s out backwards after skipping the last character
D) it prints s out backwards but does not print the 0ᵗʰ character
E) it yields a run-time error because there is no character at s.charAt(j-1) for j = 0
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
8
It is possible to convert any type of loop (while, do, or for) into any other.
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
9
Consider the following paint method to answer the questions below:
public void paint(Graphics page)
{
int x, y = 200;
page.setColor(Color.blue);
for (x = 100; x < 200; x += 20)
page.fillRect(x, y, 10, y-x);
}
This paint method will draw several bars (sort of like a bar graph). How many bars will be displayed?

A) 4
B) 5
C) 6
D) 10
E) 20
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
10
Given the following switch statement where x is an int, answer the questions below
switch (x)
{
case 3 : x += 1;
case 4 : x += 2;
case 5 : x += 3;
case 6 : x++;
case 7 : x += 2;
case 8 : x--;
case 9 : x++
}
The statement if (x < 0) y = x; else y = 0; can be rewritten using a conditional operator as

A) y = (x < 0) ? x : 0;
B) x = (x < 0) ? y : 0;
C) (x < 0) ? y = x : y = 0;
D) y = (x < 0);
E) y = if (x < 0) x : 0;
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
11
Control in a switch statement jumps to the first matching case.
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
12
Given the following switch statement where x is an int, answer the questions below
switch (x)
{
case 3 : x += 1;
case 4 : x += 2;
case 5 : x += 3;
case 6 : x++;
case 7 : x += 2;
case 8 : x--;
case 9 : x++
}
Given the following code, where x = 0, what is the resulting value of x after the for-loop terminates?
For (int i=0;i<5;i++)
X += i;

A) 0
B) 4
C) 5
D) 10
E) 15
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
13
A switch statement must have a default clause.
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
14
You might choose to use a switch statement instead of nested if-else statements if

A) the variable being tested might equal one of several hundred int values
B) the variable being tested might equal one of only a few int values
C) there are two or more int variables being tested, each of which could be one of several hundred values
D) there are two or more int variables being tested, each of which could be one of only a few values
E) none of the above, you would never choose to use a switch statement in place of nested if-else statements under any circumstance
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
15
The following nested loop structure will execute the inner most statement (x++) how many times?
For (int j = 0; j < 100; j++)
For (int k = 100; k > 0; k--)
X++;

A) 100
B) 200
C) 10,000
D) 20,000
E) 1,000,000
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
16
How many times will the following loop iterate?
Int x = 10;
Do {
System.out.println(x);
X--;
} while (x > 0);

A) 0 times
B) 1 time
C) 9 times
D) 10 times
E) 11 times
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
17
A continue statement

A) may be used within a while or a do-while loop, but not a for loop
B) is identical to a break statement within Java loops
C) may be used within any Java loop statement
D) may be used within a for loop, but not within a while or a do-while loop
E) none of the above
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
18
If a switch statement is written that contains no break statements whatsoever,

A) this is a syntax error and an appropriate error message will be generated
B) each of the case clauses will be executed every time the switch statement is encountered
C) this is equivalent to having the switch statement always take the default clause, if one is present
D) this is not an error, but nothing within the switch statement ever will be executed
E) none of the above
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
19
Consider the following paint method to answer the questions below:
public void paint(Graphics page)
{
int x, y = 200;
page.setColor(Color.blue);
for (x = 100; x < 200; x += 20)
page.fillRect(x, y, 10, y-x);
}
What will the following code do? Assume s is a String, x is an int initialized to 10, page is a Graphics object, and this is part of a paint method for an applet.
Boolean isVowel = false;
String vowels = "aeiou";
For (int j = 0; j < s.length( ); j++)
{
For (int k = 0; k < 5; k++)
If (s.charAt(j) == vowels.charAt(k)) isVowel = True;
If (isVowel) page.drawString(""+s.charAt(j), 10, 15*x++);
Else page.drawString(""+s.charAt(j), 110, 15*x++);
IsVowel = false;
}

A) The String s is printed down the applet in two columns, alternating each letter
B) The String s is printed across the applet with vowels printed in one row and all other characters printed in a row above the vowels
C) The String s is printed across the applet with vowels printed in one row and all other characters printed in a row below the vowels
D) The String s is printed down the applet in two columns with vowels appearing in the left column and all other characters in the right column
E) The String s is printed down the applet in two columns with vowels appearing in the right column and all other characters in the left column
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
20
The following for-loop is an infinite loop.
for (int j = 0; j < 1000; ) i++;
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
21
Write a "query-controlled" loop that will continue to input int values from the user, adding each to the int value sum, and then ask if the user has another value to input, until the user says that there are no more values. Assume that cs1.Keyboard has been imported.
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
22
Write a set of code that outputs all of the int values between 1 and 100 with 5 values per line, and each of those 5 values spaced out equally. Use a single for-loop to solve this problem.
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
23
Rewrite the following nested if-else statement as an equivalent switch statement.
if (letter == 'A' | | letter == 'a') System.out.println("Excellent");
else if (letter == 'B' | | letter == 'b') System.out.println("You can do better");
else if (letter == 'C' | | letter == 'c') System.out.println("Try harder");
else if (letter == 'D' | | letter == 'd') System.out.println("Try much harder");
else System.out.println("Try another major! ");
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
24
In Java, it is possible to create an infinite loop out of while and do loops, but not for-loops.
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
25
The following for-loop is odd in that the loop increment value changes during iterations of the loop. Determine the number of times the loop iterates and the final value of j after the loop terminates.
int k = 0;
for (j = 0; j < 20; j += k)
k++;
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
26
A data verification loop is a loop that asks the user to input a value within a restricted range repeatedly until the user has input a value within the requested range. Write a data verification loop that that inputs an int value x within the range 0 and 100. Assume cs1.Keyboard has been imported.
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
27
Write a paint method to draw out an American flag. The flag will comprise a blue square in the upper left corner and 13 red and white stripes, starting with red, and alternating. 6 of the 13 stripes appear to the right of the blue square and the last 7 stripes appear beneath the blue square extending as far right as the other stripes. Use a for-loop to draw the stripes. Do not try to draw any stars in the blue square.
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
28
How many times will the System.out.println(*); statement execute inside of the following nested for-loops?
for (j=0; j<10; j++)
for (k=10;k>j;k--)
System.out.println("*");
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
29
Write some code that inputs a set of int values and computes its average. Ask the user first how many int values will be input. Make sure that your code cannot produce a division by zero error. Assume that cs1.Keyboard has been imported.
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
30
Describe a situation where you should use a do loop and a situation where you should use a while loop.
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
31
The following code has a syntax error immediately before the word else. What is the error and why does it arise?
Fix the code so that this statement is a legal if-else statement.
if (x < 0) ; x++;
else x--;
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
32
Given the following tax table information, write Java code to assign the double taxRate appropriately given the double pay. Select the selection statement (if, if-else, switch) that makes the most sense.
If pay is more than 100,000, tax rate is 40%
If pay is more than 60,000 and less than or equal to 100,000, tax rate is 30%
If pay is more than 30,000 and less than or equal to 60,000, tax rate is 20%
If pay is more than 15,000 and less than or equal to 30,000, tax rate is 10%
If pay is more than 5,000 and less than or equal to 15,000, tax rate is 5%
If pay is less than or equal to 5,000, tax rate is 0%
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
33
A dialog box is a device which accepts voice commands.
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
34
The following loop is syntactically valid.
for (int j = 0; j < 1000; j++) j--;
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
35
Show the output that would occur from the following code, including proper spacing.
for (j = 0; j < 5; j++)
{
for (k = 0; k < 5; k++)
if (j!=k)
System.out.print(' ');
else
System.out.print('*');
System.out.println( );
}
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
36
Rewrite the following if-else statement using a conditional operator.
if (x > y) z = x; else z = y;
Unlock Deck
Unlock for access to all 36 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 36 flashcards in this deck.