wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

AP CS A Unit 3

Total questions: 20

Worksheet time: 20mins

Name
Class
Date
1.

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

What is output by the following code?

a)

11

b)

15

c)

31

d)

error

3.

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)

4.

Given the variables, which boolean condition is true?


int a = 10;

int b = 10;

a)

a < b && a == b

b)

a < b && a != b

c)

a > b || a != b

d)

b >= a && a >= b

5.

The following if statement tests the snowfall in Alaska during the months of December, January and February.

Assume low represents a threshold determined to be the least amount of snow required to be normal and high

represents a threshold determined to be the most amount of snow required to be normal and snow represents the

amount of snow in inches for that month. All variables are doubles. It could be replaced with:

a)
b)
c)
6.

The following truth table matches which boolean condition?

a)

A && ( A || B )

b)

!A && ( A || !B )

c)

A && ( A && B )

d)

A || ( A || B )

7.

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

To test if a grade (represented by the double variable g) is a B (between 80 - inclusive - and 90 exclusive) you

would use the following if statement:

What set of three symbols correctly fills in the blanks in the order they appear?


if (g ___ 90 ___ g ___ 90)

a)

>=, &&, <

b)

>, ||, <=

c)

>=, ||, <

d)

<=, &&, >

9.

Consider the following expression. Assume that x and y are properly initialized boolean variables.

The result of evaluating the expression is best described as:


(x || y) && !(x && y)

a)

always true

b)

true only when x is true and y is true

c)

always false

d)

true only when x and y have the same

values

e)

true only when x and y have different

values

10.

Consider the following code fragment, assume val is an int:

Which of the following code fragments would have the same effect?


if ( !(val != 50 && val >= 0))

a)

if (!(val == 50 || val < 0))

b)

if (val == 50 || val < 0)

c)

if (val == 50 || val > 0)

d)

if (val == 50 && val < 0)

11.

Consider the code (assume x and y are both ints):

Which of the following is an example of short circuit evaluation?


if (y != 0 || x * y > 1)

a)

if x * y > 1 is false it evaluates y != 0

b)

if x * y > 1 is false it doesn't evaluate y != 0

c)

if y != 0 is true it doesn't evaluate x * y > 1

d)

f y != 0 is false it doesn't evaluate x * y > 1

12.

The following truth table matches which boolean condition?

a)

A && ( A || B )

b)

A || ( !A && !B )

c)

A && ( A && B )

d)

!A && ( A || B )

13.

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.

14.

Consider the code, assuming that a, b, c and d ints:

Which of the following is an example of short circuit evaluation?


if (a > b && c == d)

a)

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

b)

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

c)

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

d)

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

15.

Look at the following code segment:

What is output when running this code, and why?

a)

EqualEqual is printed because

both if statements are true

b)

Equal is printed because the first if

statement is true

c)

Nothing is printed because the

condition in the if statement is false

d)

Nothing is printed because there is

an error in the code

e)

Equal is printed because the

second if statement is true

16.

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

e)

OneThree

17.

Assume that x and y have been defined and initialized as int values. The expression is equivalent to which of the following?


!( !(x <= y) || (y ==5))

a)

(x <= y) && (y != 5)

b)

(x < y) && (y != 5)

c)

(x >= y) && (y != 5)

d)

(x < y) || (y == 5)

18.

Consider the following Strings:

What will be output after running this code?

a)

Yes1

b)

Yes2

c)

Yes1Yes2

d)

Nothing, There is an error.

19.

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

The result of evaluating the expression above is best described as:


(x || y) || !(x && y)

a)

Always True

b)

Always False

c)

True only when x is true and y is true

d)

True only when x and y have the same value

e)

True only when x and y have different values

20.

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)

equal not equal

d)

Nothing, there is an error