NEW
Font size
S
M
L
XL
WorksheetsAP CS practice 2
Total questions: 12
Worksheet time: 36mins
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.
a)
[P, Q, R, s, T]
b)
[P, Q, s, T, u]
c)
[P, Q, T, s, u]
d)
[P, T, s, R, u]
3.
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
4.
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
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
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
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
0 0 0 1 0
0 0 0 1 0
1 1 1 1 1
0 0 0 1 0
5.
a)
I) myName + " " + myTeam + " " + myNumber;
b)
II) SoccerPlayer.name() + " " + SoccerPlayer.team() + “ “ + SoccerPlayer.number();
c)
III) name() + " " + team() + " " + number();
d)
I and III
6.
The following code segment is intended to sum the first 10 positive odd integers.
sum = 0;
for(int k = 1; k <= 10; k++) {
sum += 2*k + 1; }
Which of the following best describes the error, if any, in this code.
sum = 0;
for(int k = 1; k <= 10; k++) {
sum += 2*k + 1; }
Which of the following best describes the error, if any, in this code.
a)
The segment works as intended.
b)
The segment sums the first 9 odd integers
c)
The segment leaves out the first odd integer and includes the eleventh odd integer in the sum.
d)
The variable sum is incorrectly initialized. The segment would work as intended if sum were initialized to 1.
7.
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
8.
a)
The value at index 5 in list, or -1 if list.length < 5.
b)
The index of the first occurrence of 5 in list, or -1 if 5 does not occur in list.
c)
The index of the last occurrence of 5 in list, or -1 if 5 does not occur in list.
d)
The value at index list.length-1 in list, or -1 if list.length < 5.
9.
a)
I only
b)
I and II only
c)
I, II, and II
d)
II and III only
10.
a)
alex brad nils pete
b)
alex brad nils pete pete
c)
alex brad nils nils pete pete
d)
alex brad nils nils pete pete pete
11.
a)
A
b)
B
c)
C
d)
D
12.
a)
E
b)
B
c)
C
d)
D
Reset
