Deck 5: Conditionals and Loops

Full screen (f)
exit full mode
Question
What is wrong, logically, with the following code?
If (x > 10) System.out.println("Large");
Else if (x > 6 && x <= 10) System.out.println("Medium");
Else if (x > 3 && x <= 6) System.out.println("Small");
Else System.out.println("Very small");

A) There is no logical error, but there is no need to have (x <= 10) in the second conditional or (x <= 6) in the third conditional
B) There is no logical error, but there is no need to have (x > 6) in the second conditional or (x > 3) in the third conditional
C) The logical error is that no matter what value x is, "Very small" is always printed out
D) The logical error is that no matter what value x is, "Large" is always printed out
E) There is nothing wrong with the logic at all
Use Space or
up arrow
down arrow
to flip the card.
Question
If a break occurs within the innermost loop of a nested loop that is three levels deep

A) when the break is encountered just the innermost loop is "broken"
B) when the break is encountered, all loops are "broken" and execution continues from after the while statement (in our example)
C) when the break is encountered, all but the outermost loops are broken, and execution continues from the next iteration of the while loop (in our example)
D) this is a syntax error unless there are break or continue statements at each loop level
E) none of the above
Question
Consider the following outline of a nested if-else structure which has more if clauses than else clauses. Which of the statements below is True regarding this structure?
If (condition1)
If (condition2)
Statement1;
Else statement2;

A) syntactically it is invalid to have more if clauses than else clauses
B) statement2 will only execute if condition1 is false and condition2 is false
C) statement2 will only execute if condition1 is True and condition2 is false
D) statement2 will only execute if condition1 is false, it does not matter what condition2 is
E) statement2 will never execute
Question
Given the nested if-else structure below, answer questions below.
if (a > 0)
if (b < 0)
x = x + 5;
else
if (a > 5)
x = x + 4;
else
x = x + 3;
else
x = x + 2;
If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed?

A) 0
B) 2
C) 3
D) 4
E) 5
Question
Which of the following are True statements about check boxes?

A) they may be checked or unchecked
B) radio buttons are a special kind of check boxes
C) they are Java components
D) you can control whether or not they will be visible
E) all of the above
Question
If x is an int where x = 1, what will x be after the following loop terminates?
While (x < 100)
X *= 2;

A) 2
B) 64
C) 100
D) 128
E) none of the above, this is an infinite loop
Question
How many times will the following loop iterate?
Int x = 10;
While (x > 0)
{
System.out.println(x);
X--;
}

A) 0 times
B) 1 time
C) 9 times
D) 10 times
E) 11 times
Question
Assume that x and y are int variables with x = 5, y = 3, and a and d are char variables with a = 'a' and d = 'A', and examine the following conditions: Condition 1: (x < y && x > 0)
Condition 2: (a != d || x != 5)
Condition 3: !(True && false)
Condition 4: (x > y || a == 'A' || d != 'A')

A) All 4 Conditions are True
B) Only Condition 2 is True
C) Condition 2 and Condition 4 are True only
D) Conditions 2, 3 and 4 are all True, Condition 1 is not
E) All 4 Conditions are false
Question
Of the following if statements, which one correctly executes three instructions if the condition is True?

A) if (x < 0) a = b * 2;
Y = x;
Z = a - y;
B) { if (x < 0)
A = b * 2;
Y = x;
Z = a - y;
}
C) if { (x < 0) a = b * 2;
Y = x;
Z = a - y ;
}
D) if (x < 0) {
A = b * 2;
Y = x;
Z = a - y;
}
E) B, C and D are all correct, but not A
Question
The break statement does which of the following?

A) ends the program
B) transfers control out of the current control structure such as a loop or switch statement
C) ends the current line of output, returning the cursor
D) denotes the ending of a switch statement
E) indicates the end of line when using System.out.print
Question
Assume that count is 0, total is 20 and max is 1. The following statement will do which of the following? if (count != 0 && total / count > max) max = total / count;

A) The condition short circuits and the assignment statement is not executed
B) The condition short circuits and the assignment statement is executed without problem
C) The condition does not short circuit causing a division by zero error
D) The condition short circuits so that there is no division by zero error when evaluating the condition, but the assignment statement causes a division by zero error
E) The condition will not compile because it uses improper syntax
Question
The idea that program instructions execute in order (linearly) unless otherwise specified through a conditional statement is known as

A) boolean execution
B) conditional statements
C) try and catch
D) sequentiality
E) flow of control
Question
As introduced in the Software Failure, the terminology "risk analysis" means

A) how willing are you to risk the loss of several key programmers working on your project
B) how much are you willing to risk that a particular piece of software you are developing still contains an error or errors
C) how willing are you to risk that your software will fail once implemented
D) how willing are you to risk that the machines on which your software will run will not work
E) none of the above
Question
Consider the following code that will assign a letter grade of 'A', 'B', 'C', 'D', or 'F' depending on a student's test score. if (score >= 90) grade = 'A';
If (score >= 80) grade = 'B';
If (score >= 70) grade = 'C';
If (score >= 60) grade = 'D';
Else grade = 'F';

A) This code will work correctly in all cases
B) This code will work correctly only if grade >= 60
C) This code will work correctly only if grade < 60
D) This code will work correctly only if grade < 70
E) This code will not work correctly under any circumstances
Question
As in the other members of the C family of languages (C, C++, C#), Java interprets a zero value as false and a non-zero value as True.
Question
If x is an int where x = 0, what will x be after the following loop terminates?
While (x < 100)
X *= 2;

A) 2
B) 64
C) 100
D) 128
E) none of the above, this is an infinite loop
Question
Given the nested if-else structure below, answer questions below.
if (a > 0)
if (b < 0)
x = x + 5;
else
if (a > 5)
x = x + 4;
else
x = x + 3;
else
x = x + 2;
If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed?

A) 0
B) 2
C) 3
D) 4
E) 5
Question
Given the nested if-else structure below, answer questions below.
if (a > 0)
if (b < 0)
x = x + 5;
else
if (a > 5)
x = x + 4;
else
x = x + 3;
else
x = x + 2;
If x is currently 0, a = 1 and b = -1, what will x become after the above statement is executed?

A) 0
B) 2
C) 3
D) 4
E) 5
Question
Every Interator

A) has a hasNext( ) method
B) has a hasFirst( ) method
C) has a hasNextInt( ) method
D) has a isEmpty( ) method
E) none of the above
Question
Which of the sets of statements below will add 1 to x if x is positive and subtract 1 from x if x is negative but leave x alone if x is 0?

A) if (x > 0) x++; else x--;
B) if (x > 0) x++; else if (x < 0) x--;
C) if (x > 0) x++; if (x < 0) x--;
Else x = 0;
D) if (x == 0) x = 0; else x++;
X--;
E) x++; x--;
Question
For the questions below, assume that boolean done = false, int x = 10, int y = 11, String s = "Help" and String t = "Goodbye".
The expression (done | | s.compareTo(t) < 0) is True.
Question
String s1 is said to overlap String s2 if all of the characters in s1 also appear in s2. Write a set of code that will set the boolean variable overlap to True if s1 overlaps s2 and false otherwise. Assume both s1 and s2 have already been input.
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
An if statement may or may not have an else clause, but an else clause must be part of an if statement.
Question
In Java, the symbol "=" and the symbol "==" are used synonymously (interchangeably).
Question
For the questions below, assume that boolean done = false, int x = 10, int y = 11, String s = "Help" and String t = "Goodbye".
The expression (!done && x <= y) is True.
Question
In Java, selection statements consist of the if and if-else statements.
Question
A truth table shows, for the various True or false values of boolean variables, what the result of a boolean condition is. Fill in the following truth table. Assume that a, b and c are boolean variables.
a b c a && (!b | | c)
false false false
false false True
false True false
false True True
True false false
True false True
True True false
True True True
Question
The statement if (a >= b) a++; else b--; will do the same thing as the statement if (a < b) b--; else a++;.
Question
A truth table shows, for the various True or false values of boolean variables, what the result of a boolean condition is. Fill in the following truth table. Assume that a, b and c are boolean variables.
a b c ! (a && b) | | ! (a && c)
false false false
false false True
false True false
false True True
True false false
True false True
True True false
True True True
Question
For the questions below, assume that boolean done = false, int x = 10, int y = 11, String s = "Help" and String t = "Goodbye".
The expression (s.concat(t).length( ) < y) is True.
Question
Rewrite the following set of if statements using a nested if-else structure.
if (score >= 90) grade = 'A';
if (score >= 80 && score < 90) grade = 'B';
if (score >= 70 && score < 80) grade = 'C';
if (score >= 60 && score < 70) grade = 'D';
if (score < 60) grade = 'F';
Question
Regarding the Software Failure: The operators were warned that, although the Therac-25 had many safety precautions, it might be possible to accidentally overdose a patient.
Question
When comparing any primitive type of variable, == should always be used to test to see if two values are equal.
Question
Explain what is meant by short circuiting and provide an example of short circuiting a condition with && and provide an example of short circuiting a condition with | |.
Question
In order to compare int, float and double variables, you can use <, >, ==, !=, <=, >=, but to compare char and String variables, you must use compareTo( ), equals( ) and equalsIgnoreCase( ).
Question
The statement { } is a legal block.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/37
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 5: Conditionals and Loops
1
What is wrong, logically, with the following code?
If (x > 10) System.out.println("Large");
Else if (x > 6 && x <= 10) System.out.println("Medium");
Else if (x > 3 && x <= 6) System.out.println("Small");
Else System.out.println("Very small");

A) There is no logical error, but there is no need to have (x <= 10) in the second conditional or (x <= 6) in the third conditional
B) There is no logical error, but there is no need to have (x > 6) in the second conditional or (x > 3) in the third conditional
C) The logical error is that no matter what value x is, "Very small" is always printed out
D) The logical error is that no matter what value x is, "Large" is always printed out
E) There is nothing wrong with the logic at all
A
Explanation: A) Because this is a nested if-else statement, if (x > 10) is True, then the first println statement is executed and the rest of the statement is skipped. If (x > 10) is not True, then the first else clause is executed and the next if condition is tested. At this point, (x > 10) is known to be false and therefore (x <= 10) must be True, so there is no need to check this inequality. Similarly, if (x > 6) is false, then the second else clause is executed and the third if condition is tested. However, (x <= 6) must be True, so there is no need to check this inequality.
2
If a break occurs within the innermost loop of a nested loop that is three levels deep

A) when the break is encountered just the innermost loop is "broken"
B) when the break is encountered, all loops are "broken" and execution continues from after the while statement (in our example)
C) when the break is encountered, all but the outermost loops are broken, and execution continues from the next iteration of the while loop (in our example)
D) this is a syntax error unless there are break or continue statements at each loop level
E) none of the above
A
Explanation: A) The innermost loop (the for loop in our example) is broken, and execution continues from just after the end of the for loop.
3
Consider the following outline of a nested if-else structure which has more if clauses than else clauses. Which of the statements below is True regarding this structure?
If (condition1)
If (condition2)
Statement1;
Else statement2;

A) syntactically it is invalid to have more if clauses than else clauses
B) statement2 will only execute if condition1 is false and condition2 is false
C) statement2 will only execute if condition1 is True and condition2 is false
D) statement2 will only execute if condition1 is false, it does not matter what condition2 is
E) statement2 will never execute
B
Explanation: B) The else clause must be matched to the most recent if statement. Therefore, the else goes with the if statement that tests condition2, not the if statement that tests condition1. So, if condition1 is True and condition2 is false, then statement2 will execute.
4
Given the nested if-else structure below, answer questions below.
if (a > 0)
if (b < 0)
x = x + 5;
else
if (a > 5)
x = x + 4;
else
x = x + 3;
else
x = x + 2;
If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed?

A) 0
B) 2
C) 3
D) 4
E) 5
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
5
Which of the following are True statements about check boxes?

A) they may be checked or unchecked
B) radio buttons are a special kind of check boxes
C) they are Java components
D) you can control whether or not they will be visible
E) all of the above
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
6
If x is an int where x = 1, what will x be after the following loop terminates?
While (x < 100)
X *= 2;

A) 2
B) 64
C) 100
D) 128
E) none of the above, this is an infinite loop
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
7
How many times will the following loop iterate?
Int x = 10;
While (x > 0)
{
System.out.println(x);
X--;
}

A) 0 times
B) 1 time
C) 9 times
D) 10 times
E) 11 times
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
8
Assume that x and y are int variables with x = 5, y = 3, and a and d are char variables with a = 'a' and d = 'A', and examine the following conditions: Condition 1: (x < y && x > 0)
Condition 2: (a != d || x != 5)
Condition 3: !(True && false)
Condition 4: (x > y || a == 'A' || d != 'A')

A) All 4 Conditions are True
B) Only Condition 2 is True
C) Condition 2 and Condition 4 are True only
D) Conditions 2, 3 and 4 are all True, Condition 1 is not
E) All 4 Conditions are false
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
9
Of the following if statements, which one correctly executes three instructions if the condition is True?

A) if (x < 0) a = b * 2;
Y = x;
Z = a - y;
B) { if (x < 0)
A = b * 2;
Y = x;
Z = a - y;
}
C) if { (x < 0) a = b * 2;
Y = x;
Z = a - y ;
}
D) if (x < 0) {
A = b * 2;
Y = x;
Z = a - y;
}
E) B, C and D are all correct, but not A
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
10
The break statement does which of the following?

A) ends the program
B) transfers control out of the current control structure such as a loop or switch statement
C) ends the current line of output, returning the cursor
D) denotes the ending of a switch statement
E) indicates the end of line when using System.out.print
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
11
Assume that count is 0, total is 20 and max is 1. The following statement will do which of the following? if (count != 0 && total / count > max) max = total / count;

A) The condition short circuits and the assignment statement is not executed
B) The condition short circuits and the assignment statement is executed without problem
C) The condition does not short circuit causing a division by zero error
D) The condition short circuits so that there is no division by zero error when evaluating the condition, but the assignment statement causes a division by zero error
E) The condition will not compile because it uses improper syntax
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
12
The idea that program instructions execute in order (linearly) unless otherwise specified through a conditional statement is known as

A) boolean execution
B) conditional statements
C) try and catch
D) sequentiality
E) flow of control
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
13
As introduced in the Software Failure, the terminology "risk analysis" means

A) how willing are you to risk the loss of several key programmers working on your project
B) how much are you willing to risk that a particular piece of software you are developing still contains an error or errors
C) how willing are you to risk that your software will fail once implemented
D) how willing are you to risk that the machines on which your software will run will not work
E) none of the above
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
14
Consider the following code that will assign a letter grade of 'A', 'B', 'C', 'D', or 'F' depending on a student's test score. if (score >= 90) grade = 'A';
If (score >= 80) grade = 'B';
If (score >= 70) grade = 'C';
If (score >= 60) grade = 'D';
Else grade = 'F';

A) This code will work correctly in all cases
B) This code will work correctly only if grade >= 60
C) This code will work correctly only if grade < 60
D) This code will work correctly only if grade < 70
E) This code will not work correctly under any circumstances
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
15
As in the other members of the C family of languages (C, C++, C#), Java interprets a zero value as false and a non-zero value as True.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
16
If x is an int where x = 0, what will x be after the following loop terminates?
While (x < 100)
X *= 2;

A) 2
B) 64
C) 100
D) 128
E) none of the above, this is an infinite loop
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
17
Given the nested if-else structure below, answer questions below.
if (a > 0)
if (b < 0)
x = x + 5;
else
if (a > 5)
x = x + 4;
else
x = x + 3;
else
x = x + 2;
If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed?

A) 0
B) 2
C) 3
D) 4
E) 5
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
18
Given the nested if-else structure below, answer questions below.
if (a > 0)
if (b < 0)
x = x + 5;
else
if (a > 5)
x = x + 4;
else
x = x + 3;
else
x = x + 2;
If x is currently 0, a = 1 and b = -1, what will x become after the above statement is executed?

A) 0
B) 2
C) 3
D) 4
E) 5
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
19
Every Interator

A) has a hasNext( ) method
B) has a hasFirst( ) method
C) has a hasNextInt( ) method
D) has a isEmpty( ) method
E) none of the above
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
20
Which of the sets of statements below will add 1 to x if x is positive and subtract 1 from x if x is negative but leave x alone if x is 0?

A) if (x > 0) x++; else x--;
B) if (x > 0) x++; else if (x < 0) x--;
C) if (x > 0) x++; if (x < 0) x--;
Else x = 0;
D) if (x == 0) x = 0; else x++;
X--;
E) x++; x--;
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
21
For the questions below, assume that boolean done = false, int x = 10, int y = 11, String s = "Help" and String t = "Goodbye".
The expression (done | | s.compareTo(t) < 0) is True.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
22
String s1 is said to overlap String s2 if all of the characters in s1 also appear in s2. Write a set of code that will set the boolean variable overlap to True if s1 overlaps s2 and false otherwise. Assume both s1 and s2 have already been input.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
23
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 37 flashcards in this deck.
Unlock Deck
k this deck
24
An if statement may or may not have an else clause, but an else clause must be part of an if statement.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
25
In Java, the symbol "=" and the symbol "==" are used synonymously (interchangeably).
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
26
For the questions below, assume that boolean done = false, int x = 10, int y = 11, String s = "Help" and String t = "Goodbye".
The expression (!done && x <= y) is True.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
27
In Java, selection statements consist of the if and if-else statements.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
28
A truth table shows, for the various True or false values of boolean variables, what the result of a boolean condition is. Fill in the following truth table. Assume that a, b and c are boolean variables.
a b c a && (!b | | c)
false false false
false false True
false True false
false True True
True false false
True false True
True True false
True True True
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
29
The statement if (a >= b) a++; else b--; will do the same thing as the statement if (a < b) b--; else a++;.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
30
A truth table shows, for the various True or false values of boolean variables, what the result of a boolean condition is. Fill in the following truth table. Assume that a, b and c are boolean variables.
a b c ! (a && b) | | ! (a && c)
false false false
false false True
false True false
false True True
True false false
True false True
True True false
True True True
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
31
For the questions below, assume that boolean done = false, int x = 10, int y = 11, String s = "Help" and String t = "Goodbye".
The expression (s.concat(t).length( ) < y) is True.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
32
Rewrite the following set of if statements using a nested if-else structure.
if (score >= 90) grade = 'A';
if (score >= 80 && score < 90) grade = 'B';
if (score >= 70 && score < 80) grade = 'C';
if (score >= 60 && score < 70) grade = 'D';
if (score < 60) grade = 'F';
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
33
Regarding the Software Failure: The operators were warned that, although the Therac-25 had many safety precautions, it might be possible to accidentally overdose a patient.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
34
When comparing any primitive type of variable, == should always be used to test to see if two values are equal.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
35
Explain what is meant by short circuiting and provide an example of short circuiting a condition with && and provide an example of short circuiting a condition with | |.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
36
In order to compare int, float and double variables, you can use <, >, ==, !=, <=, >=, but to compare char and String variables, you must use compareTo( ), equals( ) and equalsIgnoreCase( ).
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
37
The statement { } is a legal block.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 37 flashcards in this deck.