Lec7: Logical Operators Flashcards
If chr is a character variable, which of the following if statements is written correctly?
if (chr == ‘a’)
In Java, when a character is stored in memory, it is actually stored as a(n):
Unicode number
What will be the value of bonus after the following code is executed?
int bonus, sales = 10000;
if (sales < 5000)
bonus = 200;
else if (sales < 7500)
bonus = 500;
else if (sales < 10000)
bonus = 750;
else if (sales < 20000)
bonus = 1000;
else
bonus = 1250
1000
What would be the value of bonus after the following statements are executed?
int bonus, sales = 1250;
if (sales > 1000)
bonus = 100;
if (sales > 750)
bonus = 50;
if (sales > 500)
bonus = 25;
else
bonus = 0;
25
What would be the value of bonus after the following statements are executed?
int bonus, sales = 85000;
char dept = ‘S’;
if (sales > 100000)
if (dept == ‘R’)
bonus = 2000;
else
bonus = 1500;
else if (sales > 75000)
if (dept == ‘R’)
bonus = 1250;
else
bonus = 1000;
else
bonus = 0;
1000
Which of the following is the correct boolean expression to test for: int x being a value between, but not including, 500 and 650, or int y not equal to 1000?
((x > 500 && x < 650) || (y != 1000))
Which of the following correctly tests the char variable chr to determine whether it is NOT equal to the character B?
if (chr!= ‘B’)
Which of the following is the correct boolean expression to test for: int x being a value less than or equal to 500 or greater than 650, and int y not equal to 1000?
((x <= 500 || x > 650) && !(y == 1000))
If str1 and str2 are both Strings, which of the following expressions will correctly determine whether they are equal?
(1)(str1 == str2)
(2) str1.equals(str2)
(3) str1.isEqual(str2)
(4) str2.isEqual(str1)
2 only
Programs never need more than one path of execution.
False
Because the || operator performs short-circuit evaluation, your boolean expression will generally be evaluated faster if the subexpression that is most likely to be true is on the left.
True
Two truths and a Lie: Identify the false statement.
A. When you use the && and || operators, you must include a complete Boolean expression on each side.
B. When you use an && or || operator, each Boolean expression that surrounds the operator is always tested in order from left to right.
C. The AND operator is written as two ampersands ( && ), and the OR operator is written as two pipes ( || ).
When you use an && or || operator, each Boolean expression that surrounds the operator is always tested in order from left to right.
Two truths and a LIE: Identify the FALSE statement.
A. The statement if(payRate < 6.00 && payRate > 50.00) can be used to select payRate values that are higher or lower than the specified limits.
B. A range check is a series of statements that determine within which of a set of ranges a value falls.
C. When you must make a series of decisions in a program, it is most efficient to first ask the question that is most likely to be true.
A. The statement if(payRate < 6.00 && payRate > 50.00) can be used to select payRate values that are higher or lower than the specified limits.
Two truths and a LIE: Identify the FALSE statement.
A. Assume p, q, and r are all Boolean variables that have been assigned the value true. After the following statement executes, the value of p is still true. p = !(q || !r);
B. Assume p, q, and r are all Boolean variables that have been assigned the value true. After the following statement executes, the value of p is still true. p = !(!q && !r);
C. Assume p, q, and r are all Boolean variables that have been assigned the value true. After the following statement executes, the value of p is still true. p = !q || r;
Assume p, q, and r are all Boolean variables that have been assigned the value true. After the following statement executes, the value of p is still true. p = !(q || !r);
The operator that combines two conditions into a single Boolean value that is true only when both of the conditions are true, but is false otherwise, is _____.
&&
The operator that combines two conditions into a single Boolean value that is true when at least one of the conditions is true is _____.
||
Assuming a variable f has been initialized to 5, which of the following statements sets g to 0?
if(f > 6 || f == 5) g = 0;
if(f >= 0 || f < 2) g = 0;
if(f < 3 || f > 4) g = 0;
All of the above statements set g to 0
None of the above statements set g to 0
All of the above statements set g to 0.
Which of the following groups has the lowest operator precedence?
Logical OR
Assuming a variable y has been assigned the value 6, the value of !(y < 7) is
false
Determine the output of the following code:
String var = “Panda”;
if (var.equals(“panda”))
System.out.println(“Cute!”);
else if (var.equals(“Panda”))
System.out.println(“Regal!”);
else
System.out.println(“Ugly…”);
Regal!
If a boolean expression involved four different independent boolean variables/expressions (e.g. p, q, r, and s), how many different rows (i.e. combinations of values) would the Truth Table contain?
16
What would be displayed after the following code fragment executed?
boolean a = true, b = false, c = false, d = true;
System.out.println(a || d && b);
true (case-sensitive)
The expression:
!(x > 20 || y % 3 == 0)
will return true for which of the following conditions (indicate ALL true conditions)
x = 0, y = 1
x = 0, y = 0
x = 15, y = 99
x = 33, y = 14
x = 20, y = 13
x = 0, y = 1
x = 20, y =13
In Java, the condition 4 > y > 1….
does not evaluate correctly and should be replaced by (4 > y && y >1)
The OR || operator
Stops evaluation upon finding one condition to be true
The condition
num != 65
Cannot be replaced by:
num > 65 || num < 65
!(num == 65)
num - 65
(num – 65) != 0
num - 65
At a certain high school students receive letter grades based on the following scale: 93 or above is an A, 84 to 92 is a B, 75 to 83 is a C, and below 75 is an F. Which of the following code segments will assign the correct string to grade for a given integer score?
I. if (score >= 93)
grade = “A”;
if (score >= 84 && score <=92)
grade = “B”;
if (score >=75 && score <= 83)
grade = “C”;
if (score < 75)
grade = “F”;
II. if (score >= 93)
grade = “A”;
if (score >= 84)
grade = “B”;
if (score >=75)
grade = “C”;
if (score < 75)
grade = “F”;
III. if (score >= 93)
grade = “A”;
else if (score >= 84)
grade = “B”;
else if (score >=75)
grade = “C”;
else
grade = “F”;
I and III only
Which of the following is equivalent to the code segment below?
if (x > 0)
x = -x;
if (x < 0)
x = 0;
x = 0;
Which of the following is equivalent to the code segment below?
if (x > 2)
x = x * 2;
if (x > 4)
x = 0;
if (x > 2) x = 0;
1) Which of the following Boolean expressions are equivalent? (Assume that x and y are integer
variables that have been initialized with the intended values.)
i) ((x > 0) && (y > 0)) || ((x > 0) && (y < 0))
ii) x != y
iii) (x > 0) && (y != 0)
iv) (x > 0) && (x + y != x)
i, iii, iv