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

What are the steps to declare and initialize an array in Java?

  1. Declare the array with type[] arrayName;. 2. Initialize the array with arrayName = new type[arraySize]; or type[] arrayName = {value1, value2, ...};
Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

All Flashcards

What are the steps to declare and initialize an array in Java?

  1. Declare the array with type[] arrayName;. 2. Initialize the array with arrayName = new type[arraySize]; or type[] arrayName = {value1, value2, ...};

What are the steps to access an element in an array?

  1. Use the array name followed by square brackets. 2. Inside the brackets, specify the index of the element you want to access (e.g., arrayName[index]).

What are the steps to traverse an array using a for loop?

  1. Initialize a loop counter i to 0. 2. Set the loop condition to i < array.length. 3. Increment i in each iteration. 4. Access each element using array[i].

What are the steps to find the minimum value in an array?

  1. Initialize minValue to the first element of the array. 2. Iterate through the array. 3. If the current element is less than minValue, update minValue to the current element. 4. After iteration, minValue holds the minimum value.

What are the steps to reverse the order of elements in an array?

  1. Create a new array of the same size. 2. Iterate through the original array from the last element to the first. 3. Copy each element to the new array in reverse order. 4. The new array contains the reversed elements.

What are the steps to shift elements in an array to the right?

  1. Store the last element. 2. Iterate from the second-to-last element to the first. 3. Move each element to the next index. 4. Place the stored last element in the first index.

What are the steps to determine if all elements in an array meet certain criteria?

  1. Initialize a boolean variable allMeetCriteria to true. 2. Iterate through the array. 3. If any element does not meet the criteria, set allMeetCriteria to false and break the loop. 4. After iteration, allMeetCriteria indicates whether all elements meet the criteria.

What are the steps to compute the average of elements in an array?

  1. Initialize a variable sum to 0. 2. Iterate through the array, adding each element to sum. 3. Divide sum by the number of elements in the array to get the average.

What are the steps to determine if there are duplicate elements in an array?

  1. Use nested loops to compare each element with every other element. 2. If any two elements are equal and have different indices, there are duplicates. 3. Return true if duplicates are found, false otherwise.

What are the steps to rotate elements in an array to the left?

  1. Store the first element. 2. Iterate from the second element to the last element. 3. Move each element to the previous index. 4. Place the stored first element in the last index.

What is the definition of Array?

A container object that holds a fixed number of values of a single type.

What is the definition of Index?

The position of an element within an array, starting from 0.

What is the definition of Traversing an array?

The process of visiting each element in an array and performing some operation on it.

What is the definition of ArrayIndexOutOfBoundsException?

An exception thrown when trying to access an array element with an invalid index (e.g., negative or beyond the array's length).

What is the definition of Enhanced For Loop?

A simplified for loop for iterating over elements in an array without needing to manage indices directly.

What is the definition of Zero-based indexing?

A system where the first element of an array has an index of 0, the second has an index of 1, and so on.

What is the definition of NullPointerException?

An exception thrown when attempting to use a reference that points to null (no object).

What is the definition of Data Structure?

A way of organizing and storing data in a computer so that it can be used efficiently.

What is the definition of Primitive Data Type?

Basic data types such as int, boolean, and double that are built into a programming language.

What is the definition of Reference Data Type?

Data types that store the memory address of an object, such as String and arrays.

What does the following code output?

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

0

What does the following code output?

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

10

What does the following code output?

java
int[] nums = {1, 2, 3, 4, 5};
int sum = 0;
for (int num : nums) {
 sum += num;
}
System.out.println(sum);

15

Identify the error in the following code:

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

ArrayIndexOutOfBoundsException. The valid indices are 0 to 4.

What does the following code output?

java
int[] arr = {5, 4, 3, 2, 1};
for (int i = 0; i < arr.length; i++) {
 arr[i] = arr[i] * 2;
}
System.out.println(arr[2]);

6

What does the following code output?

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

null

Identify the error in the following code:

java
int[] numbers = {1, 2, 3};
for (int i = 1; i <= numbers.length; i++) {
 System.out.println(numbers[i]);
}

ArrayIndexOutOfBoundsException. The loop should iterate from 0 to numbers.length - 1.

What does the following code output?

java
int[] values = {10, 20, 30, 40, 50};
int count = 0;
for (int value : values) {
 if (value > 25) {
 count++;
 }
}
System.out.println(count);

3

What does the following code output?

java
int[] numbers = {5, 2, 8, 1, 9};
int max = numbers[0];
for (int number : numbers) {
 if (number > max) {
 max = number;
 }
}
System.out.println(max);

9

What does the following code output?

java
int[] arr = {1, 2, 3, 4, 5};
int[] newArr = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
 newArr[i] = arr[arr.length - 1 - i];
}
System.out.println(newArr[0]);

5