wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

AP CS A Midterm Review

Total questions: 37

Worksheet time: 41mins

Name
Class
Date
1.
a)
4 16
b)
4 10 16
c)
0 6 12 18
d)
1 4 7 10 13 16 19
2.

What is output by the code?

a)

0

b)

1

c)

2

d)

3

e)

4

3.

What is output by the code?

a)

0

b)

1

c)

2

d)

3

e)

4

4.
a)
[P, Q, R, s, T]
b)
[P, Q, s, T, u]
c)
[P, Q, T, s, u]
d)
[P, T, s, R, u]
5.
a)
I)    while(count <= numRolls && Math.random() < 1/6.0)     count++;
b)
II)    for(int roll = 0; roll < numRolls; roll++)   {     if(Math.random() < 1/6.0)     count++;   }
c)
III)  for(int roll = 0; roll < numRolls; roll++)   {     if((int)(6 * Math.random() + 1) == 1)     count++;   }
d)
II and III
6.
a)
1  0  0  0  0
0  1  0  0  0
0  0  1  0  0
0  0  0  1  0
0  0  0  0  1
b)
0  0  0  0  1
0  0  0  1  0
0  0  1  0  0
0  1  0  0  0
1  0  0  0  0
c)
0  0  1  0  0
0  0  1  0  0
1  1  1  1  1
0  0  1  0  0
0  0  1  0  0
d)
0  0  0  1  0
0  0  0  1  0
0  0  0  1  0
1  1  1  1  1
0  0  0  1  0
7.
If you wanted to check to see if two numerical values were equal, which operator would you use?
a)
=
b)
!=
c)
equals
d)
==
8.
How would you create a new array of 5 integers?
a)
int[5] x;
b)
new int[5] x;
c)
int[] x = new int[5];
d)
int x = new int[5];
9.
How would you create a new array of 3 Strings?
a)
String[3];
b)
new String[3];
c)
String[] x = 3
d)
String[] x = new String[3];
10.

1. Consider the following code segment.


System.out.println("\"This is my output.\"");

a)

This is my output.

b)

"This is my output."

c)

"\"This is my output.\""

d)

\"This is my output.\"

e)

Nothing, it will produce an error.

11.

Which of the following is correct

a)

public class MyFile {

b)

Public class MyFile {

c)

public Class MyFile {

12.

Which of the following is NOT an escape sequence?

a)

\n

b)

/n

c)

\\

d)

//

13.
________ methods are accessible to all classes and client programs.
a)
a. private
b)
b. public
c)
c. setter
d)
d. getter
14.

The body of a method definition is contained within a set of ______________.

a)

parentheses

b)

rectangle brackets

c)

curly braces

d)

angle brackets

15.

Methods that contain the _______ keyword in the data type position do not return any data, so they can simply be called without being part of a larger statement.

a)

void

b)

static

c)

public

d)

boolean

16.

What is the output?

String s="hello";

s.substring(1,3);

a)

"ell"

b)

"hel"

c)

"he"

d)

"el"

17.

What is the output?

String s = "hello";

s.substring(1);

a)

"h"

b)

"e"

c)

"ello"

d)

"hello"

18.

What is the output?

String s = "hello";

s.substring(1,7);

a)

"hello"

b)

""

c)

index out of bounds

d)

"ello "

19.

What is the output?

String s = "hello";

s.substring(2,2);

a)

""

b)

"l"

c)

"e"

d)

index out of bounds

20.

What are the values of x, y, and z after this code executes?

a)

x = 6, y = 2.5, z = 2

b)

x = 4, y = 2.5, z = 2

c)

x = 6, y = 2, z = 3

d)

x = 4, y = 2.5, z = 3

e)

x = 4, y = 2, z = 3

21.

What are the values of x, y, and z after this code executes?

a)

x = -1, y = 1, z = 4

b)

x = -1, y = 2, z = 3

c)

x = -1, y = 2, z = 2

d)

x = -1, y = 2, z = 2

e)

x = -1, y = 2, z = 4

22.

Which of the following returns the correct average when 3 values had been added to an integer total?

a)

(double) (total / 3);

b)

total / 3;

c)

(double) total / 3;

23.

Evaluate 2 % 3

a)

2

b)

0

c)

3

d)

1

24.

Evaluate 1 / 3

a)

0.3333333333333333

b)

0

c)

It will give a run-time error

d)

0.3

e)

It will give a compile-time error

25.

Evaluate 2 + 3 * 5 - 1

a)

24

b)

14

c)

This will give a compile time error.

d)

16

26.

What is the value of b after this code executes?

a)

9.6982

b)

12

c)

10

d)

9

27.

Evaluate

(double) 5 / 2

a)

2.0

b)

2

c)

2.5

d)

error - incompatible types

28.

Evaluate

(double) ( 7 / 2 )

a)

3

b)

3.0

c)

3.5

d)

error - incompatible types

29.

Consider the following code segment:

int x = 3;

double y = Math.pow(x, 3);

double z = Math.sqrt(y - Math.abs(-2));

What is the value of z after this code segment has executed?

a)

0.0

b)

3.0

c)

5.0

d)

9.0

e)

10.0

30.

What does the expression "Nomnomnom yummy turkey".indexOf("Thank") evaluate to?

a)

Thank

b)

Nomnomnom

c)

turkey

d)

-1

e)

0

31.

Which of the following would correctly define and instantiate a Scanner?

a)

Scanner kb = Scanner(in);

b)

Scanner kb = new Scanner(in);

c)

Scanner new kb = Scanner(in);

d)

Scanner kb = new kb(in);

e)

none of these

32.

Which of the following Scanner methods can be used to read in a int?

a)

nextInt()

b)

next()

c)

nextInts()

d)

nextDouble()

e)

nextInteger()

33.

Which of the following Scanner methods can be used to read in a double?

a)

nextInt()

b)

nextDble()

c)

nextDouble()

d)

next()

e)

nextLine()

34.

Which of the following Scanner methods can be used to read in a String?

a)

nextInt()

b)

nextWord()

c)

nextDouble()

d)

next()

e)

nextLine()

35.

Which of the following Scanner methods can be used to read in a line of words?

a)

nextInt()

b)

nextDble()

c)

nextDouble()

d)

next()

e)

nextLine()

36.

Given the following statement, var must be defined as which of the following types?

var = keyboard.nextInt();

a)

short

b)

String

c)

double

d)

int

37.

Given the following statement, var must be defined as which of the following types?

var = keyboard.nextLine();

a)

short

b)

String

c)

double

d)

int