Lec7: Logical Operators Flashcards

1
Q

If chr is a character variable, which of the following if statements is written correctly?

A

if (chr == ‘a’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In Java, when a character is stored in memory, it is actually stored as a(n):

A

Unicode number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

1000

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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;

A

25

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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;

A

1000

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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?

A

((x > 500 && x < 650) || (y != 1000))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which of the following correctly tests the char variable chr to determine whether it is NOT equal to the character B?

A

if (chr!= ‘B’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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?

A

((x <= 500 || x > 650) && !(y == 1000))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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)

A

2 only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Programs never need more than one path of execution.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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 ( || ).

A

When you use an && or || operator, each Boolean expression that surrounds the operator is always tested in order from left to right.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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;

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

&&

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

The operator that combines two conditions into a single Boolean value that is true when at least one of the conditions is true is _____.

A

||

17
Q

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

A

All of the above statements set g to 0.

18
Q

Which of the following groups has the lowest operator precedence?

A

Logical OR

19
Q

Assuming a variable y has been assigned the value 6, the value of !(y < 7) is

A

false

20
Q

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…”);

A

Regal!

21
Q

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?

A

16

22
Q

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);

A

true (case-sensitive)

23
Q

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

A

x = 0, y = 1
x = 20, y =13

24
Q

In Java, the condition 4 > y > 1….

A

does not evaluate correctly and should be replaced by (4 > y && y >1)

25
Q

The OR || operator

A

Stops evaluation upon finding one condition to be true

26
Q

The condition

num != 65
Cannot be replaced by:
num > 65 || num < 65
!(num == 65)
num - 65
(num – 65) != 0

A

num - 65

27
Q

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”;

A

I and III only

28
Q

Which of the following is equivalent to the code segment below?

if (x > 0)
x = -x;
if (x < 0)
x = 0;

A

x = 0;

29
Q

Which of the following is equivalent to the code segment below?

if (x > 2)
x = x * 2;
if (x > 4)
x = 0;

A

if (x > 2) x = 0;

30
Q

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)

A

i, iii, iv