Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

AP CSA Unit 3 Boolean Expressions

Total questions: 30

Worksheet time: 15mins

Name
Class
Date
1.

Consider the code assuming that a b c and d are ints, which of the following is an example of short circuit evaluation?

a)

if a > b is true it doesn't evaluate c == d

b)

if c == d is false it evaluates a > b

c)

if a > b is false it doesn't evaluate c == d

d)

if c == d is true it doesn't evaluate a > b

2.

Just before exiting this program, what are the values of the instance variable value of MultiplyObjects a, b and c?

a)

5, 10, 20

b)

40, 40, 40

c)

40, 10, 40

d)

10, 10, 20

3.

The boolean expression (A || B) && A is true

a)

whenever A is true

b)

whenever B is true

c)

whenever either A is true or B is true

d)

whenever both A is true and B is true

e)

In all cases

4.

The boolean expression (A && B) && B is true

a)

whenever A is true

b)

whenever B is true

c)

whenever either a is true or B is true

d)

whenever both A is true and B is true

e)

In all cases

5.

For what values of n with this code segment generate output?

a)

for all values of n

b)

for all even values of n

c)

for all values of n that are multiples of 3

d)

for all values of n that are multiples of 6

e)

for all values of n that are either a multiple of 2 or a multiple of 3.

6.

What is printed as a result of executing the program?

a)

10 20

10 20

b)

10 20

20 10

c)

10 10

20 20

d)

20 20

20 20

e)

There is no output displayed.

7.

For what values of n will the code segment generate output?

a)

For all values of n

b)

For all values of n in the range [20 .. 100]

c)

For all values of n in the range [51 .. 99]

d)

For all values of n NOT in the range of [50 .. 100]

e)

For al values of n NOT in the range of [51 .. 99]

8.

For what values of n will this code segment generate output?

a)

for all values of n

b)

for all values of n which are 100 or greater

c)

for all values of n which are 100 or less

d)

for all values of n NOT equal to 100

e)

for n == 100 only

9.

What kind of statement groups several statements together?

a)

simple

b)

boolean

c)

block

d)

condition

10.

Which if condition correctly checks whether there are sufficient funds in the bank account to withdraw "amount"?

a)

if (balance = amount)

b)

if {balance >= amount}

c)

if [balance >= amount]

d)

if (balance >= amount)

11.

Which java statement correctly implements the following pseudocode: display "A" when the student's grade is higher than 90 (inclusive)

a)

if (studentGrade = 90)

b)

if (studentGrade > 90)

c)

if (studentGrade >= 90)

d)

if (studentGrade < 90)

12.

Which statement is true about the following code fragment involving a conditional?

a)

Never prints out "Insufficient funds".

b)

Does not compile.

c)

Only prints out "Insufficient funds" when amount > balance

d)

Always prints out "Insufficient funds"

13.

Consider the following code, assuming grade is an int:

Which of the following will always produce the same result as the code above, regardless of the value of grade? (multiple correct answers)

a)
b)
c)
14.

What is output by the following code?

a)

11

b)

31

c)

15

d)

error

15.

Consider the following incomplete code segment.

Consider the following potential replacements for /* missing code */ . Which of these replacements will cause

the code segment to always print false ? Assume that Math is imported correctly into our program.

a)

(int)(Math.random() * 10) + 20

b)

(int)(Math.random() * 10) + 25

c)

(int)(Math.random() * 10) + 30

d)

(int)(Math.random() + 30)

16.

Which condition should be used to test whether the contents of two strings referenced by string1 and string2 have the same value?

a)

(string1.equals(string2))

b)

(String.equals(string1, string2))

c)

(string1 == string2)

d)

(Equals(string1, string2))

17.

Which String method compares strings in dictionary (lexigraphical) order?

a)

compare

b)

compareDictionary

c)

equals

d)

compareTo

18.

If string1 and string2 are equal, then string1.compareTo(string2)___________. Fill in the blank with the correct option.

a)

.equals(0)

b)

== 0

c)

== true

d)

== null

19.

Which choice denotes an empty string?

a)

empty

b)

""

c)

nil

d)

null

20.

Which operator or method tests whether two object references are identical?

a)

==

b)

equals

c)

=

d)

identity

21.

Which operator or method should you use to determine whether the contents of objects have the same value?

a)

==

b)

equals

c)

=

d)

same

22.

Which of the following statements determines whether a person is ineligible for a junior or senior discount, assuming that a person is eligible for the discount if they are younger than 18 or older than 62?

a)

age >= 18 && age > 62

b)

age >= 18 || age <= 62

c)

age <= 18 || age >= 62

d)

age >= 18 && age <= 62

23.

Which of the following conditions tests whether x is NOT between 1 and 10 (inclusive)?

a)

10 < x || x < 1

b)

!(1 <= x <= 10)

c)

1 <= x || x <= 10

d)

1 > x && x <= 10

24.

The following truth table matches which boolean condition?

a)

A && ( A || B )

b)

!A && ( A || !B )

c)

A && ( A && B )

d)

A || ( A || B )

25.

A weather app records the current temperature to the nearest degree Fahrenheit using an int variable temp.

The app also uses a String variable tempAdj to store an adjective based on the value of temp which will be used

in a short written summary of the current weather. The values of tempAdj are determined as follows:


Which of the following code segments will correctly set the value of tempAdj for any given value of temp? (Multiple correct answers)

a)
b)
c)
26.

Consider the following code, assuming word is a string:

What does the code do?

a)

Tests if the String word contains only

non-letter characters.

b)

Tests if the String word contains only

upper-case letters.

c)

Tests if the String word contains only

numeric values.

d)

Tests if the String word contains

only lower-case letters.

27.

Assume that x and y are boolean variables and have been properly initialized.

What will be output if the value of x is true and the value of y is false:

a)

One

b)

Two

c)

Three

d)

OneTwo

28.

What is output by the following code segment? Assume a Triangle class exists and each triangle is correctly

created.

a)

equal

b)

not equal

c)

equalnot equal

d)

Nothing, there is an error

29.

What is the value of the variable condition in the following code?

a)

true

b)

The code has a syntax error and will not compile.

c)

false

d)

The code compiles but has a logic error.

30.

What is the value of condition assuming that two circles are considered equal if they have the same radius value? (The value in the parenthesis is the radius of the circle.)

a)

false

b)

true

c)

The code compiles but has a logic error.

d)

The code has a syntax error and will not compile.