AP CSA MCQ Practice - Unit 6 10
AP CSA MCQ Practice - Unit 6 10
Which of the following code segments, if located in a method in the same class as updateArray, will cause
the array myArray to contain {2, 0, 0} after the method call?
(A)
int[] myArray = {1, 0, 0};
updateArray(myArray, 0, 2);
(B)
int[] myArray = {2, 0, 0};
updateArray(myArray, 0, 2);
(C)
int[] myArray = {2, 0, 0};
updateArray(myArray, 0, 0);
(D)
int[] myArray = {0, 2, 0};
updateArray(myArray, 1, 0);
(E)
int[] myArray = {1, 1, 0};
updateArray(myArray, 2, 2);
2. Consider the following method:
Which of the following code segments, when executed in a method in the same class as getSum, will print 85?
(A)
int[] numbers = {60, 25, 10, 5, 0};
System.out.println(getSum(numbers, 0, 2));
(B)
int[] numbers = {50, 35, 20, 15, 5};
System.out.println(getSum(numbers, 1, 2));
(C)
int[] numbers = {40, 30, 25, 15, 10};
System.out.println(getSum(numbers, 0, 4));
(D)
iint[] numbers = {70, 15, 10, 5, 0};
System.out.println(getSum(numbers, 0, 1));
(E)
int[] numbers = {45, 40, 30, 20, 10};
System.out.println(getSum(numbers, 0, 1));
The method is intended to return the number of times the value target appears in the array numbers.
However, it does not work as intended and may cause a runtime error. Which of the following changes should
be made so that the method works correctly?
Which of the following changes will ensure that the code segment works as intended?
5. Consider the following code segment, where nums is a one-dimensional array of integers.
int total = 0;
The code segment is intended to print the sum of the squares of the elements in the array nums. Which of the
following code segments will produce the same output as the code segment above?
(A)
int total = 0;
(B)
int total = 0;
(D)
int total = 0;
(E)
int total = 0;
6. Consider the following incomplete method, which is intended to return the smallest integer in the nums array.
Assume that the array contains at least one element.
return smallest;
}
Which of the following can replace /* missing declaration and initialization */ so that the
method works as intended?
Which of the following can replace /* missing code */ so that the statement compiles without error?
(A) Student()
(B) ArrayList()
(C) ArrayList<Student>()
(D) Student<ArrayList>()
(E) ArrayList<mathClub>()
8. The following method is intended to remove all consecutive duplicate strings from the
ArrayList<String> list. For example, if list contains ["hi", "hi", "hello", "hello",
"hello", "bye"], it should be updated to ["hi", "hello", "bye"].
However, this method does not always work as intended. Which of the following lists will demonstrate a case
where the method does not remove all consecutive duplicates?
elements[possibleIndex] = temp;
}
}
The following declaration and method call appear in a method in the same class as insertionSort.
insertionSort(arr);
How many times is the statement possibleIndex--; in line 10 of the method executed as a result of the
call to insertionSort?
(A) 3
(B) 4
(C) 5
(D) 6
(E) 7
10. Consider the following code segment, where grid is a two-dimensional (2D) array of strings. The code
segment is intended to print the word "BAT".
String[][] grid = {
{"C", "A", "T"},
{"D", "B", "E"},
{"F", "G", "H"}
};
System.out.println( /* missing code */ );
Which of the following could replace /* missing code */ so that the code segment prints "BAT"?
(A) isValidIndex(numbers, 4, 2)
(B) isValidIndex(numbers, 3, 2)
(C) isValidIndex(numbers, -1, 1)
(D) isValidIndex(numbers, 2, 3)
(E) isValidIndex(numbers, 0, -1)
12. Consider the following method, countLetters, which is intended to return the total number of strings in a
two-dimensional String array words that contain the letter "e".
For example, if words contains {{"pen", "pencil"}, {"eraser", "board"}}, then countLetters(words) should return 3.
The method does not always work as intended. Which of the following two-dimensional arrays will demonstrate
that the method does not work correctly?
(A) {{"cat", "dog"}, {"mouse", "rat"}}
(B) {{"one", "two", "three"}, {"four", "five", "six"}}
(C) {{"sky", "cloud"}, {"air", "wind"}}
(D) {{"bee", "wasp"}, {"fly", "gnat"}}
(E) {{"tree", "leaf", "bark"}, {"stem", "root", "bud"}}
13. Consider the following code segment, where target is an integer variable.
int[][] grid = {
{7, 5, 9, 2},
{3, 8, 6, 4},
{1, 2, 5, 7},
{6, 9, 3, 8}
};
(A) 7 9
(B) 9 7
(C) 9 9
(D) 10 9
(E) 6 6
14. Consider the following class definitions.
The following code segment appears in a method in a class other than Vehicle or Car.
Which of the following best describes the result of executing the code segment?
(A) The Car constructor implicitly calls the Vehicle no-argument constructor, which sets make to "Generic", and
then sets year to 2020.
(B) The Car constructor tries to call the one-argument Vehicle constructor, but no value is passed, so a
compile-time error occurs.
(C) The statement causes a runtime error because Vehicle cannot be assigned a Car object.
(D) The Car constructor does not call any Vehicle constructor, so the object is not initialized properly.
(E) The code segment causes a compile-time error because the Car class does not explicitly call a Vehicle
constructor.
15. Consider the following class definitions.
myPet.speak();
Under which of the following conditions will the statement print "Some sound"?
(A) I only
(B) I and II only
(C) I and III only
(D) II and III only
(E) I, II, and III
16. Consider the following class definitions:
int a = 30;
int b = 50;
/* missing code */
Which of the following code segments can be used to replace /* missing code */ so that the output is
50?
(A)
Car c = new Car(a);
c.setSpeed(b);
System.out.println(c.getSpeed());
(B)
Car c = new Car(a);
c.setSpeed(b);
System.out.println(c.getLocalSpeed());
(C)
Car c = new Car(a);
c.setLocalSpeed(b);
System.out.println(c.getSpeed());
(D)
Car c = new Car(b);
c.setSpeed(a);
System.out.println(c.getLocalSpeed());
(E)
Car c = new Car(b);
c.setLocalSpeed(a);
System.out.println(c.getLocalSpeed());
17. Consider the following class definitions:
The following code segment appears in a method in a class other than Animal or Dog.
Which of the following best explains why the code segment does not compile?
(A) The Dog class must override the fetch method from the Animal class.
(B) The variable pet cannot call the speak method because Dog overrides it.
(C) The method fetch is not defined in the Animal class, so it cannot be called using a reference of type
Animal.
(D) The Dog constructor must explicitly call the superclass constructor.
(E) A Dog object cannot be assigned to a reference of type Animal.
18. Consider the following method, which is intended to return the smallest value in the portion of the int
array data that begins at the index start and goes to the end of the array.
/* missing statement */
Which of the following can be used to replace /* missing statement */ so that the minimum method
works as intended?
processString("exam");
(A) m a x e
(B) exam
(C) maxe
(D) maex
(E) amex
20. Consider the following method, which implements a recursive binary search.
(A) 2
(B) 3
(C) 4
(D) 5
(E) -1