NEW
Font size
WorksheetsAP CSA Semester 1 Review (Mod 1 - 5)
Total questions: 77
Worksheet time: 2hrs 25mins
5-1-1: What is the value of grade when the following code executes and score is 93?
if (score >= 90)
grade = "A";
if (score >= 80)
grade = "B";
if (score >= 70)
grade = "C";
if (score >= 60)
grade = "D";
else
grade = "E";
A
B
C
D
What does the following code print when x has been set to -5?
if (x < 0)
System.out.println("x is negative");
else if (x == 0)
System.out.println("x is zero");
else
System.out.println("x is positive");
x is negative
x is zero
x is positive
What does the following code print when x has been set to .8?
if (x < .25)
System.out.println("first quartile");
else if (x < .5)
System.out.println("second quartile");
else if (x < .75)
System.out.println("third quartile");
else
System.out.println("fourth quartile");
first quartile
second quartile
third quartile
fourth quartile
What is printed when the following code executes and x has been set to zero?
if (x > 0 && (y / x) == 3)
System.out.println("first case");
else
System.out.println("second case");
first case
second case
You will get a error because you can't divide by zero.
What is printed when the following code executes and x has been set to negative 1?
String message = "help";
if (x >= 0 && message.substring(x).equals("help") System.out.println("first case");
else
System.out.println("second case");
first case
second case
You will get a error because you can't use a negative index with substring.
What is printed when the following code executes and x has been set to zero and y is set to 3?
if ((y / x) == 3 || x = 0)
System.out.println("first case");
else
System.out.println("second case");
first case
second case
You will get a error because you can't divide by zero.
What is printed when the following code executes and x equals 4 and y equals 3?
if (!(x < 3 || y > 2))
System.out.println("first case");
else
System.out.println("second case");
first case
second case
What is printed when the following code executes and x equals 4 and y equals 3?
if (!(x < 3 || y > 2))
System.out.println("first case");
else
System.out.println("second case");
first case
second case
Which of the following is equivalent to the code segment below?
if (x > 2)
x = x * 2;
if (x > 4)
x = 0;
x = 0;
if (x > 2)
x *= 2;
if (x > 2)
x = 0;
if (x > 2)
x = 0;
else
x *= 2;
Assuming that x and y have been declared as valid integer values, which of the following is equivalent to this statement?
(x > 15 && x < 18) || (x > 10 || y < 20)
(x > 15 && x < 18) && (x > 10)
(y < 20) || (x > 15 && x < 18)
((x > 10) || (x > 15 && x < 18)) || (y < 20)
(x < 10 && y > 20) && (x < 15 || x > 18)
Which variable type is NOT a primitive variable type?
boolean
String
int
double
What is a logic error?
An mistyped statement that causes the compiler to throw an exception.
An illegal operation that causes the compiler to throw an exception.
An illegal operation that causes the program to have a run-time error.
An error a programmer makes that causes the program to behave differently than expected.
What is the result of 3/2 in Java?
1.5
1.0
1
0
What is the result of 3.0/2 in Java?
1.5
1.0
1
0
What is the result of 3%2?
1
1.5
1.0
0
What is the result of 2%1?
0
1
2
This cannot be calculated.
What is the long form of x++?
x += x;
x = x + 1;
x + 1;
y = x + 1;
What is the long form of x--?
z = x - y;
x - 1;
x -= x;
x = x - 1;
What is the long form of x+=5?
x = 5;
x = + 5;
x = x + 5;
y = x + 5;
Where does modulus fall within the PEMDAS order of operations?
After multiplication and division
Before multiplication and division
At the same level of multiplication and division
Whenever you feel like it.
If a is of type int and has a value of 0 when the segment is executed, what will happen?
A compile time error will be thrown.
statement2, but not statement1, will be executed.
statement1, but not statement2, will be executed.
A run time error will occur.
What is the value of x at the end of this segment?
1
2
3
4
5
If a % b evaluates to 0, what does that mean about the two variables?
a is greater.
b is greater.
a and b must be equal.
a goes into b.
b goes into a.
Suppose we do,
int y = x % 3;
Where x is a variable that has previously been delcared and assigned. Which of the following CANNOT be a value of y?
0
1
2
3
If a / b evaluates to 0 and both a and b are integers, what does that tell you about a and b?
a is greater
b is greater
a goes into b
b goes into a
They must be equal.
5%7
7%5
System.out.println(2+3.0);
System.out.println(5/2);
System.out.println(5/2.0);
System.out.println("The sum of the numbers is " + sum);
if ( !(x==2) {...}
What is the output of the following program segment?
int num = 5;
while (num >= 0)
{
num -= 2;
}
System.out.println(num);
-2
-1
0
2
21
Which of the following expressions will evaluate to true when x and y are boolean variables with different values?
I. (x || y) && (!x || !y)
II. (x || y) && !(x && y)
III. (x && !y) || (!x && y)
I only
II only
I and II only
II and III only
I, II, and III
Consider the following code segment.
String abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
< missing statement >
System.out.println(abc.substring(k, k + 1));
It is supposed to print a randomly chosen letter of the alphabet. Any of the 26 letters can be chosen with equal probability. Which of the following can replace <missing statement> for the code to work that way?
int k = Math.random(25);
int k = Math.random(0, 25);
int k = Math.random(25) + 1;
int k = Math.random(26) - 1;
int = k = (int)(26*Math.random());
The following code will produce a random number between:
int x = (int)(Math.random() * 5) + 2;
0 and 5
2 and 7
2 and 6
3 and 7
3 and 6
int x = 0;
while (x < 4) {
x = x + 1;
}
System.out.println("x is " + x);
for(int i = 0; i < 10; i = i + 3)
out. print("foo");
What is output by the code?
2
3
4
5
6
What is ouptut by the code?
2
3
4
5
6
12
012
0123
123
01234
104
54
44
35
65
8
10
5
4
6
10 7 4 1 -2
7 4 1 -2 -5 -8
10 7 4 1 -2 -5
10 7 4 1 -2 -5 -8
7 4 1 -2 -5
The code is counting the number of negative numbers between x and y.
The code is counting the number of positive numbers between x and y.
The code is counting the number of odd numbers between x and y.
The code is counting the number of even numbers between x and y.
The code is counting the number of prime numbers between x and y.
I only
II only
I and II only
I and III only
II and III only
I only
II only
I and III only
II and III only
I, II, and III
Given the following code segment, what is in the string referenced by s1?
String s1 = "xy";
String s2 = s1;
s1 = s1 + s2 + "z";
xyz
xyxyz
xy xy z
xy z
z
What is the value of len after the following executes?
String s1 = "Hey, buddy!";
int len = s1.length();
8
10
11
What does the following code print?
System.out.println("13" + 5 + 3);
21
1353
It will give a run-time error
138
It will give a compile-time error
