AP Computer Science A • Unit 4.3

Array Creation and Access

Detailed answer explanations for the Unit 4.3 multiple-choice practice questions.

Q1B
Q2B
Q3D
Q4A
Q5D
Q6C
Q7C
Q8D
Q9B
Q10C
Q11C
Q12A
Q13B
Q14B
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

Which expression creates an int array with five elements?

Aint[5] new
Bnew int[5]
Cnew int(5)
Darray int 5
Correct answer: B. new int[5] is the creation form the section uses: the keyword new, the element type, and the length inside square brackets.
Explanation

Declaring a variable such as int[] scores; only makes a variable that can hold an array reference. The array object itself comes from new, and the number in the square brackets fixes how many elements that object has.

  • A is not correct: The pieces are in the wrong order; new begins the creation expression rather than trailing after the type and length.
  • C is not correct: Parentheses after a type are the constructor-call form seen in new Student("Maya"); an array's length must sit inside square brackets instead.
  • D is not correct: This is not a creation expression at all. Nothing here is the keyword new, and there is no int[] element type with a bracketed length.
Q2

What is the first valid index of a Java array?

A-1
B0
C1
Dlength
Correct answer: B. Java starts array indexes at 0, just as it does for the characters in a String.
Explanation

The elements sit in numbered positions called indexes, and that numbering begins at zero. This is why an array of length n has valid indexes 0 through n - 1 rather than 1 through n.

  • A is not correct: An index below 0 is out of range, and using it causes an ArrayIndexOutOfBoundsException at run time.
  • C is not correct: Counting from one would skip the very first element; index 1 already names the second stored value.
  • D is not correct: length is the field holding the element count, not a position, and an index equal to the array's length is out of bounds.
Q3

If int[] nums = new int[4];, what is the last valid index?

A4
B5
Cnums.length + 1
D3
Correct answer: D. The array has four elements, so the valid indexes are 0 through 3 and the last one is 3.
Explanation

For any array of length n, the last valid index is n - 1, which can also be written nums.length - 1. The count of elements and the number of the final position are never the same value.

  • A is not correct: 4 is the length itself, and accessing arr[n] when the array has length n is one position past the end.
  • B is not correct: 5 is larger than the length, so it is even further outside the range 0 through 3.
  • C is not correct: Adding one to the length moves further past the end; the last index is found by subtracting one, not adding.
Q4

Which expression gives the number of elements in array nums?

Anums.length
Bnums.size()
Clength(nums)
Dnums.length()
Correct answer: A. An array's element count is read from the public field length, written with no parentheses after the field name.
Explanation

The length is fixed when the array is created, and the section stores that count in a field rather than behind a method call. The example System.out.println(scores.length); prints 4 for a four-element array.

  • B is not correct: The section names no size method for arrays; the element count is reached through the field length.
  • C is not correct: This treats length as a stand-alone call taking the array; the field is reached with a dot after the array name.
  • D is not correct: The parentheses make this the String form. A String uses the method length(), while an array uses the field length.
Q5

Which statement stores 10 in the first element of nums?

Anums[1] = 10;
Bnums.length = 10;
Cnums.add(10);
Dnums[0] = 10;
Correct answer: D. The first element sits at index 0, so nums[0] = 10; replaces that element with 10.
Explanation

Square brackets select one element by index, and an assignment to that expression replaces just that element while the others stay unchanged. Because indexes start at 0, the first position is 0, not 1.

  • A is not correct: Index 1 is the second position, so this would leave the first element holding whatever it already held.
  • B is not correct: length is read to find the element count, and the array's length cannot change after creation, so it is not an assignment target.
  • C is not correct: The section names no add method for arrays; an element is replaced through array[index] with square brackets.
Q6

What happens if code accesses nums[nums.length]?

AIt creates a new element.
BIt returns null for every array.
CIt causes an index-out-of-bounds error at run time.
DIt accesses the last element.
Correct answer: C. An index at least equal to the array's length is out of range, so this causes an ArrayIndexOutOfBoundsException at run time.
Explanation

If an array has length n, the valid indexes stop at n - 1, so arr[n] is one position past the end. The section warns directly against confusing an array's length with its last valid index.

  • A is not correct: An array's length is fixed when the array is created, so an out-of-range index cannot add a new position to it.
  • B is not correct: null is the default value stored in elements of a reference-type array, not the result of reading past the end of any array.
  • D is not correct: The last element is at index array.length - 1; the index equal to the length is the one just beyond it.
Q7

Which array initializer creates an array containing 2, 4, and 6?

Aint vals = [2, 4, 6];
Bvals.add(2, 4, 6);
Cint[] vals = {2, 4, 6};
Dint[] vals = new int;
Correct answer: C. An initializer list uses braces after an array-typed variable, and the three listed values become the array's elements and its length.
Explanation

When the starting values are already known, an initializer list creates and fills the array in one statement. The number of listed values becomes the array's length, so this makes an int[] of length 3.

  • A is not correct: The declared type is int rather than int[], and the values sit in square brackets instead of the braces an initializer list uses.
  • B is not correct: The section names no add method; an array is filled by an initializer list or by assigning to indexed positions.
  • D is not correct: new int gives no length in square brackets, so it does not create an array, and no values are supplied.
Q8

What default value do elements of a newly created int array receive?

Atrue
B""
C1
D0
Correct answer: D. When new creates the array, every element is filled with the element type's default value, which is 0 for int.
Explanation

The defaults follow the element type: 0 for int, 0.0 for double, false for boolean, and null for a reference type. That is why printing scores[0] right after new int[3] shows 0.

  • A is not correct: true is not a default for any element type here; boolean elements start as false, and these elements are int.
  • B is not correct: An empty String is not among the listed defaults, and a String[] element would start as null rather than a String object.
  • C is not correct: The default is the type's zero value, not a starting count of one; new does not seed elements with 1.
Q9

Consider the following code segment. What is the result of compiling and running this code segment?

int[] checkpoints;
System.out.println(checkpoints.length);
AIt prints 0.
BThe compiler reports that checkpoints has not been initialized.
CIt prints null.
DIt causes an out-of-bounds error at run time.
Correct answer: B. checkpoints is a local array variable that never receives a reference, and such a variable cannot be used before it does.
Explanation

The declaration creates only a variable capable of holding an array reference; no new and no initializer list ever gives it one. An array instance variable would begin as null, but a local variable in this position is rejected before the code runs.

  • A is not correct: No array object exists here, so there is no length field to read and no element count of 0 to print.
  • C is not correct: Beginning as null describes an array instance variable; this local variable has not received a reference at all.
  • D is not correct: No index is used in this segment, and the problem is reported at compile time rather than during the run.
Q10

Consider the following code segment. What happens when this code segment is compiled?

int[] levels = new int[3];
levels[0] = 4;
levels[1] = 2.5;
AThe code compiles and stores 2 at index 1.
BThe code compiles and changes levels to a double[] array.
CThe compiler rejects 2.5 because an int[] stores int values.
DThe compiler rejects the first assignment because index 0 is invalid.
Correct answer: C. An int[] stores int values, and 2.5 is not one, so the assignment is rejected.
Explanation

The element type chosen at creation controls what may be stored in every position. new int[3] makes an array whose elements each hold an int, in the same way an int[] holds int values and a double[] holds double values.

  • A is not correct: The section gives no rule that turns 2.5 into a stored int; the value simply does not match the element type.
  • B is not correct: An array's element type is fixed when the array is created, so new int[3] cannot become a double[] later.
  • D is not correct: Index 0 is the first valid index of a length-3 array, so levels[0] = 4; is a normal element assignment.
Q11

Consider the following code segment. What is printed?

int[] urgencies = {4, 2, 5, 3, 1};
int i = 1;
urgencies[i + 2] = urgencies[i] * 2;
System.out.println(urgencies[3]);
A2
B3
C4
D6
Correct answer: C. With i equal to 1, the statement stores urgencies[1] * 2, which is 4, into urgencies[3], and that is the element printed.
Explanation

An index may be an int expression, so i + 2 evaluates to 3 and selects the fourth element. The right side is read first from index 1, and the assignment then replaces only the element at index 3.

  • A is not correct: 2 is the value read from index 1 before it is doubled, not the value stored at index 3.
  • B is not correct: 3 is what index 3 held in the initializer list, but the assignment replaced that element.
  • D is not correct: 6 would require the element at index 1 to be 3, but the initializer list puts 2 there.
Q12

Consider the following code segment. Assume Book has a constructor that receives one String. What is printed?

Book[] shelf = new Book[4];
shelf[2] = new Book("Orbit");

System.out.println(shelf[0]);
Anull
B0
Can empty String
DNothing is printed; index 0 is out of bounds.
Correct answer: A. Creating a Book[] makes places for references only, so every element starts as null and index 0 was never assigned.
Explanation

Creating an array whose element type is a class does not call that class's constructor. Only the separate new expression for index 2 creates an object, so the other elements still hold the reference default.

  • B is not correct: 0 is the default for int elements; for a reference element type the default value is null.
  • C is not correct: Creating the array does not create any objects, so no empty String or any other object is placed in element 0.
  • D is not correct: The array has four elements, so the valid indexes are 0 through 3 and index 0 is in bounds.
Q13

Consider the following code segment. What is printed?

boolean[] available = new boolean[3];
System.out.println(available[1]);
Atrue
Bfalse
Cnull
DThe code does not compile.
Correct answer: B. new fills every element of a boolean[] with the element type's default value, which is false.
Explanation

No value is ever assigned to any position here, so each element still holds what creation put there. The default depends only on the element type: false for boolean, just as it is 0 for int.

  • A is not correct: true is not the default for boolean elements, and nothing in the segment assigns a value to index 1.
  • C is not correct: null is the default for a reference element type, while boolean elements start as false.
  • D is not correct: The segment uses ordinary array creation with new and a valid index for a length-3 array, so there is nothing to reject.
Q14

Consider the following code segment. What is printed?

int[] visits = {120, 98, 105, 131, 110};
System.out.println(visits[visits.length - 1]);
A131
B110
C5
DNothing is printed; an out-of-bounds error occurs.
Correct answer: B. The array has five elements, so visits.length - 1 is 4 and that last element holds 110.
Explanation

The number of values in the initializer list becomes the array's length, and the last valid index is always one less than that length. Writing visits[visits.length - 1] reaches the final element without naming a fixed number.

  • A is not correct: 131 sits at index 3, one position before the last valid index of this five-element array.
  • C is not correct: 5 is the array's length itself; the expression subtracts one from it and then reads the element there.
  • D is not correct: Subtracting one keeps the index inside the range 0 through 4, so no out-of-bounds error occurs.