WorksheetsAP CS A Unit 3
Total questions: 20
Worksheet time: 20mins
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)
What is output by the following code?
11
15
31
error
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.
(int)(Math.random() * 10) + 20
(int)(Math.random() * 10) + 25
(int)(Math.random() * 10) + 30
(int)(Math.random() + 30)
Given the variables, which boolean condition is true?
int a = 10;
int b = 10;
a < b && a == b
a < b && a != b
a > b || a != b
b >= a && a >= b
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:
The following truth table matches which boolean condition?
A && ( A || B )
!A && ( A || !B )
A && ( A && B )
A || ( A || B )
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)
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)
>=, &&, <
>, ||, <=
>=, ||, <
<=, &&, >
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)
always true
true only when x is true and y is true
always false
true only when x and y have the same
values
true only when x and y have different
values
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))
if (!(val == 50 || val < 0))
if (val == 50 || val < 0)
if (val == 50 || val > 0)
if (val == 50 && val < 0)
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)
if x * y > 1 is false it evaluates y != 0
if x * y > 1 is false it doesn't evaluate y != 0
if y != 0 is true it doesn't evaluate x * y > 1
f y != 0 is false it doesn't evaluate x * y > 1
The following truth table matches which boolean condition?
A && ( A || B )
A || ( !A && !B )
A && ( A && B )
!A && ( A || B )
Consider the following code, assuming word is a string:
What does the code do?
Tests if the String word contains only
non-letter characters.
Tests if the String word contains only
upper-case letters.
Tests if the String word contains only
numeric values.
Tests if the String word contains
only lower-case letters.
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)
if a > b is false it doesn't evaluate c == d
if a > b is true it doesn't evaluate c == d
if c == d is false it evaluates a > b
if c == d is true it doesn't evaluate a > b
Look at the following code segment:
What is output when running this code, and why?
EqualEqual is printed because
both if statements are true
Equal is printed because the first if
statement is true
Nothing is printed because the
condition in the if statement is false
Nothing is printed because there is
an error in the code
Equal is printed because the
second if statement is true
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:
One
Two
Three
OneTwo
OneThree
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))
(x <= y) && (y != 5)
(x < y) && (y != 5)
(x >= y) && (y != 5)
(x < y) || (y == 5)
Consider the following Strings:
What will be output after running this code?
Yes1
Yes2
Yes1Yes2
Nothing, There is an error.
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)
Always True
Always False
True only when x is true and y is true
True only when x and y have the same value
True only when x and y have different values
What is output by the following code segment? Assume a Triangle class exists and each triangle is correctly
created.
equal
not equal
equal not equal
Nothing, there is an error
