All Flashcards
What are the differences between a for loop and an enhanced for loop when traversing arrays?
For Loop: Requires managing indices, can modify array elements. | Enhanced For Loop: Simpler syntax, cannot modify array elements, no index access.
What are the differences between primitive type arrays and object type arrays?
Primitive Type Arrays: Store actual values, default values are 0/false. | Object Type Arrays: Store references, default value is null.
What are the differences between 1D and 2D arrays?
1D Arrays: Single row of elements. | 2D Arrays: Multiple rows and columns of elements.
What are the differences between Arrays and ArrayLists?
Arrays: Fixed size, primitive data types and objects. | ArrayLists: Dynamic size, objects only.
What are the differences between initializing an array with default values and initializing it with specific values?
Default Values: Elements are set to default values (0, false, null). | Specific Values: Elements are assigned specific values upon creation.
What are the differences between accessing an array element using a valid index and an invalid index?
Valid Index: Returns the value at that index. | Invalid Index: Throws an ArrayIndexOutOfBoundsException.
What are the differences between shifting elements left and shifting elements right in an array?
Shifting Left: Elements move towards the beginning of the array. | Shifting Right: Elements move towards the end of the array.
What are the differences between determining the minimum value and the maximum value in an array?
Minimum Value: Find the smallest element. | Maximum Value: Find the largest element.
What are the differences between determining if at least one element meets a criteria and determining if all elements meet a criteria?
At Least One: Stop iterating when one element meets the criteria. | All Elements: Iterate through all elements to ensure all meet the criteria.
What are the differences between reversing an array in place and creating a new reversed array?
In Place: Modifies the original array. | New Array: Creates a new array with the reversed elements, leaving the original array unchanged.
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
What is array initialization?
Assigning an initial size and optionally values to the elements of an array when it is created.
What is the significance of array indices?
Array indices provide a way to access and manipulate individual elements within the array.
What is array traversal?
Iterating through each element of the array to perform an operation.
Why is it important to avoid ArrayIndexOutOfBoundsException?
Accessing an invalid index can lead to program crashes or unexpected behavior.
How does the enhanced for loop simplify array processing?
It provides a concise way to iterate through array elements without managing indices, but it can't modify the array.
What happens if an array is created but not explicitly populated with values?
The elements of the array will be initialized with the default values for their type (e.g., 0 for int, false for boolean, null for objects).
Why are arrays fixed in size?
Arrays are fixed in size to ensure memory allocation is contiguous and efficient, which allows for fast access to elements.
What is the purpose of using loops with arrays?
Loops allow for efficient processing of each element in an array, such as modifying values, searching for specific elements, or performing calculations.
What are some common array algorithms?
Finding min/max values, computing sums/averages, determining if elements meet certain criteria, shifting/rotating elements, and reversing order.
What are the limitations of enhanced for loops?
They cannot be used to modify the original array, and they do not provide access to the index of the elements.