zuai-logo
zuai-logo
  1. AP Computer Science A
FlashcardFlashcard
Study GuideStudy GuideQuestion BankQuestion Bank

What is an array?

A data structure that stores a fixed-size, sequential collection of elements of the same type.

Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

All Flashcards

What is an array?

A data structure that stores a fixed-size, sequential collection of elements of the same type.

What is a zero-indexed language?

A programming language where the first element in an array or list has an index of 0.

What is array length?

The number of elements an array can hold, which is fixed upon creation.

What is bracket notation?

The method of accessing array elements using square brackets and the index of the element.

What is ArrayIndexOutOfBoundsException?

An exception thrown when trying to access an array element with an index that is outside the valid range (0 to array.length - 1).

What is a constructor?

A special method used to create and initialize objects, including arrays.

What is 'null' in the context of arrays?

The default value for array elements of reference types, indicating that the element does not currently refer to any object.

What does 'pre-initialized' mean in the context of arrays?

Arrays that are initialized with values at the time of their creation, rather than being populated later.

What is a reference type?

A data type that refers to an object, and whose variables hold references to these objects, not the objects themselves.

What is an import statement?

A statement used to access classes and functionalities from external libraries or packages in a programming language.

What does the following code output?

java
int[] numbers = new int[5];
System.out.println(numbers[0]);

0

What does the following code output?

java
boolean[] flags = new boolean[3];
System.out.println(flags[1]);

false

What does the following code output?

java
String[] names = new String[2];
System.out.println(names[0]);

null

What does the following code output?

java
int[] values = {10, 20, 30};
System.out.println(values[2]);

30

Identify the error in the following code:

java
int[] nums = {1, 2, 3};
System.out.println(nums[3]);

ArrayIndexOutOfBoundsException: The index 3 is out of bounds for the array nums (valid indices are 0, 1, and 2).

Identify the error in the following code:

java
int[] numbers = new int[5];
numbers[5] = 10;

ArrayIndexOutOfBoundsException: The index 5 is out of bounds for the array numbers (valid indices are 0 to 4).

What does the following code output?

java
int[] arr = {5, 10, 15};
System.out.println(arr.length);

3

What does the following code output?

java
int[] data = new int[4];
System.out.println(data[0] + data[1]);

0

Identify the error in the following code:

java
int[] sizes = {1, 2, 3};
System.out.println(sizes.length());

The .length is being called as a method, but it is an instance variable of the array. Remove the parentheses: sizes.length

What does the following code output?

java
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[numbers.length - 1]);

5

Why are arrays of fixed size?

Arrays are fixed in size because their memory allocation is contiguous, making it efficient to access elements but difficult to resize dynamically.

Why is understanding array indexing important?

Correct array indexing is crucial for accessing and manipulating array elements without causing errors like ArrayIndexOutOfBoundsException.

What is the significance of 'arrayName.length'?

It provides a way to determine the size of an array, which is essential for iterating over the array and preventing out-of-bounds errors.

How does Java initialize array elements by default?

Java initializes array elements to default values based on their data type (0 for integers, 0.0 for doubles, false for booleans, and null for reference types).

What are the benefits of using pre-initialized arrays?

Pre-initialized arrays allow for the direct assignment of values upon creation, simplifying code and improving readability when the initial values are known.

Why are arrays considered reference types?

Arrays are reference types because the variable holds a reference (memory address) to the array object, not the array object itself.

What is the role of the 'new' keyword when creating arrays?

The 'new' keyword is used to allocate memory for the array in the heap, creating a new array object.

How does the choice of data type affect array creation?

The data type determines the type of elements the array can store and the amount of memory allocated for each element.

What is the impact of using an incorrect array index?

Using an incorrect array index can lead to an ArrayIndexOutOfBoundsException, causing the program to crash or produce unexpected results.

How do arrays relate to data structures in computer science?

Arrays are a fundamental data structure used to organize and store collections of data, serving as the basis for more complex data structures.