Use Arrays.sort(arr); to sort the array in ascending order. Example:
Arrays.sort(arr);
Click to reveal answer
Which of the following is the correct way to declare an array of strings named names?
AString[] names = new String;
BString[] names;
CString names = new String[];
DString names[] = ();
✗ Incorrect
Option B correctly declares an array of strings. Options A, B, and C are invalid.
How do you access the third element of an array arr?
Aarr.get(3)
Barr[3]
Carr[2]
Darr(2)
✗ Incorrect
Arrays in Java start at index 0, so the third element is at index 2.
What does arr.length return?
AThe number of elements in the array
BThe last element of the array
CThe size of the array in bytes
DThe index of the last element
✗ Incorrect
arr.length gives the total number of elements stored in the array.
Which method sorts an array arr in Java?
AArrays.sort(arr);
Barr.sort();
CCollections.sort(arr);
Darr.sorted();
✗ Incorrect
The static method Arrays.sort() sorts arrays. Collections.sort() is for lists.
How do you loop through all elements of an array arr using a for-each loop?
Afor each (int x in arr) { }
Bfor (int i = 0; i < arr.length; i++) { }
Cfor (int i = 1; i <= arr.length; i++) { }
Dfor (int x : arr) { }
✗ Incorrect
Option D is the correct for-each loop syntax in Java.
Explain how to declare, initialize, and access elements in a Java array.
Think about how you create a list of items and get each item by its position.
You got /3 concepts.
Describe how to loop through all elements of an array and how to sort an array in Java.
Consider how you would read each item one by one and then arrange them in order.
You got /2 concepts.
Practice
(1/5)
1. Which of the following correctly declares an array of integers with 5 elements in Java?
easy
A. int[] numbers = int[5];
B. int numbers = new int[5];
C. int numbers[] = new int;
D. int[] numbers = new int[5];
Solution
Step 1: Understand Java array declaration syntax
In Java, arrays are declared with the type followed by square brackets, then the variable name, and initialized with 'new' and size.
Step 2: Check each option for correct syntax
int[] numbers = new int[5]; uses correct syntax: int[] numbers = new int[5]; Options A, B, and D have syntax errors or missing parts.
Final Answer:
int[] numbers = new int[5]; -> Option D
Quick Check:
Correct array declaration = int[] numbers = new int[5]; [OK]
Hint: Remember: type[] name = new type[size]; for arrays [OK]
Common Mistakes:
Missing square brackets after type
Not using 'new' keyword
Incorrect variable initialization
2. Which of the following is the correct way to access the third element of an array named arr in Java?
easy
A. arr(3);
B. arr[2];
C. arr[3];
D. arr.get(2);
Solution
Step 1: Recall Java array indexing
Java arrays use zero-based indexing, so the first element is at index 0, second at 1, third at 2.
Step 2: Identify correct syntax for accessing elements
Elements are accessed with square brackets []. So arr[2] accesses the third element. Options A and D use incorrect syntax, and C accesses the fourth element.
Final Answer:
arr[2]; -> Option B
Quick Check:
Third element index = 2 [OK]
Hint: Arrays start at 0, so third element is arr[2] [OK]
Common Mistakes:
Using parentheses instead of brackets
Using 1-based indexing
Trying to use methods like get() on arrays
3. What is the output of the following Java code?
int[] nums = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
System.out.println(sum);
medium
A. 15
B. 10
C. 5
D. 0
Solution
Step 1: Understand the array and loop
The array nums contains 1, 2, 3, 4, 5. The loop runs from i=0 to i=4 (nums.length is 5).
Step 2: Calculate the sum of all elements
sum = 0 + 1 + 2 + 3 + 4 + 5 = 15.
Final Answer:
15 -> Option A
Quick Check:
Sum of 1 to 5 = 15 [OK]
Hint: Sum array by adding all elements in a loop [OK]
Common Mistakes:
Off-by-one error in loop
Starting sum at wrong value
Using wrong loop condition
4. The following code is intended to print all elements of the array data, but it causes a runtime error. What is the error?
int[] data = {10, 20, 30};
for (int i = 1; i <= data.length; i++) {
System.out.println(data[i]);
}
medium
A. ArrayIndexOutOfBoundsException because loop goes beyond last index
B. Syntax error due to wrong for loop syntax
C. Prints all elements correctly
D. NullPointerException because array is not initialized
Solution
Step 1: Analyze loop indices and array length
The array has length 3, valid indices are 0, 1, 2. The loop runs i from 1 to 3 inclusive.
Step 2: Identify index causing error
When i = 3, data[3] is accessed, which is out of bounds and causes ArrayIndexOutOfBoundsException.
Final Answer:
ArrayIndexOutOfBoundsException because loop goes beyond last index -> Option A
Quick Check:
Loop index must be less than length [OK]
Hint: Loop indices must be 0 to length-1 for arrays [OK]
Common Mistakes:
Using <= instead of < in loop condition
Starting loop at 1 instead of 0
Ignoring zero-based indexing
5. Given an integer array arr, which code snippet correctly counts how many elements are greater than 10?
hard
A. int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= 10) {
count++;
}
}
System.out.println(count);
B. int count = 0;
for (int i = 1; i <= arr.length; i++) {
if (arr[i] > 10) {
count++;
}
}
System.out.println(count);
C. int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 10) {
count++;
}
}
System.out.println(count);
D. int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] < 10) {
count++;
}
}
System.out.println(count);
Solution
Step 1: Understand the counting condition
We want to count elements strictly greater than 10, so condition is arr[i] > 10.
Step 2: Check loop indices and condition correctness
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 10) {
count++;
}
}
System.out.println(count); uses correct zero-based indexing and correct condition. int count = 0;
for (int i = 1; i <= arr.length; i++) {
if (arr[i] > 10) {
count++;
}
}
System.out.println(count); has out-of-bounds error. int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= 10) {
count++;
}
}
System.out.println(count); counts elements >= 10 (includes 10). int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] < 10) {
count++;
}
}
System.out.println(count); counts elements less than 10, which is wrong.
Final Answer:
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 10) {
count++;
}
}
System.out.println(count); -> Option C
Quick Check:
Count elements with arr[i] > 10 using correct loop [OK]
Hint: Use i < arr.length and condition arr[i] > 10 to count [OK]
Common Mistakes:
Using <= instead of < in loop
Using wrong comparison operator
Starting loop at 1 causing index errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.