Deck 6: More Conditionals and Loops
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/35
Play
Full screen (f)
Deck 6: More Conditionals and Loops
1
Each switch statement must terminate with a break statement.
False
2
The following loop is syntactically valid:
for(int j = 0; j < 1000; j++) j--;
for(int j = 0; j < 1000; j++) j--;
True
3
A for statement is normally used when you do not know how many times the loop should be executed.
False
4
A conditional operator is virtually the same as a switch statement.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
5
Given that s is a String, what does the following loop do? for(int j = s.length(); j > 0; j--)
System.out.print;
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 first character
E) It yields a run-time error because there is no character at s.charAt(j-1) for j = 0
System.out.print;
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 first 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 35 flashcards in this deck.
Unlock Deck
k this deck
6
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 these
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 these
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
7
A switch statement must have a default clause.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
8
If a switch statement contains no break statements at all,
A) a syntax error will occur
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 a syntax error but nothing within the switch statement will ever be executed
E) None of these
A) a syntax error will occur
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 a syntax error but nothing within the switch statement will ever be executed
E) None of these
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
9
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 integer values
B) the variable being tested might equal one of a few integer values
C) there are two or more integer variables being tested, each one of which could be one of several hundred values
D) there are two or more integer variables being tested, each one of which could be one of a few values
E) None of these; you would never choose a switch statement instead of nested if-else statements under any circumstances.
A) the variable being tested might equal one of several hundred integer values
B) the variable being tested might equal one of a few integer values
C) there are two or more integer variables being tested, each one of which could be one of several hundred values
D) there are two or more integer variables being tested, each one of which could be one of a few values
E) None of these; you would never choose a switch statement instead of nested if-else statements under any circumstances.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
10
How many times will the following nested loop structure execute the innermost statement (x++;)? for(int j = 0; j < 100; j++)
For(int k = 100; k > 0; k--)
X++;
A) 100
B) 200
C) 10,000
D) 20,100
E) 1,000,000
For(int k = 100; k > 0; k--)
X++;
A) 100
B) 200
C) 10,000
D) 20,100
E) 1,000,000
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
11
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
X += i;
A) 0
B) 4
C) 5
D) 10
E) 15
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
12
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 the condition in its while statement is false and the while loop will continue to loop while the condition in its while statement is true
D) the while loop will continue to loop while the condition in its while statement is false and the do loop will continue to loop while the condition in its while statement is true
E) None of these; there is no difference between the two types of loops
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 the condition in its while statement is false and the while loop will continue to loop while the condition in its while statement is true
D) the while loop will continue to loop while the condition in its while statement is false and the do loop will continue to loop while the condition in its while statement is true
E) None of these; there is no difference between the two types of loops
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
13
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
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 35 flashcards in this deck.
Unlock Deck
k this deck
14
The following for loop is an infinite loop::
for(int j = 0; j < 1000;) i++;
for(int j = 0; j < 1000;) i++;
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
15
Control in a switch statement jumps to the first matching case.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
16
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 35 flashcards in this deck.
Unlock Deck
k this deck
17
It is possible to convert any type of loop (while, do, for) into any other.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
18
How can the following statement be rewritten using a conditional operator? if(x <0) y = x;
Else y = 0;
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;
Else y = 0;
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 35 flashcards in this deck.
Unlock Deck
k this deck
19
A loop can be used in a GUI to draw concentric circles.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
20
What does the break statement do?
A) It ends a program.
B) It transfers control out of the current control structure such as a loop or switch statement.
C) It ends the current line of output, returning the cursor.
D) It denotes the end of a switch statement.
E) It indicates the end of a line when using System.out.print.
A) It ends a program.
B) It transfers control out of the current control structure such as a loop or switch statement.
C) It ends the current line of output, returning the cursor.
D) It denotes the end of a switch statement.
E) It indicates the end of a line when using System.out.print.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
21
Which of the following statements are true about Java loops?
A) All three loop types 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 type of loop to use is a while loop.
D) Loops may be replaced by an appropriate combination of if-else and switch statements.
E) None of these
A) All three loop types 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 type of loop to use is a while loop.
D) Loops may be replaced by an appropriate combination of if-else and switch statements.
E) None of these
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
22
Write code that outputs all of the int values between 1 and 100 with five values per line, and each of those five values spaced out equally. Use a single for loop to solve this problem.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
23
Rewrite the following nested if-else statements using 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! ");
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 35 flashcards in this deck.
Unlock Deck
k this deck
24
Write a declaration that initializes an int variable named num1 to 100 if the boolean variable score is true and to 50 otherwise, using the conditional operator.
Unlock Deck
Unlock for access to all 35 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++;
int k = 0;
for (j = 0; j < 20; j += k)
k++;
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
26
Rewrite the following if-else statement using a conditional operator.
if (x > y) z = x; else z = y;
if (x > y) z = x; else z = y;
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
27
Write the code to display an ImageView named the_view at half its original size.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
28
Code Example Ch 06-1
In the following example, x is an int:
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++
}
Refer to Code Example Ch 06-1: If x is 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
In the following example, x is an int:
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++
}
Refer to Code Example Ch 06-1: If x is 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 35 flashcards in this deck.
Unlock Deck
k this deck
29
Which of the following would rotate an Ellipse named lipse 30 degrees counterclockwise?
A) lipse.setRotate(30);
B) lipse.setRotate(370);
C) lipse.setRotate(-30);
D) Ellipse.lipse.setRotate(30);
E) Ellipse.lipse.setRotate(-10*3);
A) lipse.setRotate(30);
B) lipse.setRotate(370);
C) lipse.setRotate(-30);
D) Ellipse.lipse.setRotate(30);
E) Ellipse.lipse.setRotate(-10*3);
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
30
Write a do loop to obtain a list of scores from a teacher. The teacher should end input by entering the sentinel value, 999. The program should output the average of the scores.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
31
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%
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 35 flashcards in this deck.
Unlock Deck
k this deck
32
If a break occurs within the innermost loop of a nested loop that is three levels deep,
A) just the innermost loop will be "broken"
B) all loops are "broken" and execution continues from after the end of the loop
C) all but the outermost loops are "broken" and execution continues from the next iteration of the outermost loop
D) a syntax error is generated unless there are break or continue statements at each loop level
E) None of these are true
A) just the innermost loop will be "broken"
B) all loops are "broken" and execution continues from after the end of the loop
C) all but the outermost loops are "broken" and execution continues from the next iteration of the outermost loop
D) a syntax error is generated unless there are break or continue statements at each loop level
E) None of these are true
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
33
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("*");
A) 50
B) 100
C) 55
D) 10
E) 20
For(k = 10; k > j; k--)
System.out.println("*");
A) 50
B) 100
C) 55
D) 10
E) 20
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
34
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 35 flashcards in this deck.
Unlock Deck
k this deck
35
Code Example Ch 06-1
In the following example, x is an int:
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++
}
Refer to Code Example Ch 06-1: If x is 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
In the following example, x is an int:
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++
}
Refer to Code Example Ch 06-1: If x is 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 35 flashcards in this deck.
Unlock Deck
k this deck