AP Computer Science A › Boolean
Consider the code below:
int[] vals = {841,-14,1,41,49,149,14,148,14};
boolean[] bools = new boolean[vals.length];
for(int i = 0; i < vals.length; i++) {
bools[i] = vals[i] > 20 && vals[i] % 2 == 0;
}
for(int i = 0; i < bools.length; i++) {
if(bools[i]) {
System.out.println(vals[i]);
}
}
What is the output of the code above?
148
148
No output
No output
true
false
false
true
false
true
false
true
false
true
false
false
true
false
true
false
true
false
false
false
false
false
false
false
false
true
false
false
false
false
false
false
false
false
true
false
841
41
49
149
841
41
49
149
This code uses a pair of parallel arrays, one being a boolean containing the result of the logic
vals[i] > 20 && vals[i] % 2 == 0
as applied to each element in the vals
array.
The logic will evaluate to true
when a given value is greater than 20 and also is even. (Remember that the %
is the modulus operator, giving you the remainder of the division by 2 in this case. When this is 0, the division is an even division without remainder. When the divisor is 2, this means the number is divisible by 2—it is even.)
Now, in the second loop, it merely prints the values for which this was true. (This isn't the most efficient algorithm in the world. It is merely trying to test your ability to use parallel arrays and boolean values!) There is only one number in the entire list that fits: 148. Notice, however, that it does not output the boolean values. Those answers are traps that are trying to see if you are not paying attention.
Consider the code below:
int[] vals = {841,-14,1,41,49,149,14,148,14};
boolean[] bools = new boolean[vals.length];
for(int i = 0; i < vals.length; i++) {
bools[i] = vals[i] > 20 && vals[i] % 2 == 0;
}
for(int i = 0; i < bools.length; i++) {
if(bools[i]) {
System.out.println(vals[i]);
}
}
What is the output of the code above?
148
No output
true
false
false
true
false
true
false
true
false
false
false
false
false
false
false
false
true
false
841
41
49
149
This code uses a pair of parallel arrays, one being a boolean containing the result of the logic
vals[i] > 20 && vals[i] % 2 == 0
as applied to each element in the vals
array.
The logic will evaluate to true
when a given value is greater than 20 and also is even. (Remember that the %
is the modulus operator, giving you the remainder of the division by 2 in this case. When this is 0, the division is an even division without remainder. When the divisor is 2, this means the number is divisible by 2—it is even.)
Now, in the second loop, it merely prints the values for which this was true. (This isn't the most efficient algorithm in the world. It is merely trying to test your ability to use parallel arrays and boolean values!) There is only one number in the entire list that fits: 148. Notice, however, that it does not output the boolean values. Those answers are traps that are trying to see if you are not paying attention.
Consider the code below:
int[] vals = {841,-14,1,41,49,149,14,148,14};
boolean[] bools = new boolean[vals.length];
for(int i = 0; i < vals.length; i++) {
bools[i] = vals[i] > 20 && vals[i] % 2 == 0;
}
for(int i = 0; i < bools.length; i++) {
if(bools[i]) {
System.out.println(vals[i]);
}
}
What is the output of the code above?
148
No output
true
false
false
true
false
true
false
true
false
false
false
false
false
false
false
false
true
false
841
41
49
149
This code uses a pair of parallel arrays, one being a boolean containing the result of the logic
vals[i] > 20 && vals[i] % 2 == 0
as applied to each element in the vals
array.
The logic will evaluate to true
when a given value is greater than 20 and also is even. (Remember that the %
is the modulus operator, giving you the remainder of the division by 2 in this case. When this is 0, the division is an even division without remainder. When the divisor is 2, this means the number is divisible by 2—it is even.)
Now, in the second loop, it merely prints the values for which this was true. (This isn't the most efficient algorithm in the world. It is merely trying to test your ability to use parallel arrays and boolean values!) There is only one number in the entire list that fits: 148. Notice, however, that it does not output the boolean values. Those answers are traps that are trying to see if you are not paying attention.
Consider the code below:
int[] vals = {841,-14,1,41,49,149,14,148,14};
boolean[] bools = new boolean[vals.length];
for(int i = 0; i < vals.length; i++) {
bools[i] = vals[i] > 20 && vals[i] % 2 == 0;
}
for(int i = 0; i < bools.length; i++) {
if(bools[i]) {
System.out.println(vals[i]);
}
}
What is the output of the code above?
148
No output
true
false
false
true
false
true
false
true
false
false
false
false
false
false
false
false
true
false
841
41
49
149
This code uses a pair of parallel arrays, one being a boolean containing the result of the logic
vals[i] > 20 && vals[i] % 2 == 0
as applied to each element in the vals
array.
The logic will evaluate to true
when a given value is greater than 20 and also is even. (Remember that the %
is the modulus operator, giving you the remainder of the division by 2 in this case. When this is 0, the division is an even division without remainder. When the divisor is 2, this means the number is divisible by 2—it is even.)
Now, in the second loop, it merely prints the values for which this was true. (This isn't the most efficient algorithm in the world. It is merely trying to test your ability to use parallel arrays and boolean values!) There is only one number in the entire list that fits: 148. Notice, however, that it does not output the boolean values. Those answers are traps that are trying to see if you are not paying attention.
Consider the code below:
int[] vals = {841,-14,1,41,49,149,14,148,14};
boolean[] bools = new boolean[vals.length];
for(int i = 0; i < vals.length; i++) {
bools[i] = vals[i] > 20 && vals[i] % 2 == 0;
}
for(int i = 0; i < bools.length; i++) {
if(bools[i]) {
System.out.println(vals[i]);
}
}
What is the output of the code above?
148
No output
true
false
false
true
false
true
false
true
false
false
false
false
false
false
false
false
true
false
841
41
49
149
This code uses a pair of parallel arrays, one being a boolean containing the result of the logic
vals[i] > 20 && vals[i] % 2 == 0
as applied to each element in the vals
array.
The logic will evaluate to true
when a given value is greater than 20 and also is even. (Remember that the %
is the modulus operator, giving you the remainder of the division by 2 in this case. When this is 0, the division is an even division without remainder. When the divisor is 2, this means the number is divisible by 2—it is even.)
Now, in the second loop, it merely prints the values for which this was true. (This isn't the most efficient algorithm in the world. It is merely trying to test your ability to use parallel arrays and boolean values!) There is only one number in the entire list that fits: 148. Notice, however, that it does not output the boolean values. Those answers are traps that are trying to see if you are not paying attention.
Convert the decimal number 67 to binary.
1000011
1100011
1000101
1001010
1000010
In a regular number system, also known as decimal, the position furtherest to the right is , then one over to the left would be
, then
. These are the ones, tens and hundreds place. Binary is a base 2 number system. Therefore, the digit to the furthest right is
, then to the left
, then
, and so on.
Explanation
1000011 The bolded number has a value of 1
1000011 The bolded number has a value of 2
1000011 The bolded number has a value of 64
The positions that are marked true (as 1) in the binary number 1000011 corresponds to the numbers 64, 2, and 1 which added up equals 67
Step By Step
Convert the decimal number 67 to binary.
1000011
1100011
1000101
1001010
1000010
In a regular number system, also known as decimal, the position furtherest to the right is , then one over to the left would be
, then
. These are the ones, tens and hundreds place. Binary is a base 2 number system. Therefore, the digit to the furthest right is
, then to the left
, then
, and so on.
Explanation
1000011 The bolded number has a value of 1
1000011 The bolded number has a value of 2
1000011 The bolded number has a value of 64
The positions that are marked true (as 1) in the binary number 1000011 corresponds to the numbers 64, 2, and 1 which added up equals 67
Step By Step
Convert the decimal number 67 to binary.
1000011
1100011
1000101
1001010
1000010
In a regular number system, also known as decimal, the position furtherest to the right is , then one over to the left would be
, then
. These are the ones, tens and hundreds place. Binary is a base 2 number system. Therefore, the digit to the furthest right is
, then to the left
, then
, and so on.
Explanation
1000011 The bolded number has a value of 1
1000011 The bolded number has a value of 2
1000011 The bolded number has a value of 64
The positions that are marked true (as 1) in the binary number 1000011 corresponds to the numbers 64, 2, and 1 which added up equals 67
Step By Step
In the following equation, considering the boolean
variables x
, y
, and z
, which of the following combinations of values will evaluate to true
?
(x\,\&\&\,!y)\,||\,((!x\,||\,z)\,\&\&\,!(y\,||\,z))
x == true, y == false, z == false
x == true, y == true, z == true
x == false, y == true, z == false
x == false, y == false, z == true
None of the answers evaluates to true
.
When evaluated with x == true, y == false, z == false_,_
the equation comes out to be
All other combinations of values produce false
.
In the following equation, considering the boolean
variables x
, y
, and z
, which of the following combinations of values will evaluate to true
?
(x\,\&\&\,!y)\,||\,((!x\,||\,z)\,\&\&\,!(y\,||\,z))
x == true, y == false, z == false
x == true, y == true, z == true
x == false, y == true, z == false
x == false, y == false, z == true
None of the answers evaluates to true
.
When evaluated with x == true, y == false, z == false_,_
the equation comes out to be
All other combinations of values produce false
.