[go: up one dir, main page]

0% found this document useful (0 votes)
34 views15 pages

AP CSA MCQ Practice - Unit 6 10

The document presents a series of multiple-choice questions related to Java programming concepts, including array manipulation, method functionality, and class inheritance. Each question provides code snippets and asks the reader to identify correct code segments or modifications to achieve specific outcomes. The questions cover various topics such as method parameters, loops, and object-oriented programming principles.

Uploaded by

sehyeonjang5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views15 pages

AP CSA MCQ Practice - Unit 6 10

The document presents a series of multiple-choice questions related to Java programming concepts, including array manipulation, method functionality, and class inheritance. Each question provides code snippets and asks the reader to identify correct code segments or modifications to achieve specific outcomes. The questions cover various topics such as method parameters, loops, and object-oriented programming principles.

Uploaded by

sehyeonjang5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

AP CSA MCQ Practice (Unit 6-10)

Time Limit: 45 minutes

1. Consider the following method:

public void updateArray(int[] arr, int index, int value) {


arr[index] *= value;
}

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:

public static int getSum(int[] nums, int a, int b) {


return nums[a] + nums[b];
}

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));

3. Consider the following method:

public static int countMatches(int[] numbers, int target) {


int count = 0;
for (int i = 0; i <= numbers.length; i++) {
if (numbers[i] == target) {
count++;
}
}
return count;
}

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?

(A) Change int i = 0; to int i = 1; in the for loop header


(B) Change i <= numbers.length; to i < numbers.length; in the for loop header
(C) Change i <= numbers.length; to i < numbers.length - 1; in the for loop header
(D) Change i <= numbers.length; to i < numbers.length + 1; in the for loop header
(E) No change is needed; the method works as intended
4. Consider the following code segment, which is intended to update the array minVals so that each element
of minVals is the smaller of the two values at the corresponding indices in data1 and minVals. The array
data1 should remain unchanged.

for (int i = 0; i < data1.length; i++) {


if (data1[i] < minVals[i]) {
minVals[i] = data1[i];
} else {
data1[i] = minVals[i];
}
}

Which of the following changes will ensure that the code segment works as intended?

(A) Change i < data1.length to i <= data1.length in the loop header


(B) Change if (data1[i] < minVals[i]) to if (data1[i] > minVals[i])
(C) Remove lines 3–6
(D) Swap the positions of data1[i] = minVals[i]; and minVals[i] = data1[i];
(E) Remove the else block

5. Consider the following code segment, where nums is a one-dimensional array of integers.

int total = 0;

for (int value : nums) {


total += value * value;
}
System.out.print(total);

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;

for (int i = 0; i < nums.length; i++) {


total += i * i;
}
System.out.print(total);

(B)
int total = 0;

for (int i = 1; i <= nums.length; i++) {


total += nums[i] * nums[i];
}
System.out.print(total);
(C)
int total = 0;

for (int i = 0; i < nums.length; i++) {


total += nums[i];
}
System.out.print(total);

(D)
int total = 0;

for (int i = 0; i < nums.length; i++) {


total += nums[i] * nums[i];
}
System.out.print(total);

(E)
int total = 0;

for (int i = 0; i <= nums.length; i++) {


total += nums[i] * nums[i];
}
System.out.print(total);

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.

public static int smallestNum(int[] nums) {

/* missing declaration and initialization */

for (int i = 1; i < nums.length; i++) {


if (nums[i] < smallest) {
smallest = nums[i];
}
}

return smallest;
}

Which of the following can replace /* missing declaration and initialization */ so that the
method works as intended?

(A) int smallest = 0;


(B) int smallest = nums.length;
(C) int smallest = nums[1];
(D) int smallest = nums[0];
(E) int smallest = Integer.MAX_VALUE;
7. Consider the following statement, which is intended to create an ArrayList named mathClub to store
elements of type Student. Assume the Student class has been properly defined.

ArrayList<Student> mathClub = new /* missing code */;

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"].

public static void removeConsecutiveDuplicates(ArrayList<String> list) {


for (int i = 0; i < list.size() - 1; i++) {
if (list.get(i).equals(list.get(i + 1))) {
list.remove(i);
i++;
}
}
}

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?

(A) ["a", "a", "b", "b", "c"]


(B) ["a", "b", "b", "c", "d"]
(C) ["a", "a", "a", "b", "c"]
(D) ["a", "b", "c", "c", "d"]
(E) ["a", "b", "c", "d", "e"]
9. Consider the following correct implementation of the insertion sort algorithm.

public static void insertionSort(int[] elements) {


for (int j = 1; j < elements.length; j++) {
int temp = elements[j];
int possibleIndex = j;

while (possibleIndex > 0 && temp < elements[possibleIndex - 1]) {


elements[possibleIndex] = elements[possibleIndex - 1];
possibleIndex--; // Line 10
}

elements[possibleIndex] = temp;
}
}

The following declaration and method call appear in a method in the same class as insertionSort.

int[] arr = {9, 3, 7, 1, 5};

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) grid[1][1] + grid[0][1] + grid[0][2]


(B) grid[1][1] + grid[1][0] + grid[0][2]
(C) grid[0][1] + grid[1][1] + grid[0][2]
(D) grid[0][2] + grid[1][1] + grid[0][1]
(E) grid[1][1] + grid[2][2] + grid[0][0]
11. Consider the following method, which is intended to determine whether the given row and column indexes
are within the bounds of a 2D array matrix.

public boolean isValidIndex(int[][] matrix, int row, int col)


{
if (row >= 0 && row < matrix.length)
{
if (col >= 0 && col < matrix[0].length)
{
return true;
}
}
return false;
}

Suppose the following declaration is made in the same class:

int[][] numbers = new int[4][3];

Which of the following method calls returns true?

(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".

public static int countLetters(String[][] words) {


int total = 0;
for (int row = 0; row < words.length; row++) {
for (int col = 0; col < words[row].length - 1; col++) {
if (words[row][col].indexOf("e") >= 0) {
total++;
}
}
}
return total;
}

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}
};

for (int row = 0; row < grid.length; row++) {


for (int col = 0; col < grid[0].length; col++) {
if (grid[row][col] == target) {
System.out.print(row + col + grid[row][col] + " ");
}
}
}

What is printed when target has the value 6?

(A) 7 9
(B) 9 7
(C) 9 9
(D) 10 9
(E) 6 6
14. Consider the following class definitions.

public class Vehicle {


private String make;
public Vehicle() {
make = "Generic";
}
public Vehicle(String m) {
make = m;
}
}
public class Car extends Vehicle {
private int year;
public Car(int y) {
year = y;
}
}

The following code segment appears in a method in a class other than Vehicle or Car.

Vehicle v = new Car(2020);

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.

public class Animal {


public void speak() {
System.out.println("Some sound");
}
}

public class Dog extends Animal {


public void speak() {
System.out.println("Bark");
}
}

public class Cat extends Animal {


// No speak method defined
}

The following statement appears in a method in another class:

myPet.speak();

Under which of the following conditions will the statement print "Some sound"?

I. When myPet is an object of type Animal


II. When myPet is an object of type Dog​
III. When myPet is an object of type Cat

(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:

public class Vehicle {


private int speed;

public int getSpeed() {


return speed;
}

public void setSpeed(int s) {


speed = s;
}
}

public class Car extends Vehicle {


private int speed;

public Car(int initialSpeed) {


setSpeed(initialSpeed);
}

public int getSpeed() {


return super.getSpeed();
}

public int getLocalSpeed() {


return speed;
}

public void setSpeed(int s) {


super.setSpeed(s);
}

public void setLocalSpeed(int s) {


speed = s;
}
}

The following code segment appears in another class:

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:

public class Animal {


public void speak() {
System.out.println("Some sound");
}
}

public class Dog extends Animal {


public void speak() {
System.out.println("Bark");
}

public void fetch() {


System.out.println("Fetching...");
}
}

The following code segment appears in a method in a class other than Animal or Dog.

Animal pet = new Dog();


pet.speak();
pet.fetch();

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.

/** Precondition: 0 <= start < data.length */


public int minimum(int[] data, int start)
{
if (start == data.length - 1)
{
return data[start];
}

/* missing statement */

if (val < data[start])


{
return val;
}
else
{
return data[start];
}
}

Which of the following can be used to replace /* missing statement */ so that the minimum method
works as intended?

(A) int val = minimum(data, start);​


(B) int val = minimum(data, start - 1);​
(C) int val = minimum(data, start + 1);​
(D) int val = minimum(data[start - 1], start);​
(E) int val = minimum(data[start + 1], start);

19. Consider the following method:

public static void processString(String str) {


if (str.length() > 0) {
System.out.print(str.substring(0, 1));
processString(str.substring(1));
}
}

What is the output of the following call?

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.

/** Returns an index in arr where the value x appears if x appears


* in arr between arr[left] and arr[right], inclusive;
* otherwise returns -1.
* Precondition: arr is sorted in ascending order.
* left >= 0, right < arr.length, arr.length > 0
*/
public static int binarySearch(int[] arr, int left, int right, int x) {
if (right >= left) {
int mid = (left + right) / 2;
if (arr[mid] == x) {
return mid;
} else if (arr[mid] > x) {
return binarySearch(arr, left, mid - 1, x);
} else {
return binarySearch(arr, mid + 1, right, x);
}
}
return -1;
}

The following code segment appears in a method in the same class:

int[] values = {2, 4, 6, 8, 10, 12, 14};


int result = binarySearch(values, 0, values.length - 1, 10);
System.out.println(result);

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

(A) 2
(B) 3
(C) 4
(D) 5
(E) -1

You might also like