wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

AP CSA Semester 1 Review (Mod 1 - 5)

Total questions: 77

Worksheet time: 2hrs 25mins

Name
Class
Date
1.

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)

A

b)

B

c)

C

d)

D

2.

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");

a)

x is negative

b)

x is zero

c)

x is positive

3.

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");

a)

first quartile

b)

second quartile

c)

third quartile

d)

fourth quartile

4.

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");

a)

first case

b)

second case

c)

You will get a error because you can't divide by zero.

5.

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");

a)

first case

b)

second case

c)

You will get a error because you can't use a negative index with substring.

6.

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");

a)

first case

b)

second case

c)

You will get a error because you can't divide by zero.

7.

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");

a)

first case

b)

second case

8.

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");

a)

first case

b)

second case

9.

Which of the following is equivalent to the code segment below?

if (x > 2)

x = x * 2;

if (x > 4)

x = 0;

a)

x = 0;

b)

if (x > 2)

x *= 2;

c)

if (x > 2)

x = 0;

d)

if (x > 2)

x = 0;

else

x *= 2;

10.

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)

a)

(x > 15 && x < 18) && (x > 10)

b)

(y < 20) || (x > 15 && x < 18)

c)

((x > 10) || (x > 15 && x < 18)) || (y < 20)

d)

(x < 10 && y > 20) && (x < 15 || x > 18)

11.

Which variable type is NOT a primitive variable type?

a)

boolean

b)

String

c)

int

d)

double

12.

What is a logic error?

a)

An mistyped statement that causes the compiler to throw an exception.

b)

An illegal operation that causes the compiler to throw an exception.

c)

An illegal operation that causes the program to have a run-time error.

d)

An error a programmer makes that causes the program to behave differently than expected.

13.

What is the result of 3/2 in Java?

a)

1.5

b)

1.0

c)

1

d)

0

14.

What is the result of 3.0/2 in Java?

a)

1.5

b)

1.0

c)

1

d)

0

15.

What is the result of 3%2?

a)

1

b)

1.5

c)

1.0

d)

0

16.

What is the result of 2%1?

a)

0

b)

1

c)

2

d)

This cannot be calculated.

17.

What is the long form of x++?

a)

x += x;

b)

x = x + 1;

c)

x + 1;

d)

y = x + 1;

18.

What is the long form of x--?

a)

z = x - y;

b)

x - 1;

c)

x -= x;

d)

x = x - 1;

19.

What is the long form of x+=5?

a)

x = 5;

b)

x = + 5;

c)

x = x + 5;

d)

y = x + 5;

20.

Where does modulus fall within the PEMDAS order of operations?

a)

After multiplication and division

b)

Before multiplication and division

c)

At the same level of multiplication and division

d)

Whenever you feel like it.

21.
What will be produced by the following statement?
a)
I
b)
II
c)
III
d)
none of these
22.
Expressions follow order of operations rules. What value will result hold after this statement is executed?
a)
-5
b)
0
c)
13
d)
12
23.

If a is of type int and has a value of 0 when the segment is executed, what will happen?

a)

A compile time error will be thrown.

b)

statement2, but not statement1, will be executed.

c)

statement1, but not statement2, will be executed.

d)

A run time error will occur.

24.
Which of the following will evaluate to true only if boolean expressions X, Y, and Z are all false?
a)
!X && !( Y && !Z )
b)
!X || !Y || !Z
c)
!( X || Y || Z )
d)
!X || !( Y || !Z )
25.
Assume x and y are both integers. Using the example expression, which of the following will always evaluate to true?
a)
x > y and y > 0
b)
x > y
c)
x < y
d)
x > y and y < 0
26.
Assume that x, y, and z are all integers. In the example expression, which of the following will guarantee that the expression is true?
a)
z < x is false
b)
z < x is true
c)
x < y is false
d)
z == x * y is true
27.
Assume x, y, and z are all integers. What will happen if x == y is false?
a)
/* statement 1 */ will be executed
b)
/* statement 2 */ will be executed
c)
Either /* statement 1 */ or /* statement 2 */ will be executed
d)
A compile-time error will occur
28.
Assume that counter and sentinel are both integers. Which of the following is true about the following code segments?
a)
I and II are exactly the equivalent for all input values of counter.
b)
I and II are exactly equivalent for all input values of counter >= 1, but differ when counter <= 0
c)
I and II are exactly equivalent only when counter = 0
d)
I and II are exactly equivalent when counter is an even number
29.
Ignoring spacing, what will be the output of the following nested loop?
a)
I
b)
II
c)
III
d)
IV
30.
Which of the following will produce the output shown?
a)
I only
b)
II only
c)
I and III only
d)
I, II, and III
31.
Which of the following is a true statement?
a)
I only
b)
II only
c)
III only
d)
II and III only
32.
Which of the following replacements for /* code segement */ would cause the method to work as intended?
a)
I only
b)
II only
c)
I and II only
d)
II and III only
33.
The following code fragment outputs "13 / 5 = 2.0", however the programmer wanted it to output "13 / 5 = 2.6". Which of the following will NOT correct the error?
a)
double answer = ( double ) 13 / 5;
b)
double answer = 13 / ( double ) 5;
c)
double answer = 13.0 / 5;
d)
double answer = ( double ) ( 13 / 5 );
34.
What values are stored in num1 and num2 after execution of the following program segment?
a)
num1 = 30     num2 = 90
b)
num1 = 30     num2 = -30
c)
num1 = 30     num2 = 60
d)
num1 = 3       num2 = -3
35.
What will be the output after the execution of this code segment?
a)
1     6
b)
7     12
c)
-3    12
d)
4    12
36.
Which of the following pairs of declarations will cause an error message?
a)
none
b)
I only
c)
II only
d)
II and III only
37.
The % symbol returned the following:
a)
The sum of the equation
b)
The remainder 
c)
The difference between two numbers.
d)
The percentage 
38.
To concatenate the programmer must: 
a)
have quotations (" ")
b)
use the plus symbol + 
c)
escape sequences (\t\n)
d)
use System.out.print( );
39.

What is the value of x at the end of this segment?

a)

1

b)

2

c)

3

d)

4

e)

5

40.

If a % b evaluates to 0, what does that mean about the two variables?

a)

a is greater.

b)

b is greater.

c)

a and b must be equal.

d)

a goes into b.

e)

b goes into a.

41.

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?

a)

0

b)

1

c)

2

d)

3

42.

If a / b evaluates to 0 and both a and b are integers, what does that tell you about a and b?

a)

a is greater

b)

b is greater

c)

a goes into b

d)

b goes into a

e)

They must be equal.

43.
How would the following evaluate in Java?
   
5%7
a)
5
b)
7
c)
2
d)
0
44.
How would the following evaluate in Java?
7%5
a)
5
b)
7
c)
2
d)
0
45.
What is the output?
System.out.println(2+3.0);
a)
23.0
b)
5
c)
5.0
d)
23
46.
What is the output?
System.out.println(5/2);
a)
2.5
b)
0.4
c)
2
d)
1
47.
What is the output?                          
System.out.println(5/2.0);
a)
2.5
b)
0.4
c)
2
d)
1
48.
The operator != means:
a)
Equal to
b)
Less than
c)
Not equal to
d)
Greater than or equal to
49.
Boolean logical operators include all of the following except:
a)
!!
b)
!
c)
&&
d)
||
50.
What is the result of: a && b?
a)
True if a is false and false if a is true 
b)
True if a and b are both true and false otherwise
c)
True if a or b or both are true and false otherwise. 
51.
If a, b and c are int variables with a = 5, b = 7, c = 12, then the statement int z = (a * b – c) / a; will result in z equal to 4.
a)
True
b)
False
52.
Which will output "The sum of the numbers is 14"?
a)
System.out.println("The sum of the numbers is 14");
b)
sum = 14;
System.out.println("The sum of the numbers is " + sum);
c)
System.out.println("The sum of the numbers is " + (6 + 8));
d)
all of the above
53.
if x = 2 and y = 6 which of the following would result in the Boolean condition being true?
a)
if ( (x<3) && (y<6) ) {...}
b)
if ( (x<3) || (y<6) ) {...}
c)
if (x > 3) || (y < 6) ) {..}
d)
if ( (x >= 3) && (y <= 6) ) {..}
54.
If x =3, then the boolean condition in the if statement would be 
if ( !(x==2) {...}
a)
true
b)
false
55.
Declaration
a)
•To join two operands end-to-end.
b)
•A statement that creates a new variable and determines its type.
c)
•An error in a program that makes it do something other than what the programmer intended.
d)
•A piece of information a method requires before it can run. Parameters are variables: they contain values and have types.
56.
Casting
a)
•A statement that declares a new variable and assigns a value to it at the same time.
b)
•taking an Object of one particular type and “turning it into” another Object type.
c)
•If you want to truncate to the Ones place: simply cast to int.
d)
•An error in a program that makes it impossible to parse (and therefore impossible to compile).
57.

What is the output of the following program segment?


int num = 5;

while (num >= 0)

{

num -= 2;

}

System.out.println(num);

a)

-2

b)

-1

c)

0

d)

2

e)

21

58.

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)

a)

I only

b)

II only

c)

I and II only

d)

II and III only

e)

I, II, and III

59.

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?

a)

int k = Math.random(25);

b)

int k = Math.random(0, 25);

c)

int k = Math.random(25) + 1;

d)

int k = Math.random(26) - 1;

e)

int = k = (int)(26*Math.random());

60.
What gets printed?
a)
4 16
b)
4 10 16
c)
0 6 12 18
d)
1 4 7 10 13 16 19
61.
a)
I only
b)
I and II only
c)
I and III only
d)
II and III only
62.
a)
1 2 3 4 5
b)
1 2 3 2 1
c)
5 4 3 2 1 1 2 3 4 5
d)
1 2 3 4 5 5 4 3 2 1
63.

The following code will produce a random number between:


int x = (int)(Math.random() * 5) + 2;

a)

0 and 5

b)

2 and 7

c)

2 and 6

d)

3 and 7

e)

3 and 6

64.
What is the output of the following code?
int x = 0;
while (x < 4) {
 x = x + 1;
}
System.out.println("x is " + x);
a)
0
b)
1
c)
3
d)
4
65.
How many times will this loop run?
for(int i = 0; i < 10; i = i + 3)
        out. print("foo");
a)
1
b)
2
c)
3
d)
4
66.

What is output by the code?

a)

2

b)

3

c)

4

d)

5

e)

6

67.

What is ouptut by the code?

a)

2

b)

3

c)

4

d)

5

e)

6

68.
a)

12

b)

012

c)

0123

d)

123

e)

01234

69.
a)

104

b)

54

c)

44

d)

35

e)

65

70.
a)

8

b)

10

c)

5

d)

4

e)

6

71.
a)

10 7 4 1 -2

b)

7 4 1 -2 -5 -8

c)

10 7 4 1 -2 -5

d)

10 7 4 1 -2 -5 -8

e)

7 4 1 -2 -5

72.
a)

The code is counting the number of negative numbers between x and y.

b)

The code is counting the number of positive numbers between x and y.

c)

The code is counting the number of odd numbers between x and y.

d)

The code is counting the number of even numbers between x and y.

e)

The code is counting the number of prime numbers between x and y.

73.
a)

I only

b)

II only

c)

I and II only

d)

I and III only

e)

II and III only

74.
a)

I only

b)

II only

c)

I and III only

d)

II and III only

e)

I, II, and III

75.

Given the following code segment, what is in the string referenced by s1?


String s1 = "xy";

String s2 = s1;

s1 = s1 + s2 + "z";

a)

xyz

b)

xyxyz

c)

xy xy z

d)

xy z

e)

z

76.

What is the value of len after the following executes?


String s1 = "Hey, buddy!";

int len = s1.length();

a)

8

b)

10

c)

11

77.

What does the following code print?


System.out.println("13" + 5 + 3);

a)

21

b)

1353

c)

It will give a run-time error

d)

138

e)

It will give a compile-time error