wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

AP CSA Review Set #1

Total questions: 37

Worksheet time: 1hrs 17mins

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.
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;
3.
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
4.
What does the following code print when x has been set to 2000?
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)
 is zero
c)
x is positive
5.

Given the following code segment, which of the following is true?


String s1 = new String("Hi There");

String s2 = new String("Hi There");

String s3 = s1;


I. (s1 == s2)

II. (s1.equals(s2))

III. (s1 == s3)

IV. (s2.equals(s3))

a)

II and IV

b)

II, III, and IV

c)

I, II, III, IV

d)

II only

e)

IV only

6.

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

Check MeCompare me

7.

After the following code is executed, which of I, II and/or III will evaluate to true?


String s1 = "xyz";

String s2 = s1;

String s3 = s2;


I. s1.equals(s3)

II. s1 == s2

III. s1 == s3

a)

I, II, III

b)

I only

c)

II only

d)

III only

e)

II and III only

8.

What is output from the following code?


String s = "Georgia Tech";

String s1 = s.substring(0,7);

String s2 = s1.substring(2);

String s3 = s2.substring(0,3);

System.out.println(s3);

a)

org

b)

eor

c)

eorg

d)

orgi

e)

You will get an index out of bounds exception

9.
Inheritance is used when the relationship between two classes is a(n) _______ relationship.
a)
"HAS-A"
b)
"PART-OF-A"
c)
"IS-A"
d)
inverse
10.

The following Syntax is used for?


class Subclass-name extends Superclass-name

{

//methods and fields

}

a)

Polymorphism

b)

Encapsulation

c)

Inheritance

d)

None of the above

11.

A class that is inherited is called a ______ .

a)

superclass

b)

Subclass

c)

subsetclass

d)

Relativeclass

12.

Class Cat extends Animal. Which is NOT a valid declaration?

a)

Cat x = new Cat();

b)

Animal X = new Animal();

c)

Cat x = new Animal();

d)

Animal x = new Cat();

13.

Class dog implements the animal interface.

Which IS a valid declaration?

a)

Dog d = new Animal();

b)

Animal d = new Dog();

c)

Animal d = new Animal();

14.

Cat class extends Predator class.

Which of the following must be true?

a)

A Cat object has access to all Predator variables and methods.

b)

A Predator object has access to all Cat variables and methods.

c)

Cat c = new Predator();

causes an error.

d)

Predator p = new Cat();

p has access to Cat methods and uses the Predator implementations.

15.
Which of these cannot be passed down through inheritance?
a)
Public variables
b)
Private variables
c)
Methods
d)
Data
16.
In Java, each child class can have only one parent.
a)
True
b)
False
17.

When overloading a method, the method header ________ .

a)

Must exactly match the header of the method you are overloading.

b)

Must have the same name, but differ in order, number, or type of parameters.

c)

Can differ in name, but must have the same number and order of parameters.

d)

Can be completely different from the method you are overloading, but must have @Override.

18.

Which of the following statements would successfully call a superclass constructor?

a)

superclass();

b)

super();

c)

superclass("parameter");

d)

construct();

19.

Which of the following statements is invalid?

a)

Tutor jimmy = new CSTutor();

b)

CSTutor sally = new JavaTutor();

c)

Tutor suzie = new JavaTutor();

d)

Tutor johnny = new Tutor();

20.
What is returned as a result of the call mystery(4,6)?
a)
2
b)
3
c)
4
d)
1
21.

What is the output of this program?

a)

0

b)

1

c)

120

d)

None of the above

22.
What is the returned value of recMethod(5)?
a)
68
b)
70
c)
75
d)
82
23.

What is the output of this program?

a)

24

b)

30

c)

120

d)

720

24.

What is the action of method mystery2?

a)

a+b

b)

a*b

c)

ab

d)

ba

e)

a!

25.
What is printed as a result of the call stringMaker("COMPSCI")?
a)
COMPSCI
COMPSC
COMPS
COMP
COM
CO
C
b)
COMPSCI
OMPSCI
MPSCI
PSCI
SCI
CI
I
c)
CO
COM
COMP
COMPS
COMPSC
COMPSCI
d)
C
CO
COM
COMP
COMPS
COMPSC
COMPSCI
26.

What is the list nums if it is initially [5, 3, 1] and the following code is executed?

nums.add(6);

nums.add(0, 4);

nums.remove(1);

a)

[4, 3, 1, 6]

b)

[5, 3, 1, 6]

c)

[4, 3, 6]

d)

[4, 5, 3, 6]

27.

What index value is used to locate the last element in the nums ArrayList?

a)

nums.size()-1

b)

nums.length()-1

c)

nums.size()

d)

nums.length

28.

Which statement below is the correct way to retrieve the first element in the nums ArrayList?

a)

nums.get(0)

b)

nums[0]

c)

nums(0)

d)

nums[1]

29.

Given the ArrayList nums with the values [3, 7, 6, 0], what code below is the proper way to change the 7 to be a 5?

a)

nums.set(1, 5)

b)

nums.set(7, 5)

c)

nums.set(5, 2)

d)

nums[2] = 5

30.

Given the nums ArrayList with the values [5, 4, 3, 2], what statement below removes the value 3 from the list?

a)

nums.remove(2)

b)

nums.remove(3)

c)

nums.get(2) = null

d)

nums.remove(1)

31.

Given the nums ArrayList with the values [1, 2, 3, 4, 6], what code below can be used to add a 5 between the 4 and 6 in the list?

a)

nums.add(4, 5)

b)

nums.add(5, 4)

c)

nums.add(5, 5)

d)

nums.add(5)

32.

What will print when the following code executes?

ArrayList<Integer> list = new ArrayList<Integer>();

list.add(7);

list.add(8);

list.add(9);

System.out.println(list.remove(0));

a)

7

b)

8

c)

9

d)

0

33.

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

ArrayList<Integer> alist = new ArrayList<Integer>();

alist.add(1);

alist.add(2);

alist.remove(1);

alist.add(1, 3);

alist.set(1, 4);

alist.add(5);

System.out.println(alist);

a)

[1, 4, 5]

b)

[1, 4, 3, 5]

c)

[2, 4, 5]

d)

[2, 4, 3, 5]

34.

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

ArrayList<Double> vals = new ArrayList<Double>();

vals.add(3.5);

vals.add(4.5);

vals.add(5.5);

vals.add( vals.set(0, 2.5) );

System.out.println(vals);

a)

[2.5, 4.5, 5.5, 3.5]

b)

[2.5, 3.5, 4.5, 5.5]

c)

[3.5, 4.5, 5.5, 2.5]

d)

[2.5, 4.5, 5.5]

35.

Which statement is the correct declaration and initialization of an ArrayList of String values?

a)

ArrayList<String> name;

name = new ArrayList<String>();

b)

ArrayList name;

name = new ArrayList<String>();

c)

ArrayList<String> name;

name = ArrayList<String>();

d)

String<ArrayList> name;

name = new String<ArrayList>();

36.

What is printed as a result of executing the following code on an ArrayList of Integers called nums with the values [3, 2, -4, 5, 7]?

for (int idx = 0; idx < nums.size(); idx++)

if (nums.get(idx) % 2 == 0)

nums.add(99);

System.out.println(nums.size());

a)

7

b)

5

c)

6

d)

memory full because too many values added

37.
a)
4 16
b)
4 10 16
c)
0 6 12 18
d)
1 4 7 10 13 16 19