0% found this document useful (0 votes)
10 views9 pages

SG Unit6ProgressCheckMCQ 619f17e634aa49.619f17e71419a5.33413876

The document is a scoring guide for an AP Computer Science A Unit 6 Progress Check, containing multiple-choice questions related to Java programming concepts. It covers topics such as array manipulation, method functionality, and error handling. Each question provides options for answers, focusing on understanding code behavior and debugging.

Uploaded by

ruotiangroup
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)
10 views9 pages

SG Unit6ProgressCheckMCQ 619f17e634aa49.619f17e71419a5.33413876

The document is a scoring guide for an AP Computer Science A Unit 6 Progress Check, containing multiple-choice questions related to Java programming concepts. It covers topics such as array manipulation, method functionality, and error handling. Each question provides options for answers, focusing on understanding code behavior and debugging.

Uploaded by

ruotiangroup
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/ 9

AP COMPUTER SCIENCE A Scoring Guide

Unit 6 Progress Check: MCQ

1. Consider the following code segment.

int[] arr = {1, 2, 3, 4, 5};

Which of the following code segments would correctly set the first two elements of array arr to 10 so that the
new value of array arr will be {10, 10, 3, 4, 5} ?
arr[0] = 10;
(A) arr[1] = 10;

arr[1] = 10;
(B) arr[2] = 10;

(C) arr[0, 1] = 10;


(D) arr[1, 2] = 10;
(E) arr = 10, 10, 3, 4, 5;

2. Consider the following method.

public int[] transform(int[] a)


{
a[0]++;
a[2]++;
return a;
}

The following code segment appears in a method in the same class as transform.

/* missing code */
arr = transform(arr);

After executing the code segment, the array arr should contain {1, 0, 1, 0}. Which of the following can
be used to replace /* missing code */ so that the code segment works as intended?

I. int[] arr = {0, 0, 0, 0};


II. int[] arr = new int[0];
III. int[] arr = new int[4];
(A) I only
(B) II only
(C) III only
(D) I and II
(E) I and III

AP Computer Science A Page 1 of 9


Scoring Guide

Unit 6 Progress Check: MCQ

3. Consider the following method, which is intended to return the number of strings of length greater than or equal to 3
in an array of String objects.

public static int checkString(String[] arr)


{
int count = 0;
for (int k = 0; k < arr.length; k++)
{
if (arr[k].length() >= 3)
{
count++;
}
}
return count;
}

Which of the following code segments compile without error?

I. checkString(new String[]);
II. checkString(new String[0]);
III. String[] str = {"cat", "dog"};
checkString(str);
(A) II only
(B) III only
(C) I and III only
(D) II and III only
(E) I, II, and III

4. Consider the following code segment.

int[] arr = {10, 20, 30, 40, 50};


for(int x = 1; x < arr.length - 1; x++)
{
arr[x + 1] = arr[x] + arr[x + 1];
}

Which of the following represents the contents of arr after the code segment has been executed?
(A) {10, 20, 30, 70, 120}
(B) {10, 20, 50, 90, 50}

(C) {10, 20, 50, 90, 140}

(D) {10, 30, 60, 100, 50}


(E) {10, 30, 60, 100, 150}

Page 2 of 9 AP Computer Science A


Scoring Guide

Unit 6 Progress Check: MCQ

5. Consider the following code segment.

int[] arr = {4, 3, 2, 1, 0};


int total = 0;
for (int k = 0; k <= total; k++)
{
if (arr[k] % 2 == 0)
{
total += arr[k];
}
else
{
total -= arr[k];
}
}
System.out.print(total);

What, if anything, is printed as a result of executing the code segment?


(A) 2

(B) 1

(C) 0
(D) -4
(E) Nothing is printed because the code segment causes a runtime error.

AP Computer Science A Page 3 of 9


Scoring Guide

Unit 6 Progress Check: MCQ

6. The array fruits is declared below.

String [] fruits = {"apples", "bananas", "cherries", "dates"};

Which of the following code segments will cause an ArrayIndexOutOfBoundsException ?

I.
for (int i = 0; i <= fruits.length; i++)
{
System.out.println(fruits[i]);
}

II.
for (int i = 0; i <= fruits.length - 1; i++)
{
System.out.println(fruits[i]);
}

III.
for (int i = 1; i <= fruits.length; i++)
{
System.out.println(fruits[i - 1]);
}
(A) I only
(B) II only
(C) I and III only
(D) II and III only
(E) I, II, and III

7. The Fibonacci numbers are a sequence of integers. The first two numbers are 1 and 1. Each subsequent number is
equal to the sum of the previous two integers. For example, the first seven Fibonacci numbers are 1, 1, 2, 3, 5, 8,
and 13.

The following code segment is intended to fill the fibs array with the first ten Fibonacci numbers. The code
segment does not work as intended.

int[] fibs = new int[10];


fibs[0] = 1;
fibs[1] = 1;
for (int j = 1; j < fibs.length; j++)
{
fibs[j] = fibs[j - 2] + fibs[j - 1];
}

Which of the following best identifies why the code segment does not work as intended?

Page 4 of 9 AP Computer Science A


Scoring Guide

Unit 6 Progress Check: MCQ

(A) In the for loop header, the initial value of j should be 0.

(B) In the for loop header, the initial value of j should be 2.

(C) The for loop condition should be j < fibs.length - 1.


(D) The for loop condition should be j < fibs.length + 1.
(E) The for loop should increment j by 2 instead of by 1.

8. Consider the following code segment.

int[] numbers = {1, 2, 3, 4, 5, 6};


for (int i = 0; i < numbers.length; i++)
{
System.out.println(numbers[i]);
}

Which of the following for loops produces the same output as the code segment?
for (int x : numbers)
{
(A) System.out.println(numbers[x]);
}
for (int x : numbers)
{
(B) System.out.println(numbers);
}
for (int x : numbers)
{
(C) System.out.println(x);
}
for (numbers : int x)
{
(D) System.out.println(numbers[x]);
}
for (numbers : int x)
{
(E) System.out.println(x);
}

AP Computer Science A Page 5 of 9


Scoring Guide

Unit 6 Progress Check: MCQ

9. Consider the following class definition.

public class Toy


{
private int yearFirstSold;
public int getYearFirstSold()
{
return yearFirstSold;
}
/* There may be instance variables, constructors, and other methods
not shown. */
}

The following code segment, which appears in a class other than Toy, prints the year each Toy object in
toyArray was first sold by its manufacturer. Assume that toyArray is a properly declared and initialized
array of Toy objects.

for (Toy k : toyArray)


{
System.out.println(k.getYearFirstSold());
}

Which of the following could be used in place of the given code segment to produce the same output?

I.
for (int k = 0; k < toyArray.length; k++)
{
System.out.println(getYearFirstSold(k));
}

II.
for (int k = 0; k < toyArray.length; k++)
{
System.out.println(k.getYearFirstSold());
}

III.
for (int k = 0; k < toyArray.length; k++)
{
System.out.println(toyArray[k].getYearFirstSold());
}
(A) I only
(B) II only
(C) III only
(D) I and II
(E) II and III

Page 6 of 9 AP Computer Science A


Scoring Guide

Unit 6 Progress Check: MCQ

10. Consider the following two code segments.

I.
int[] arr = {1, 2, 3, 4, 5};
for (int x = 0; x < arr.length; x++)
{
System.out.print(arr[x + 3]);
}

II.
int[] arr = {1, 2, 3, 4, 5};
for (int x : arr)
{
System.out.print(x + 3);
}

Which of the following best describes the behavior of code segment I and code segment II ?
(A) Both code segment I and code segment II will print 45.
(B) Both code segment I and code segment II will print 45678.
Code segment I will cause an ArrayIndexOutOfBoundsException and code segment II will
(C)
print 45.
Code segment I will cause an ArrayIndexOutOfBoundsException and code segment II will
(D)
print 45678.
(E) Both code segment I and code segment II will cause an ArrayIndexOutOfBoundsException.

11. The code segment below is intended to set the boolean variable duplicates to true if the int array
arr contains any pair of duplicate elements. Assume that arr has been properly declared and initialized.

boolean duplicates = false;


for (int x = 0; x < arr.length - 1; x++)
{
/* missing loop header */
{
if (arr[x] == arr[y])
{
duplicates = true;
}
}
}

Which of the following can replace /* missing loop header */ so that the code segment works as intended?

AP Computer Science A Page 7 of 9


Scoring Guide

Unit 6 Progress Check: MCQ

(A) for (int y = 0; y <= arr.length; y++)


(B) for (int y = 0; y < arr.length; y++)
(C) for (int y = x; y < arr.length; y++)
(D) for (int y = x + 1; y < arr.length; y++)
(E) for (int y = x + 1; y <= arr.length; y++)

12. In the code segment below, assume that the int array numArr has been properly declared and initialized. The
code segment is intended to reverse the order of the elements in numArr. For example, if numArr initially
contains {1, 3, 5, 7, 9}, it should contain {9, 7, 5, 3, 1} after the code segment executes.

/* missing loop header */


{
int temp = numArr[k];
numArr[k] = numArr[numArr.length - k - 1];
numArr[numArr.length - k - 1] = temp;
}

Which of the following can be used to replace /* missing loop header */ so that the code segment works as
intended?
(A) for (int k = 0; k < numArr.length / 2; k++)

(B) for (int k = 0; k < numArr.length; k++)


(C) for (int k = 0; k < numArr.length / 2; k--)
(D) for (int k = numArr.length - 1; k >= 0; k--)
(E) for (int k = numArr.length - 1; k >= 0; k++)

13. Consider the following code segment, which is intended to print the maximum value in an integer array values.
Assume that the array has been initialized properly and that it contains at least one element.

int maximum = /* missing initial value */;


for (int k = 1; k < values.length; k++)
{
if (values[k] > maximum)
{
maximum = values[k];
}
}
System.out.println(maximum);

Which of the following should replace /* missing initial value */ so that the code segment will work as
intended?

Page 8 of 9 AP Computer Science A


Scoring Guide

Unit 6 Progress Check: MCQ

(A) 0
(B) values[0]
(C) values[1]
(D) Integer.MIN_VALUE
(E) Integer.MAX_VALUE

14. Consider the following method, which is intended to return the index of the first negative integer in a given array of
integers.

public int positionOfFirstNegative(int[] values)


{
int index = 0;
while (values[index] >= 0)
{
index++;
}
return index;
}

What precondition is needed on the values array so that the method will work as intended?

(A) The array values must contain at least one negative integer.

(B) The array values must contain at least one nonnegative integer.
(C) The array values must contain at least one positive integer.
(D) No precondition is needed. The method will never work as intended.
(E) No precondition is needed. The method will always work as intended.

AP Computer Science A Page 9 of 9

You might also like