AP Computer Science A › Evaluating Boolean Expressions
Consider the following code:
int\[\] a = {8,4,1,5,1,5,6,2,4};
int b = 1;
boolean\[\] vals = new boolean\[a.length\];
for(int i = 0; i < a.length; i++) {
vals\[i\] = (a\[i\] - 1 > 4);
}
for(int i = 0; i < vals.length; i++) {
String s = "Duns Scotus";
if(!vals\[i\]) {
s = "Mithrandir";
}
System.out.println(s);
}
What is the output for the code above?
Duns Scotus
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
None of the others
Begin by getting a general idea of the loop logic being executed. For each execution of the first loop, you are assigning a boolean to the vals array. Now, you could rewrite the code a little to make the math easier:
a\[i\] - 1 > 4
really is the same as:
a\[i\] > 5
Now, the only values for which this is true are:
8 and 6 (the first and the 7th values in a).
Now, looking at the second loop, the value s will be "Duns Scotus" for every value that is true in vals. This is because the if statement checks for !vals\[i\]. Thus, the first and the seventh values alone will be "Duns Scotus". Everything else will be "Mithrandir".
Consider the following code:
int\[\] a = {8,4,1,5,1,5,6,2,4};
int b = 1;
boolean\[\] vals = new boolean\[a.length\];
for(int i = 0; i < a.length; i++) {
vals\[i\] = (a\[i\] - 1 > 4);
}
for(int i = 0; i < vals.length; i++) {
String s = "Duns Scotus";
if(!vals\[i\]) {
s = "Mithrandir";
}
System.out.println(s);
}
What is the output for the code above?
Duns Scotus
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
None of the others
Begin by getting a general idea of the loop logic being executed. For each execution of the first loop, you are assigning a boolean to the vals array. Now, you could rewrite the code a little to make the math easier:
a\[i\] - 1 > 4
really is the same as:
a\[i\] > 5
Now, the only values for which this is true are:
8 and 6 (the first and the 7th values in a).
Now, looking at the second loop, the value s will be "Duns Scotus" for every value that is true in vals. This is because the if statement checks for !vals\[i\]. Thus, the first and the seventh values alone will be "Duns Scotus". Everything else will be "Mithrandir".
boolean x;
int y;
x = false;
y = 10;
x = !(y < 10 || y > 10);
System.out.println ( x );
What is printed?
True
False
5
10
x
The expression !(y < 10 || y > 10) evaluates to (y >= 10 && y <= 10).
Since y = 10, the expression evaluates to true.
boolean x;
int y;
x = false;
y = 10;
x = !(y < 10 || y > 10);
System.out.println ( x );
What is printed?
True
False
5
10
x
The expression !(y < 10 || y > 10) evaluates to (y >= 10 && y <= 10).
Since y = 10, the expression evaluates to true.
Which is true?
a = 4
b = 2
c = 5
d = 6
a) a = c - d
b) a = b - d
c) a = d - b
d) a = c * d
c)
a)
b)
d)
c) is the correct choice because a = 6 - 2 since d = 6 and b = 2. Substitute the numbers for the variables in each answer choice. You will see that all of the answer choices are not equal to 4 except for c).
Which of the following Boolean expression evalutes to True?
I. False && False && False && True && False
II. (False && False) || (True || False) || (False && (True || False))
III. (True && False) && (True || False || (True && True))
IV. !(True && True && False)
V. False || False || False || True || False
II, IV, V
I, IV, V
IV
III, IV, V,
IV, V
Answer: II, IV, V
The best way to approach this problem is to divide each conditional into chunks. Then, if there is at least one True in a long list of OR statements, the statement must be true according to Boolean Logic. Likewise, if there is at least one False in a long list of AND's, the statment must be False. For example
False && False && False && True evalulates to False
and
True || False || False ... evaluetes to True
Statement I evalutes to False because False and any number of And's must evaluate to false because False && X evalutes to False, regardless of the value of X.
Statement II evalutes to True because there is one True statement in a list of OR's that make the entire statement True.
Statement III evalutes to False for the same reason Statement I does.
Statement IV is true because although the conditionals in the parentheses are False, the ! (not) operator negates the False to True.
Statement V is true.
Which of the following Boolean expression evalutes to True?
I. False && False && False && True && False
II. (False && False) || (True || False) || (False && (True || False))
III. (True && False) && (True || False || (True && True))
IV. !(True && True && False)
V. False || False || False || True || False
II, IV, V
I, IV, V
IV
III, IV, V,
IV, V
Answer: II, IV, V
The best way to approach this problem is to divide each conditional into chunks. Then, if there is at least one True in a long list of OR statements, the statement must be true according to Boolean Logic. Likewise, if there is at least one False in a long list of AND's, the statment must be False. For example
False && False && False && True evalulates to False
and
True || False || False ... evaluetes to True
Statement I evalutes to False because False and any number of And's must evaluate to false because False && X evalutes to False, regardless of the value of X.
Statement II evalutes to True because there is one True statement in a list of OR's that make the entire statement True.
Statement III evalutes to False for the same reason Statement I does.
Statement IV is true because although the conditionals in the parentheses are False, the ! (not) operator negates the False to True.
Statement V is true.
Which is true?
a = 4
b = 2
c = 5
d = 6
a) a = c - d
b) a = b - d
c) a = d - b
d) a = c * d
c)
a)
b)
d)
c) is the correct choice because a = 6 - 2 since d = 6 and b = 2. Substitute the numbers for the variables in each answer choice. You will see that all of the answer choices are not equal to 4 except for c).
Consider the following code:
int\[\] a = {8,4,1,5,1,5,6,2,4};
int b = 1;
boolean\[\] vals = new boolean\[a.length\];
for(int i = 0; i < a.length; i++) {
vals\[i\] = (a\[i\] - 1 > 4);
}
for(int i = 0; i < vals.length; i++) {
String s = "Duns Scotus";
if(!vals\[i\]) {
s = "Mithrandir";
}
System.out.println(s);
}
What is the output for the code above?
Duns Scotus
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
None of the others
Begin by getting a general idea of the loop logic being executed. For each execution of the first loop, you are assigning a boolean to the vals array. Now, you could rewrite the code a little to make the math easier:
a\[i\] - 1 > 4
really is the same as:
a\[i\] > 5
Now, the only values for which this is true are:
8 and 6 (the first and the 7th values in a).
Now, looking at the second loop, the value s will be "Duns Scotus" for every value that is true in vals. This is because the if statement checks for !vals\[i\]. Thus, the first and the seventh values alone will be "Duns Scotus". Everything else will be "Mithrandir".
Which is true?
a = 4
b = 2
c = 5
d = 6
a) a = c - d
b) a = b - d
c) a = d - b
d) a = c * d
c)
a)
b)
d)
c) is the correct choice because a = 6 - 2 since d = 6 and b = 2. Substitute the numbers for the variables in each answer choice. You will see that all of the answer choices are not equal to 4 except for c).