- A library stores a small catalog of book IDs by index. Given the code snippet,
public class Library {
public static void main(String[] args) {
// Each index is a shelf position; values are book IDs
int[] bookIds = {101, 205, 309, 412};
// Retrieve the third element (index 2)
int id = bookIds[2];
System.out.println(id);
}
}Which line of code correctly accesses the third element of the array? Answer: int id = bookIds[2];
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically using correct syntax for element access. In Java, arrays use square brackets [] with an index to access elements, following zero-based indexing where the first element is at index 0. The provided code snippet demonstrates accessing the third element of the bookIds array, which requires using index 2. Choice C is correct because bookIds[2] uses the proper syntax with square brackets and the correct index 2 to access the third element (value 309). Choice A is incorrect because it uses index 3, which would access the fourth element, demonstrating an off-by-one error in understanding zero-based indexing. To help students: Emphasize that array access always uses square brackets, practice converting between element position (1st, 2nd, 3rd) and index values (0, 1, 2), and avoid confusion with method calls that use parentheses.
- 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[2]); // access last score
}
} Answer: Create a new int[] with one extra slot and copy values.
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 teacher stores 5 student grades in an array. Given the code snippet,
public class Report {
public static void main(String[] args) {
// Each index is a student; values are grades
int[] grades = {88, 92, 75, 90, 84};
int last = grades[grades.length - 1];
System.out.println(last);
}
}What is the output of accessing element at index grades.length - 1 in the array? Answer: 84
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically using the length property to access the last element. In Java, arrays have a length property that returns the number of elements, and since indexing is zero-based, the last element is at index length-1. The provided code snippet demonstrates accessing the last element of the 'grades' array using grades[grades.length - 1]. Choice B is correct because grades.length is 5, so grades.length - 1 equals 4, which accesses the element at index 4 containing the value 84. Choice C (5) is incorrect because it confuses the array length with the value stored in the last element. To help students: Emphasize the relationship between array length and valid indices (0 to length-1), practice using the length property for dynamic array access, and understand why length-1 gives the last valid index.
- A library stores book titles in an array. Given the code snippet,
public class Titles {
public static void main(String[] args) {
// String stores text titles
String[] titles = {"Dune", "1984", "Hamlet"};
String first = titles[0];
System.out.println(first);
}
}What data type does the array titles store? Answer: String
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically identifying the data type of arrays that store text. In Java, arrays must be declared with a specific data type, and String is the standard type for storing text values. The provided code snippet demonstrates creating an array 'titles' that stores book titles as text strings. Choice C is correct because the array is declared as String[], which stores text objects, as evidenced by the string literals "Dune", "1984", and "Hamlet". Choice A (char) is incorrect because char stores single characters, not complete text strings like book titles. To help students: Emphasize the difference between primitive types and reference types like String, practice identifying appropriate data types for different kinds of data, and understand that String is used for text while char is for single characters only.
- 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[0];
}
}Array indices start at 0. What data type does the array bookIds store? Answer: int
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.
- Game Development: Given the code snippet, what is the output of accessing element at index 0 in the array?
public class Leaderboard {
public static void main(String[] args) {
// Each index stores a player's score
int[] scores = {1500, 1200, 1800}; // int array: points
System.out.println(scores[0]); // top player's score
}
} Answer: 1500
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically accessing elements at specific indices. In Java, arrays are fixed-size data structures that use zero-based indexing, meaning the first element is at index 0. The provided code snippet demonstrates creating an array 'scores' with values {1500, 1200, 1800} and accessing the element at index 0 using scores[0]. Choice C (1500) is correct because scores[0] accesses the first element in the array, which contains the value 1500. Choice A (1200) would be the result of accessing scores[1], while Choice B (1800) would come from scores[2], showing confusion about which index corresponds to which position. To help students: Use visual representations showing array elements with their indices labeled underneath, practice tracing code execution line by line, and reinforce that index 0 always refers to the first element.
- A weather app stores weekly rainfall amounts. Given the code snippet,
public class Rainfall {
public static void main(String[] args) {
// double stores decimal measurements
double[] rain = new double[3];
rain[0] = 0.5;
rain[1] = 1.25;
rain[2] = 0.0;
System.out.println(rain[1]);
}
}What is the output of accessing element at index 1 in the array? Answer: 1.25
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically creating arrays with a specified size and assigning values to specific indices. In Java, arrays can be created with a specific size using the new keyword, then individual elements can be assigned values using index notation. The provided code snippet demonstrates creating a double array 'rain' of size 3 and assigning decimal values to each position. Choice B is correct because rain[1] accesses the element at index 1, which was assigned the value 1.25. Choice C (0.0) is incorrect because it represents the value at index 2, not index 1, showing confusion about which index is being accessed. To help students: Emphasize the two-step process of array creation and element assignment, practice tracing through code to track which values are stored at which indices, and reinforce that array indices are independent of the values stored.
- 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[i]);
}
}Indices start at 0. What is the output of accessing element at index 3 in the array? Answer: 95
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 game stores the top 3 player scores in an array. Given the code snippet,
public class GameScores {
public static void main(String[] args) {
// int stores whole-number scores
int[] scores = {300, 250, 200};
// Display the first-place score (index 0)
System.out.println(scores[0]);
}
}What is the output of accessing element at index 0 in the array? Answer: 300
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically accessing the first element using zero-based indexing. In Java, arrays are fixed-size data structures where the first element is always at index 0, regardless of the context or meaning of the data. The provided code snippet demonstrates creating an array 'scores' with game scores and accessing the first element using scores[0]. Choice A is correct because scores[0] accesses the first element in the array, which contains the value 300, representing the first-place score. Choice B (250) is incorrect because it represents the value at index 1 (second place), showing confusion about zero-based indexing. To help students: Emphasize that index 0 always refers to the first element, practice accessing elements at different positions, and use meaningful variable names to reinforce the connection between array position and data meaning.
- Student Grades: Given the code snippet, how is the array grades initialized in the code?
public class GradeReport {
public static void main(String[] args) {
// grades holds test scores for one student
double[] grades = new double[3]; // double array: decimal scores
grades[0] = 88.5;
grades[1] = 91.0;
grades[2] = 79.5;
System.out.println(grades[1]); // retrieve a score
}
} Answer: It is created with length 3 using new double[3].
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically recognizing array initialization syntax. In Java, arrays are fixed-size data structures that can be 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 double[3], which creates an array of length 3 to hold double values. Choice A is correct because the code explicitly shows 'double[] grades = new double[3];' which creates an array with three slots for double values. Choice C is incorrect because it suggests using an initializer list, but the code shows individual assignment statements after array creation. To help students: Practice distinguishing between array creation with 'new' keyword versus initializer lists, emphasize that array size is fixed once created, and use memory diagrams to visualize how arrays are allocated with a specific number of slots.
- 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[3]; // 3 days
rain[0] = 0.2;
rain[1] = 1.0;
rain[2] = 0.0;
}
}The array stores decimal values. What data type does the array rain store? Answer: double
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? Answer: scores = new int[]{400, 250, 125, 500};
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.
- A game stores player scores by index. Given the code snippet,
public class Leaderboard {
public static void main(String[] args) {
// Each index is a player; value is score
int[] scores = {10, 20, 30, 40};
// Update the second player's score
scores[1] = scores[1] + 5;
System.out.println(scores[1]);
}
}What is the output of accessing element at index 1 in the array? Answer: 25
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically modifying array elements and accessing the updated value. In Java, array elements can be modified by accessing them with their index and using assignment operators. The provided code snippet demonstrates updating the second player's score by adding 5 to the current value at index 1. Choice B is correct because scores[1] initially contains 20, then scores[1] = scores[1] + 5 updates it to 25, and the final println displays this updated value. Choice A (20) is incorrect because it represents the original value before the update, missing that the array element was modified. To help students: Emphasize that array elements can be modified after initialization, practice using compound assignment operations on array elements, and trace through code to track how values change during execution.
- 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[3]); // access the fourth product
}
} Answer: 2
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.
- 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[1];
System.out.println(secondDay);
}
}What data type does the array highs store? Answer: double
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[1]); // retrieve a title
}
} Answer: String
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, how is the array temps initialized in the code?
public class DailyTemps {
public static void main(String[] args) {
// Temperatures for five days
int[] temps = {65, 67, 70, 66, 68}; // int array: daily readings
int midweek = temps[2]; // third day
System.out.println(midweek);
}
} Answer: It is created with five values using an initializer list.
This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically recognizing array initialization using an initializer list. In Java, arrays can be created and initialized in one statement using curly braces containing comma-separated values, known as an initializer list. The provided code snippet demonstrates creating an array 'temps' using the syntax int[] temps = {65, 67, 70, 66, 68}, which creates and populates the array simultaneously. Choice A is correct because the code shows an initializer list with five integer values enclosed in curly braces. Choice B is incorrect as it suggests using the 'new' keyword with a size, which is a different initialization method not shown in the code. To help students: Compare and contrast the two main ways to create arrays (new keyword vs initializer lists), emphasize that initializer lists automatically determine array size based on the number of elements provided, and practice recognizing different initialization patterns in code.
- 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? Answer: Change to: int[] stock = {4, 7, 2, 9};
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.
- 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[0]; // first day
System.out.println(today);
}
} Answer: int
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 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? Answer: double t = highs[2];
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.