Question 1: Creating Arrays in Java
Question type: Multiple Choice
Question body: Which of the following statements correctly initializes an array in Java?
Responses:
A. int[] array = new int[5];
B. int array = new int[5];
C. int array[] = int[5];
D. int[] array = int[5];
Correct response: A. int[] array = new int[5];
Explanation of the correct answer: Option A correctly initializes an array of integers with a length of 5. The syntax Type[] variableName = new Type[size]; is the correct way to declare and instantiate an array in Java.
Tip: Remember that arrays in Java are fixed in size once they are created, and the size must be specified at the time of creation.
Difficulty: Easy
Question 2: Traversing Arrays with For Loops
Question type: Complete the code
Question body: Complete the following code to iterate through the array numbers and print each value.
int[] numbers = {1, 2, 3, 4, 5};
for(_____ i = 0; i < _____.length; i++) {
System.out.println(__________);
}
Correct response: int; numbers; numbers[i]
Explanation of the correct answer: The complete code would be:
int[] numbers = {1, 2, 3, 4, 5};
for(int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
This loops through the array using a standard for loop, with i serving as the index to access each element of the array.
Tip: Use array.length to dynamically get the length of the array and avoid hard-coded values.
Difficulty: Easy
Question 3: Finding the Largest Value in an Array
Question type: Complete the code
Question body: Write a piece of code that finds the largest value in the values array.
int[] values = {2, 45, 34, 12, 89, 5};
int largest = ________;
for(int value : values) {
if(value > largest) {
largest = value;
}
}
System.out.println("The largest value is: " + largest);
Correct response: values[0]
Explanation of the correct answer: Initially setting largest to values[0] ensures that we are comparing the numbers within the array itself, starting with the very first element.
int largest = values[0];
Tip: Always initialize the variable you're comparing against with a sensible default value from the array itself if possible.
Difficulty: Medium
Question 4: Reverse Order an Array
Question type: Find the error
Question body: Identify the error in the following code snippet used to reverse an array.
int[] array = {1, 2, 3, 4, 5};
int temp;
for(int i = 0; i < array.length / 2; i++) {
temp = array[i];
array[i] = array[array.length - i];
array[array.length - i] = temp;
}
Correct response: The correct line should be array[i] = array[array.length - 1 - i];
Explanation of the correct answer: The mistake is in incorrectly accessing the mirror element in the array. To access the element from the end of the array, we need to subtract 1 additional from the length because array indexing is zero-based. Without subtracting 1, you will get an ArrayIndexOutOfBoundsException.
Tip: Remember that array indexing starts at 0, so the last element's index is always length - 1.
Difficulty: Medium