Deck 5: Conditionals and Loops

Full screen (f)
exit full mode
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
Use Space or
up arrow
down arrow
to flip the card.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
In Java, selection statements consist of the if and if-else statements.
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
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
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
The statement { } is a legal block.
Question
The statement if (a >= b) a++; else b--; will do the same thing as the statement if (a < b) b--; else a++;.
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
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
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
In Java, the symbol "=" and the symbol "==" are used synonymously (interchangeably).
Question
When comparing any primitive type of variable, == should always be used to test to see if two values are equal.
Question
An if statement may or may not have an else clause, but an else clause must be part of an if statement.
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
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
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
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
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
D
Explanation: D) Condition 1 is not True because (x < y) is false, making the entire condition false. Since (c != d) is True, Condition 2 is True even though (x != 5) is false. Condition 3 is True because (True && false) is false, but !false is True. Condition 4 is True because (x > y) is True, making the entire Condition True. Therefore, Conditions 2, 3 and 4 are True while Condition 1 is false.
2
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
B
Explanation: B) Since (a > 0) is not True, the else clause for this condition is executed, which is x = x + 2, so x is now 2.
3
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
D
Explanation: D) With x = 1, the loop condition is True and the statement executes, doubling x, which is now 2. Since the loop condition is still True, the statement executes again, doubling x to 4. The condition remains True for the next 4 iterations, where x becomes 8, 16, 32 and 64. Since (x < 100) is still True when x = 64, the loop iterates again and x becomes 2 * 64 = 128. Now, (x < 100) is no longer True and the loop terminates with x = 128.
4
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
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
16
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
17
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
18
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
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
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
21
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
22
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
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
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
25
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
26
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
27
The statement { } is a legal block.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
28
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
29
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
30
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
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
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
33
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
34
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
35
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
36
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
37
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
locked card icon
Unlock Deck
Unlock for access to all 37 flashcards in this deck.