Boolean

Help Questions

AP Computer Science A › Boolean

Questions 1 - 10
1

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

Explanation

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.

2

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

Explanation

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.

3

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

Explanation

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.

4

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

Explanation

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.

5

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

Explanation

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.

6

Convert the decimal number 67 to binary.

1000011

1100011

1000101

1001010

1000010

Explanation

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

  • To convert the number 67 to binary, first find the digit place number that has the largest number possible that is less than or equal to 67. In this case it would be the position holding .
  • The number for 64 is 1000000
  • To get 67, we need to add 3
  • Again, find the digit place number that has the largest number possible less than or equal to 3. In this case, it would be
  • The number for 66 (64 + 2) is 1000010
  • To get from 66 to 67, repeat the steps from before.
  • The number for 67 (66 + 1) is 1000011
7

Convert the decimal number 67 to binary.

1000011

1100011

1000101

1001010

1000010

Explanation

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

  • To convert the number 67 to binary, first find the digit place number that has the largest number possible that is less than or equal to 67. In this case it would be the position holding .
  • The number for 64 is 1000000
  • To get 67, we need to add 3
  • Again, find the digit place number that has the largest number possible less than or equal to 3. In this case, it would be
  • The number for 66 (64 + 2) is 1000010
  • To get from 66 to 67, repeat the steps from before.
  • The number for 67 (66 + 1) is 1000011
8

Convert the decimal number 67 to binary.

1000011

1100011

1000101

1001010

1000010

Explanation

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

  • To convert the number 67 to binary, first find the digit place number that has the largest number possible that is less than or equal to 67. In this case it would be the position holding .
  • The number for 64 is 1000000
  • To get 67, we need to add 3
  • Again, find the digit place number that has the largest number possible less than or equal to 3. In this case, it would be
  • The number for 66 (64 + 2) is 1000010
  • To get from 66 to 67, repeat the steps from before.
  • The number for 67 (66 + 1) is 1000011
9

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.

Explanation

When evaluated with x == true, y == false, z == false_,_ the equation comes out to be

All other combinations of values produce false.

10

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.

Explanation

When evaluated with x == true, y == false, z == false_,_ the equation comes out to be

All other combinations of values produce false.

Page 1 of 4
Return to subject