Computer Science A: Sample Multiple-Choice Questions
Following is a representative set of questions. Questions marked with an asterisk are also representative of Exam questions. The AP Computer Science A Exam will include several multiple-choice questions based on the Computer Science Case Study.
Directions: Determine the answer to each of the following questions or incomplete statements, using the available space for any necessary scratchwork. Then decide which is the best of the choices given and fill in the corresponding oval on the answer sheet. No credit will be given for anything written in the examination booklet. Do not spend too much time on any one problem.
1. Consider the following code segment.
for (int k 5 0; k , 20; k 5 k 1 2)
{
if (k % 3 55 1)
System.out.print(k 1 " ");
}
What is printed as a result of executing the code segment?
(a) 4 16
(b) 4 10 16
(c) 0 6 12 18
(d) 1 4 7 10 13 16 19
(e) 0 2 4 6 8 10 12 14 16 18
2. Consider the following code segment.
ArrayList<String> list 5 new ArrayList<String>();
list.add("P");
list.add("Q");
list.add("R");
list.set(2, "s");
list.add(2, "T");
list.add("u");
System.out.println(list);
What is printed as a result of executing the code segment?
(a) [P, Q, R, s, T]
(b) [P, Q, s, T, u]
(c) [P, Q, T, s, u]
(d) [P, T, Q, s, u]
(e) [P, T, s, R, u]
3. Consider the following instance variable and method.
private ArrayList<Integer> nums;
/ ** Precondition: nums.size . 0
*/
public void numQuest()
{
int k 5 0;
Integer zero 5 new Integer(0);
while (k , nums.size())
{
if (nums.get(k).equals(zero))
nums.remove(k);
k11;
}
}
Assume that ArrayList nums initially contains the following
Integer values.
[0, 0, 4, 2, 5, 0, 3, 0]
What will ArrayList nums contain as a result of executing numQuest ?
(a) [0, 0, 4, 2, 5, 0, 3, 0]
(b) [4, 2, 5, 3]
(c) [0, 0, 0, 0, 4, 2, 5, 3]
(d) [3, 5, 2, 4, 0, 0, 0, 0]
(e) [0, 4, 2, 5, 3]
4. At a certain high school students receive letter grades based on the following
scale.
Numeric Score Letter Grade
93 or above A
From 84 to 92 inclusive B
From 75 to 83 inclusive C
Below 75 F
Which of the following code segments will assign the correct string to grade
for a given integer score ?
I. if (score .5 93)
grade 5 "A";
if (score .5 84 && score ,5 92)
grade 5 "B";
if (score .5 75 && score ,5 83)
grade 5 "C";
if (score , 75)
grade 5 "F";
II. if (score .5 93)
grade 5 "A";
if (84 ,5 score ,5 92)
grade 5 "B";
if (75 ,5 score ,5 83)
grade 5 "C";
if (score , 75)
grade 5 "F";
III. if (score .5 93)
grade 5 "A";
else if (score .5 84)
grade 5 "B";
else if (score .5 75)
grade 5 "C";
else
grade 5 "F";
(a) II only
(b) III only
(c) I and II only
(d) I and III only
(e) I, II, and III
5. Consider the following output.
1 1 1 1 1
2 2 2 2
3 3 3
4 4
5
Which of the following code segments will produce this output?
(a) for (int j 5 1; j ,5 5; j11)
{
for (int k 5 1; k ,5 5; k11)
{
System.out.print(j 1 " ");
}
System.out.println();
}
(b) for (int j 5 1; j ,5 5; j11)
{
for (int k 5 1; k ,5 j; k11)
{
System.out.print(j 1 " ");
}
System.out.println();
}
(c) for (int j 5 1; j ,5 5; j11)
{
for (int k 5 5; k .5 1; k22 )
{
System.out.print(j 1 " ");
}
System.out.println();
}
(d) for (int j 5 1; j ,5 5; j11)
{
for (int k 5 5; k .5 j; k22 )
{
System.out.print(j 1 " ");
}
System.out.println();
}
(e) for (int j 5 1; j ,5 5; j11)
{
for (int k 5 j; k ,5 5; k11)
{
System.out.print(k 1 " ");
}
System.out.println();
}
6. A car dealership needs a program to store information about the cars for sale.
For each car, they want to keep track of the following information: number of
doors (2 or 4), whether the car has air conditioning, and its average number of
miles per gallon. Which of the following is the best design?
(a) Use one class, Car, which has three data fields:
int numDoors, boolean hasAir, and
double milesPerGallon.
(b) Use four unrelated classes: Car, Doors, AirConditioning, and
MilesPerGallon.
(c) Use a class Car which has three subclasses: Doors, AirConditioning,
and MilesPerGallon.
(d) Use a class Car, which has a subclass Doors, with a subclass
AirConditioning, with a subclass MilesPerGallon.
(e) Use three classes: Doors, AirConditioning, and MilesPerGallon,
each with a subclass Car.
7. Consider the following declarations.
public interface Comparable
{
int compareTo(Object other);
}
public class SomeClass implements Comparable
{
// ... other methods not shown
}
Which of the following method signatures of compareTo will satisfy the
Comparable interface requirement?
I II. public int compareTo(Object other)
I II. public int compareTo(SomeClass other)
III. public boolean compareTo(Object other)
(a) I only
(b) II only
(c) III only
(d) I and II only
(e) I, II, and III
No comments:
Post a Comment