wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

AP CSA Review Unit 2

Total questions: 22

Worksheet time: 12mins

Name
Class
Date
1.

Consider the following code segment.

What is printed as a result of executing the code segment?

a)

CS AP12

b)

AP CS3

c)

CSAP 12

d)

APCS 12

e)

APCS 3

2.

Consider the following code segment.

What is printed when the code segment is executed?

a)

2

5

-1

b)

2

5

2

c)

2

6

-1

d)

3

6

-1

e)

-1

-1

2

3.

Consider the following:

What, if anything, is printed when the code segment is executed?

a)

8.0

b)

8.5

c)

9

d)

9.0

e)

Nothing is printed because the code does not compile. The actual parameter d1 passed to calculate is a Double, but the formal parameter x is a double.

4.

The code segment below is intended to randomly print one of the values 2, 4, 6, or 8 with equal probability.

Which of the following can be used to replace /* missing code */ so that the code segment works as intended?

a)

Math.random() * 4 + 1

b)

Math.random() * 8

c)

(int) (Math.random() * 4)

d)

(int) (Math.random() * 4 + 1)

e)

(int) (Math.random() * 8 + 1)

5.

Which of the following is wrapper class in java

a)

int

b)

Integer

c)

integer

d)

String

6.

Consider the following code segment, which is intended to display 6.0.

Which of the following best describes the error, if any, in the code segment?

a)

There are no errors and the code works as intended.

b)

Either the numerator or the denominator of the fraction 1 / 2 should be cast as double.

c)

The expression fact1 * fact 2 should be cast as double.

d)

The expressions 1 / 2 and 3 * 4 should both be cast as double.

e)

The variables fact1 and fact2 should both be declared as int.

7.

Java has 8 primitive data types, which of the following best describes the int datatype.

a)

An int can be stored with 32 bits of memory.

b)

There are methods that can be used with an int.

c)

An int has no maximum or minimum constraints.

d)

An int can hold a wide range of fractional values.

8.

String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


What is the correct way to find the length of "txt" string?

a)

int len = length(txt);

b)

float len = txt.length();

c)

int len = txt.length();

d)

double len = length(txt);

9.

What is the most appropriate way to check if two Strings are identical?

a)

string1 == string2

b)

string1.equals(string2)

c)

string1.same(string2)

d)

string1.equalsIgnorecase(string2)

10.

System.out.println(1 + 2 + "3");

a)

123

b)

6

c)

33

d)

error

11.

How do you start an escape sequence?

a)

/

b)

\

c)

*

d)

//

12.

a)

An instance of the story class is Book.

b)

An instance of the Book object is story.

c)

An attribute of the story object is isHardcover.

d)

An attribute of the pages object is Book.

e)

An attribute of the Book instance is story.

13.

a)

An instance of the vehicle class is Car.

b)

An instance of the Car object is vehicle.

c)

An attribute of the year object is int.

d)

An attribute of the vehicle object is color.

e)

An attribute of the Car instance is vehicle.

14.

a)

origin is an instance of the printXY method.

b)

origin is an instance of the OrderedPair class.

c)

origin is an instance of two int objects.

d)

OrderedPair is an instance of the origin object.

e)

printXY is an instance of the OrderedPair class.

15.

Match the following

a)

0.0 <= x < 10.0

1.

 (Math.random()*10)

b)

Integer 0-99, inclusive

2.

(int) (Math.random()*100)

c)

Integer 0-10, inclusive

3.

(int) (Math.random() * 11)

d)

Integer 1-11, inclusive

4.

(int) (Math.random() * 11 + 1)

e)

Integer 0-19, inclusive

5.

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

16.

Consider the following description of method min.

public int min(int first, int second) – Returns the lesser of its two parameters.

Which of the following lines of code would return the minimum value of p, q, and r

a)

min(min(p, q), r)

b)

min(p, min(q, r))

c)

min(min(p, q), p)

17.

A class contains the two methods detailed below:

public void methodA(int arg) – Calls methodB with the value of arg * 10

public void methodB(int arg) – Displays the value of arg + 10

What would be printed as a result of calling methodA(4)?

a)

14

b)

40

c)

50

d)

140

18.

a)

int result = secret(4, 4);

b)

int result = secret(4, 4.0);

c)

double result = secret(4, 4.0);

d)

double result = secret(4.0, 4);

e)

double result = secret(4.0, 4.0);

19.

How to get the first letter of a String saved in the variable 'abc'?

a)

abc.get(1);

b)

abc[1]

c)

abc.substring(0);

d)

abc.substring(0,1);

20.

How to code a conditional that verifies if a string called 'mystery' contains a website ending in ".com"?

a)

if (mystery.substring(-4,-1) == ".com")

b)

if (mystery.substring(-4,-1).equals(".com"))

c)

if (mystery.substring(mystery.length-4) == ".com")

d)

if (mystery.substring(mystery.length-4).equals(".com"))

21.

Choose the line that assigns "love" to str2.

a)

str2 = str.substring(2,6);

b)

str2 = str.substring(2,5);

c)

str2 = str.substring(2);

d)

str2 = str.substring(4);

22.

Conversion from primitive type to wrapper object is called as

a)

unboxing

b)

autoboxing

c)

wrapping

d)

encapsulation