Array Creation and Access
Help Questions
AP Computer Science A › Array Creation and Access
A teacher stores quiz scores in an array of integers. Given the code snippet,
public class Grades {
public static void main(String[] args) {
int[] grades = new int<u>5</u>; // 5 students
grades<u>0</u> = 90;
grades<u>1</u> = 85;
}
}
The array stores whole-number scores. How is the array grades initialized in the code?
It is initialized with values {90, 85, 0, 0, 0}.
It is created with length 4 because indices go 0-4.
It is initialized as an ArrayList of 5 integers.
It is created with length 5 using new int[5].
Explanation
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically using correct syntax and indexing. In Java, arrays are fixed-size data structures created using the 'new' keyword followed by the type and size in square brackets. The provided code snippet demonstrates creating an array 'grades' using new int[5], which allocates space for 5 integer elements. Choice A is correct because it accurately describes that the array is created with length 5 using the syntax new int[5], which creates an array with indices 0 through 4. Choice C is incorrect because uninitialized array elements default to 0 for integers, so the array contains {90, 85, 0, 0, 0}, but this describes the state after assignment, not the initialization itself. To help students: Emphasize the difference between array creation/initialization and subsequent element assignment, practice using the 'new' keyword syntax, and understand default values for primitive types.
A store tracks stock counts by product index. Given the code snippet,
public class Inventory {
public static void main(String[] args) {
int[] stock = {12, 5, 0, 18}; // index = product ID (0-3)
int productId = 1;
int current = stock<u>productId</u>; // retrieve stock for product 1
System.out.println(current);
}
}
Array indexing starts at 0. What is the output of accessing element at index 1 in the array?
0
5
12
18
Explanation
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically using correct syntax and indexing. In Java, arrays are fixed-size data structures used to store elements of a specific data type, accessed using zero-based indexing. The provided code snippet demonstrates creating an array 'stock' of integers and accessing elements using their index positions. Choice B is correct because it accurately reflects that stock[1] accesses the second element (index 1), which contains the value 5, adhering to zero-based indexing. Choice A is incorrect because it represents the value at index 0, not index 1, demonstrating confusion about which index is being accessed. To help students: Emphasize practicing array creation and access through coding exercises, highlight the importance of zero-based indexing, and encourage tracing through code step-by-step to understand which element is being accessed.
A store tracks stock counts by product index. Given the code snippet,
public class InventoryUpdate {
public static void main(String[] args) {
int[] stock = {7, 3, 9}; // products 0-2
stock<u>2</u> = stock<u>2</u> - 1; // sold one unit of product 2
System.out.println(stock<u>2</u>);
}
}
Indexing starts at 0. What is the output of accessing element at index 2 in the array?
3
7
8
9
Explanation
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically modifying array elements and using expressions with array access. In Java, arrays are mutable data structures where individual elements can be modified using assignment statements with array indexing. The provided code snippet demonstrates creating an array 'stock' and modifying the element at index 2 by subtracting 1 from its current value (9). Choice B is correct because stock[2] initially contains 9, and after the operation stock[2] = stock[2] - 1, it becomes 8, which is then printed. Choice A is incorrect because it shows the original value before modification, indicating a misunderstanding of when the modification occurs in the code execution sequence. To help students: Emphasize tracing through code line by line, practice modifying array elements using compound expressions, and understand that array modifications happen immediately when the assignment statement executes.
A library stores book ID numbers in an array. Given the code snippet,
public class Library {
public static void main(String[] args) {
int[] bookIds = {1012, 1044, 1100}; // each element is a book ID
int firstId = bookIds<u>0</u>;
}
}
Array indices start at 0. What data type does the array bookIds store?
double
char
int
String
Explanation
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically identifying array data types from declaration and initialization. In Java, arrays are declared with a specific data type that all elements must match, indicated by the type before the square brackets. The provided code snippet demonstrates creating an array 'bookIds' declared as int[], meaning it stores integer values. Choice B is correct because the array is explicitly declared as int[] bookIds, indicating it stores primitive int values for the book ID numbers. Choice A is incorrect because String would require the declaration String[] bookIds and would store text values, not the numeric IDs shown in the initialization. To help students: Emphasize examining the array declaration syntax carefully, practice identifying data types from both declaration and initialization values, and understand that all elements in an array must be of the same declared type.
A weather app stores daily high temperatures in an array. Given the code snippet,
public class Weather {
public static void main(String[] args) {
double[] highs = {71.5, 73.0, 69.8, 75.2}; // degrees
// day 1 is index 0
}
}
The array stores decimal temperatures. Which line of code correctly accesses the third element of the array?
double t = highs(2);
double t = highs[2];
double t = highs[1];
double t = highs[3];
Explanation
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically using correct syntax and indexing. In Java, arrays are fixed-size data structures used to store elements of a specific data type, accessed using zero-based indexing with square brackets. The provided code snippet demonstrates creating an array 'highs' of doubles and the need to access the third element (which is at index 2 due to zero-based indexing). Choice B is correct because it uses the proper syntax highs[2] to access the third element, as arrays start counting from index 0. Choice C is incorrect because it uses parentheses instead of square brackets, which is invalid syntax for array access in Java - parentheses are used for method calls, not array indexing. To help students: Emphasize the difference between array access syntax (square brackets) and method call syntax (parentheses), practice counting elements starting from zero, and use visual diagrams showing array indices.
A library stores book titles in an array. Given the code snippet,
public class Catalog {
public static void main(String[] args) {
String[] titles = {"Dune", "1984", "Hamlet"}; // index = shelf slot
String pick = titles<u>1</u>;
System.out.println(pick);
}
}
Array indexing starts at 0. What is the output of accessing element at index 1 in the array?
1984
Dune
Hamlet
null
Explanation
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically accessing String array elements using zero-based indexing. In Java, arrays can store any data type including Strings, and are accessed using the same zero-based indexing regardless of the data type stored. The provided code snippet demonstrates creating a String array 'titles' with book titles and accessing the element at index 1. Choice B is correct because titles[1] accesses the second element in the array, which contains the String "1984", following zero-based indexing where the first element is at index 0. Choice A is incorrect because "Dune" is at index 0, not index 1, showing a common off-by-one error in understanding array positions. To help students: Emphasize that zero-based indexing applies to all array types including String arrays, practice accessing different positions in arrays, and use debugging techniques to verify which element is at each index.
A teacher reports a specific student's grade using an array. Given the code snippet,
public class GradeReport {
public static void main(String[] args) {
int[] grades = {88, 92, 76, 95}; // 4 students
int i = 3;
System.out.println(grades<u>i</u>);
}
}
Indices start at 0. What is the output of accessing element at index 3 in the array?
76
88
92
95
Explanation
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically using variables as array indices. In Java, arrays can be accessed using integer variables as indices, not just literal numbers, following the same zero-based indexing rules. The provided code snippet demonstrates creating an array 'grades' with four elements and accessing an element using the variable i, which holds the value 3. Choice C is correct because grades[i] where i=3 accesses the fourth element (index 3), which contains the value 95, demonstrating proper use of a variable as an array index. Choice D is incorrect because 88 is at index 0, not index 3, showing confusion about which position the index variable references. To help students: Emphasize that variables can be used as array indices, practice tracing variable values when used as indices, and reinforce counting positions starting from zero.
A weather station stores rainfall amounts in an array. Given the code snippet,
public class Rain {
public static void main(String[] args) {
double[] rain = new double<u>3</u>; // 3 days
rain<u>0</u> = 0.2;
rain<u>1</u> = 1.0;
rain<u>2</u> = 0.0;
}
}
The array stores decimal values. What data type does the array rain store?
boolean
int
double
String
Explanation
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically identifying array data types from declaration syntax. In Java, arrays are declared with a specific data type that determines what kind of values can be stored, shown before the square brackets in the declaration. The provided code snippet demonstrates creating an array 'rain' declared as double[], and initializing it with decimal values representing rainfall amounts. Choice A is correct because the array is explicitly declared as double[] rain, indicating it stores double (decimal) values for rainfall measurements. Choice B is incorrect because int would only store whole numbers and would be declared as int[], not allowing the decimal values like 0.2 shown in the code. To help students: Emphasize matching data types to the kind of data being stored (decimals need double or float), practice reading array declarations carefully, and understand that the declared type must match the values being assigned.
A game tracks top scores using an array. Given the code snippet,
public class GameScores {
public static void main(String[] args) {
int[] scores = {400, 250, 125}; // top 3 scores
System.out.println(scores.length);
}
}
The array stores whole-number scores. How would you modify the array to add one more element?
scores = {400, 250, 125, 500};
scores.length = 4;
scores = new int[]{400, 250, 125, 500};
scores.add(500);
Explanation
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically that arrays have fixed size and cannot be dynamically resized. In Java, arrays are fixed-size data structures - once created, their length cannot be changed, requiring creation of a new array to add elements. The provided code snippet demonstrates an array 'scores' with 3 elements, and the question asks how to add a fourth element. Choice C is correct because it creates a new array with 4 elements using the array initializer syntax, which is the only way to 'add' an element to an array in Java. Choice A is incorrect because add() is an ArrayList method, not available for arrays, showing confusion between arrays and ArrayLists. To help students: Emphasize the fixed-size nature of arrays versus dynamic collections like ArrayList, practice creating new arrays when size changes are needed, and understand the syntax differences between array initialization and ArrayList operations.
Inventory Management: Given the code snippet, what is the output of accessing element at index 3 in the array?
public class RestockCheck {
public static void main(String[] args) {
// Stock counts for four products
int[] stock = {7, 3, 10, 2}; // int array: item counts
System.out.println(stock<u>3</u>); // access the fourth product
}
}
2
10
3
ArrayIndexOutOfBoundsException
Explanation
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically accessing the last element in an array using correct indexing. In Java, arrays are fixed-size data structures with zero-based indexing, where valid indices range from 0 to length-1. The provided code snippet demonstrates creating an array 'stock' with four elements {7, 3, 10, 2} and accessing the element at index 3, which is the fourth and last element. Choice B (2) is correct because stock[3] accesses the element at index 3, which contains the value 2. Choice D (ArrayIndexOutOfBoundsException) is incorrect because index 3 is valid for an array of length 4 (indices 0, 1, 2, 3). To help students: Practice identifying valid index ranges for arrays of different sizes, emphasize that the last valid index is always length-1, and use debugging exercises to recognize when index errors would occur.