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 _____.
&&