AP Computer Science a Quiz: Array Creation And Access
20 questions · exam conditions
0:00
Array Creation And AccessQuestion 1 of 20

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?

double
int
String
boolean
← Back to quizzes

AP Computer Science a Quiz

AP Computer Science a Quiz: Array Creation And Access

Practice Array Creation And Access in AP Computer Science a with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.

What this quiz covers

This quiz focuses on Array Creation And Access, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.

How to use this quiz

Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.

All questions

Question 1

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?

  1. double (correct answer)
  2. int
  3. String
  4. boolean

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.

Question 2

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?

  1. double t = highs[3];
  2. double t = highs[2]; (correct answer)
  3. double t = highs(2);
  4. double t = highs[1];

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.

Question 3

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
  }
}
  1. Create a new int[] with one extra slot and copy values. (correct answer)
  2. Use scores.add(600) to append a new score.
  3. Assign scores[3] = 600 without changing the array size.
  4. Increase scores.length to 4 to make room for 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.

Question 4

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?

  1. int
  2. double (correct answer)
  3. String
  4. boolean

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.

Question 5

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
  }
}
  1. int
  2. String (correct answer)
  3. double
  4. boolean

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.

Question 6

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);
  }
}
  1. String
  2. int (correct answer)
  3. double
  4. char

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.

Question 7

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[productId]; // 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?

  1. 12
  2. 5 (correct answer)
  3. 0
  4. 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.

Question 8

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?

  1. Change to: int[] stock = new int[4];
  2. Use: stock.add(9);
  3. Use: stock[3] = 9; without resizing
  4. Change to: int[] stock = {4, 7, 2, 9}; (correct answer)

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.

Question 9

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[5]; // 5 students
    grades[0] = 90;
    grades[1] = 85;
  }
}

The array stores whole-number scores. How is the array grades initialized in the code?

  1. It is created with length 5 using new int[5]. (correct answer)
  2. It is created with length 4 because indices go 0-4.
  3. It is initialized with values {90, 85, 0, 0, 0}.
  4. It is initialized as an ArrayList of 5 integers.

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.

Question 10

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[2] = stock[2] - 1; // sold one unit of product 2
    System.out.println(stock[2]);
  }
}

Indexing starts at 0. What is the output of accessing element at index 2 in the array?

  1. 9
  2. 8 (correct answer)
  3. 3
  4. 7

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.

Question 11

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?

  1. String
  2. int (correct answer)
  3. double
  4. char

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.

Question 12

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[1];
    System.out.println(pick);
  }
}

Array indexing starts at 0. What is the output of accessing element at index 1 in the array?

  1. Dune
  2. 1984 (correct answer)
  3. Hamlet
  4. 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.

Question 13

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?

  1. 92
  2. 76
  3. 95 (correct answer)
  4. 88

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.

Question 14

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?

  1. scores.add(500);
  2. scores = {400, 250, 125, 500};
  3. scores = new int[]{400, 250, 125, 500}; (correct answer)
  4. scores.length = 4;

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.

Question 15

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
  }
}
  1. 10
  2. 2 (correct answer)
  3. 3
  4. 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.

Question 16

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
  }
}
  1. It is created with length 3 using new double[3]. (correct answer)
  2. It is created with length 2 using new double[2].
  3. It is created using {88.5, 91.0, 79.5} only.
  4. It is created by calling grades.add(88.5).

Explanation: 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.

Question 17

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
  }
}
  1. 1200
  2. 1800
  3. 1500 (correct answer)
  4. scores[1]

Explanation: 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.

Question 18

Library System: Given the code snippet, which line of code correctly accesses the third element of the array?

public class LibraryCatalog {
  public static void main(String[] args) {
    // Each index stores a book ID in the catalog
    String[] bookIds = {"BK101", "BK205", "BK330", "BK412"}; // String array: IDs

    // Choose the correct way to access the third ID
  }
}
  1. bookIds[3]
  2. bookIds[2] (correct answer)
  3. bookIds(2)
  4. bookIds.get(2)

Explanation: This question tests AP Computer Science A skills: understanding array creation and access in Java, specifically using correct syntax and indexing to access array elements. In Java, arrays are fixed-size data structures accessed using square bracket notation with zero-based indexing, where the first element is at index 0. The provided code snippet demonstrates a String array 'bookIds' containing book IDs, and asks for the correct way to access the third element. Choice B (bookIds[2]) is correct because to access the third element in a zero-based indexing system, we use index 2, with proper square bracket syntax. Choice A (bookIds[3]) is incorrect as it would access the fourth element, demonstrating a common off-by-one error where students forget about zero-based indexing. To help students: Create exercises that map ordinal positions (first, second, third) to array indices (0, 1, 2), emphasize that parentheses and dot notation like get() are for other data structures, not arrays.

Question 19

Inventory Management: Given the code snippet, what is the output of accessing element at index 2 in the array?

public class StoreStock {
  public static void main(String[] args) {
    // Each index represents a product's stock count
    int[] stock = {12, 5, 9, 0}; // int array: counts of items in stock

    System.out.println(stock[2]); // access the third product
  }
}
  1. 5
  2. 9 (correct answer)
  3. 0
  4. stock[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. The provided code snippet demonstrates creating an array 'stock' of int type with values {12, 5, 9, 0} and accessing elements using their index positions. Choice B (9) is correct because stock[2] accesses the third element in the array (index 2), which contains the value 9, adhering to zero-based indexing. Choice C (0) might be chosen by students who incorrectly think index 2 means the second element, while Choice A (5) would result from accessing index 1 instead. To help students: Emphasize practicing array creation and access through coding exercises, highlight the importance of zero-based indexing with visual diagrams showing index positions 0, 1, 2, 3 corresponding to elements, and encourage tracing through code step-by-step to avoid indexing errors.

Question 20

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);
  }
}
  1. It is created with five values using an initializer list. (correct answer)
  2. It is created with length 6 using new int[6].
  3. It is created as a String array with five elements.
  4. It is created by calling temps.append(65).

Explanation: 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.