Array Creation and Access
Help Questions
AP Computer Science A › Array Creation and Access
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?
double
int
boolean
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 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[3];
double t = highs[1];
double t = highs(2);
double t = highs[2];
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.
Game Development: Given the code snippet, how would you modify the array scores to add one more element?
public class AddPlayerScore {
public static void main(String[] args) {
// Scores for three players
int[] scores = {300, 450, 500}; // int array: player scores
System.out.println(scores<u>2</u>); // access last score
}
}
Assign scores[3] = 600 without changing the array size.
Increase scores.length to 4 to make room for a new score.
Create a new int[] with one extra slot and copy values.
Use scores.add(600) to append a new score.
Explanation
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically the immutable size property of arrays and how to work around it. In Java, arrays have a fixed size that cannot be changed after creation, unlike dynamic data structures like ArrayList. The provided code snippet shows an array 'scores' with three elements, and asks how to add a fourth element. Choice A is correct because the only way to 'add' to an array is to create a new, larger array and copy the existing values, since arrays cannot be resized. Choice B (scores.add()) is incorrect because arrays don't have an add method - that's for ArrayList. To help students: Emphasize the fundamental difference between arrays (fixed size) and ArrayList (dynamic size), practice array copying techniques, and explain when to choose arrays versus more flexible data structures based on whether the size will change.
A weather station stores daily high temperatures. Given the code snippet,
public class Weather {
public static void main(String[] args) {
// double stores decimal temperature readings
double[] highs = {72.5, 68.0, 70.25, 75.0};
// Index 0 is the first day
double secondDay = highs<u>1</u>;
System.out.println(secondDay);
}
}
What data type does the array highs store?
String
boolean
int
double
Explanation
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically identifying array data types. In Java, arrays are fixed-size data structures that must be declared with a specific data type, which determines what kind of values can be stored. The provided code snippet demonstrates creating an array 'highs' that stores temperature readings with decimal values. Choice B is correct because the array is declared as double[], which stores floating-point numbers with decimal precision, as evidenced by values like 72.5 and 70.25. Choice A (int) is incorrect because integers cannot store decimal values, and the array clearly contains decimal numbers. To help students: Emphasize matching data types to the kind of data being stored, practice identifying array types from declarations, and understand that double is used for decimal values while int is for whole numbers only.
Library System: Given the code snippet, what data type does the array titles store?
public class BookTitles {
public static void main(String[] args) {
// Titles in a small reading list
String[] titles = {"Dune", "1984", "Hamlet"}; // String array: book titles
System.out.println(titles<u>1</u>); // retrieve a title
}
}
int
String
boolean
double
Explanation
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically identifying the data type of array elements from the declaration. In Java, the data type of an array is specified in its declaration before the square brackets, determining what type of values can be stored. The provided code snippet shows the declaration 'String[] titles' which creates an array that stores String objects representing book titles. Choice B (String) is correct because the array is explicitly declared as String[], as evidenced by both the declaration and the string literal values in quotes. Choice A (int) is incorrect as integer arrays cannot store text values like "Dune" or "1984" even though "1984" contains digits. To help students: Practice reading array declarations to identify element types, emphasize that the type before [] determines what the array stores, and show examples of compile errors when trying to store incompatible types.
Weather Data: Given the code snippet, what data type does the array temps store?
public class WeatherTracker {
public static void main(String[] args) {
// Daily temperatures (in degrees) for a week
int[] temps = {72, 68, 75, 70, 69}; // int array: whole-number temperatures
int today = temps<u>0</u>; // first day
System.out.println(today);
}
}
int
double
char
String
Explanation
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically identifying the data type of array elements. In Java, arrays are fixed-size data structures that store elements of a single, specific data type declared when the array is created. The provided code snippet demonstrates creating an array 'temps' declared as int[], meaning it stores integer values representing temperatures. Choice B (int) is correct because the array is explicitly declared as 'int[] temps', indicating it stores integer values. Choice C (double) is incorrect as it would require 'double[] temps' declaration, and the values shown (72, 68, 75, 70, 69) are whole numbers without decimal points. To help students: Emphasize the importance of reading array declarations carefully, practice identifying data types from array declarations, and explain that the type before the brackets determines what kind of values the array can hold.
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 by product index. Given the code snippet,
public class Restock {
public static void main(String[] args) {
// Index = product; value = stock count
int[] stock = {4, 7, 2};
// Need one more product slot in the array
}
}
How would you modify the array to add one more element?
Change to: int[] stock = {4, 7, 2, 9};
Use: stock.add(9);
Use: stock[3] = 9; without resizing
Change to: int[] stock = new int[4];
Explanation
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically the fixed-size nature of arrays and how to modify array contents. In Java, arrays have a fixed size once created and cannot be dynamically resized; to have a different number of elements, you must create a new array. The provided code snippet shows an array 'stock' initialized with 3 elements, and the task is to accommodate 4 elements. Choice D is correct because it creates a new array with 4 elements by using array initializer syntax with all 4 values specified. Choice C is incorrect because stock[3] would cause an ArrayIndexOutOfBoundsException since the array only has indices 0, 1, and 2. To help students: Emphasize that arrays cannot be resized after creation, practice creating arrays with different sizes, and understand the difference between modifying existing elements and needing more array slots.
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 created with length 4 because indices go 0-4.
It is created with length 5 using new int[5].
It is initialized as an ArrayList of 5 integers.
It is initialized with values {90, 85, 0, 0, 0}.
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 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.