Deck 5: Conditionals and Loops

Full screen (f)
exit full mode
Question
The statement:
if (a >= b) a++; else b--;
will do the same thing as the statement:
if (a < b) b--; else a++;.
Use Space or
up arrow
down arrow
to flip the card.
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
In Java, the symbol "=" and the symbol "==" are used synonymously (interchangeably).
Question
Assume that boolean done = false, int x = 10, int y = 11, String s = "Help" and String t = "Goodbye". Then the expression (done || s.compareTo(t) < 0) is true.
Question
If you create an ArrayList without specifying the type of element, the ArrayList will store Object references which means you can put any type of object in the list.
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
Assume that boolean done = false, int x = 10, int y = 11, String s = "Help" and String t = "Goodbye". Then the expression (!done && x <= y) is true.
Question
An if statement may or may not have an else clause, but an else clause must be part of an if statement.
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
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
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 the grade is greater than or equal to 60.
C) This code will work correctly only if the grade is less than 60.
D) This code will work correctly only if the grade is less than 70.
E) This code will not work correctly under any circumstances.
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,4 are all true, Conditions 1 is not
E) all 4 conditions are false
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
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
The statement {} is a legal block.
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
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
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
Assume that boolean done = false, int x = 10, int y = 11, String s = "Help" and String t = "Goodbye". Then the expression (s.concat(t).length() < y) is true.
Question
In Java, selection statements consist only of if and if-else statements.
Question
Which of the following are true about check boxes?

A) They may be checked or unchecked.
B) You can allow more than one check box to be checked by the user at a given time.
C) Check boxes are Java components.
D) You can control whether they will be visible or not.
E) All of these are true about check boxes
Question
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
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 these; this is an infinite loop
Question
Refer to Code Segment Ch 05-1. If x is currently 0, a = 1 and b = -1, what will x become after the statement shown is executed?

A) 0
B) 2
C) 3
D) 4
E) 5
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
Every iterator

A) has a hasNext() method
B) has a hasFirst()method
C) has a hasNextInt()method
D) has a isEmpty()method
E) None of these
Question
Which type of GUI control would be best to use if you wanted the user to select one date from a list of three possible dates to take an exam?

A) check boxes because they are used for mutually exclusive options
B) radio buttons because they are used for mutually exclusive options
C) either check boxes or radio buttons because either are used for mutually exclusive options
D) neither check boxes nor radio buttons can be used for mutually exclusive options
E) There is no way to force a user to select only one option from a list; this must be done with a text input box.
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
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 these; 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
B) once
C) 9 times
D) 10 times
E) 11 times
Question
Refer to Code Segment Ch 05-1. If x is currently 0, a = 5 and b = 5, what will x become after the statement shown is executed?

A) 0
B) 2
C) 3
D) 4
E) 5
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
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
Refer to Code Segment Ch 05-1. If x is currently 0, a = 0 and b = -5, what will x become after the statement shown is executed?

A) 0
B) 2
C) 3
D) 4
E) 5
Question
What does the break statement do?

A) ends a 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 end of a switch statement
E) indicated the end of a line when using System.out.print
Question
As introduced in the Software Failure, the terminology "risk analysis" means

A) how willing you are to risk the loss of several key programmers working on your project
B) how much you are willing to risk that a particular piece of software you are developing still contains an error or errors
C) how willing you are to risk that your software will fail once implemented
D) how willing you are to risk that the machines on which your software will run will not work
E) None of these
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
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
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/38
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 5: Conditionals and Loops
1
The statement:
if (a >= b) a++; else b--;
will do the same thing as the statement:
if (a < b) b--; else a++;.
True
2
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
E
3
In Java, the symbol "=" and the symbol "==" are used synonymously (interchangeably).
False
4
Assume that boolean done = false, int x = 10, int y = 11, String s = "Help" and String t = "Goodbye". Then the expression (done || s.compareTo(t) < 0) is true.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
5
If you create an ArrayList without specifying the type of element, the ArrayList will store Object references which means you can put any type of object in the list.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
6
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 38 flashcards in this deck.
Unlock Deck
k this deck
7
Assume that boolean done = false, int x = 10, int y = 11, String s = "Help" and String t = "Goodbye". Then the expression (!done && x <= y) is true.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
8
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 38 flashcards in this deck.
Unlock Deck
k this deck
9
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 38 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 38 flashcards in this deck.
Unlock Deck
k this deck
11
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 the grade is greater than or equal to 60.
C) This code will work correctly only if the grade is less than 60.
D) This code will work correctly only if the grade is less than 70.
E) This code will not work correctly under any circumstances.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
12
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,4 are all true, Conditions 1 is not
E) all 4 conditions are false
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
13
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 38 flashcards in this deck.
Unlock Deck
k this deck
14
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 38 flashcards in this deck.
Unlock Deck
k this deck
15
The statement {} is a legal block.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
16
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 38 flashcards in this deck.
Unlock Deck
k this deck
17
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 38 flashcards in this deck.
Unlock Deck
k this deck
18
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 38 flashcards in this deck.
Unlock Deck
k this deck
19
Assume that boolean done = false, int x = 10, int y = 11, String s = "Help" and String t = "Goodbye". Then the expression (s.concat(t).length() < y) is true.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
20
In Java, selection statements consist only of if and if-else statements.
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
21
Which of the following are true about check boxes?

A) They may be checked or unchecked.
B) You can allow more than one check box to be checked by the user at a given time.
C) Check boxes are Java components.
D) You can control whether they will be visible or not.
E) All of these are true about check boxes
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
22
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
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
23
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 these; this is an infinite loop
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
24
Refer to Code Segment Ch 05-1. If x is currently 0, a = 1 and b = -1, what will x become after the statement shown is executed?

A) 0
B) 2
C) 3
D) 4
E) 5
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
25
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 38 flashcards in this deck.
Unlock Deck
k this deck
26
Every iterator

A) has a hasNext() method
B) has a hasFirst()method
C) has a hasNextInt()method
D) has a isEmpty()method
E) None of these
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
27
Which type of GUI control would be best to use if you wanted the user to select one date from a list of three possible dates to take an exam?

A) check boxes because they are used for mutually exclusive options
B) radio buttons because they are used for mutually exclusive options
C) either check boxes or radio buttons because either are used for mutually exclusive options
D) neither check boxes nor radio buttons can be used for mutually exclusive options
E) There is no way to force a user to select only one option from a list; this must be done with a text input box.
Unlock Deck
Unlock for access to all 38 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 38 flashcards in this deck.
Unlock Deck
k this deck
29
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 these; this is an infinite loop
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
30
How many times will the following loop iterate? int x = 10;
While (x > 0)
{
System.out.println(x);
X--;
}

A) 0
B) once
C) 9 times
D) 10 times
E) 11 times
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
31
Refer to Code Segment Ch 05-1. If x is currently 0, a = 5 and b = 5, what will x become after the statement shown is executed?

A) 0
B) 2
C) 3
D) 4
E) 5
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
32
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 38 flashcards in this deck.
Unlock Deck
k this deck
33
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 38 flashcards in this deck.
Unlock Deck
k this deck
34
Refer to Code Segment Ch 05-1. If x is currently 0, a = 0 and b = -5, what will x become after the statement shown is executed?

A) 0
B) 2
C) 3
D) 4
E) 5
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
35
What does the break statement do?

A) ends a 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 end of a switch statement
E) indicated the end of a line when using System.out.print
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
36
As introduced in the Software Failure, the terminology "risk analysis" means

A) how willing you are to risk the loss of several key programmers working on your project
B) how much you are willing to risk that a particular piece of software you are developing still contains an error or errors
C) how willing you are to risk that your software will fail once implemented
D) how willing you are to risk that the machines on which your software will run will not work
E) None of these
Unlock Deck
Unlock for access to all 38 flashcards in this deck.
Unlock Deck
k this deck
37
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 38 flashcards in this deck.
Unlock Deck
k this deck
38
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 38 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 38 flashcards in this deck.