A one-dimensional array in Java is a collection of elements of the same type stored in a single row. It allows you to store multiple values in a single variable, accessed by an index starting at 0.
Click to reveal answer
beginner
How do you declare a one-dimensional array of integers named 'numbers' with 5 elements in Java?
You declare it like this: int[] numbers = new int[5]; This creates an array that can hold 5 integers, all initially set to 0.
Click to reveal answer
beginner
How do you access the third element in a one-dimensional array named 'data'?
You access it using its index: data[2] Remember, array indexes start at 0, so the third element is at index 2.
Click to reveal answer
intermediate
What happens if you try to access an array index that is out of bounds in Java?
Java throws an ArrayIndexOutOfBoundsException. This means you tried to access an index less than 0 or greater than or equal to the array length.
Click to reveal answer
beginner
How can you initialize a one-dimensional array with values at the time of declaration?
You can write: int[] numbers = {10, 20, 30, 40, 50}; This creates an array with 5 elements already set to those values.
Click to reveal answer
What is the index of the first element in a Java array?
A1
B0
C-1
DDepends on the array size
✗ Incorrect
Java arrays start indexing at 0, so the first element is at index 0.
Which of the following is the correct way to declare a one-dimensional array of doubles named 'prices' with 10 elements?
Adouble prices = new double[10];
Bdouble prices[] = new double;
Cdouble prices[] = new double(10);
Ddouble[] prices = new double[10];
✗ Incorrect
The correct syntax is double[] prices = new double[10]; to declare an array of 10 doubles.
What will happen if you try to access array element at index 5 in an array declared as int[] arr = new int[5];?
AReturns the last element
BReturns 0
CThrows ArrayIndexOutOfBoundsException
DCreates a new element at index 5
✗ Incorrect
Since valid indexes are 0 to 4, accessing index 5 causes an ArrayIndexOutOfBoundsException.
How do you find the length of an array named 'items' in Java?
Aitems.length
Bitems.size()
Citems.length()
Dlength(items)
✗ Incorrect
In Java, arrays have a public final field called length accessed as items.length.
Which of these initializes an array with values 1, 2, 3 in one line?
Aint arr[] = {1, 2, 3};
Bint[] arr = new int[3]{1, 2, 3};
Cint arr = {1, 2, 3};
Dint[] arr = (1, 2, 3);
✗ Incorrect
You can initialize an array with values using int arr[] = {1, 2, 3};.
Explain what a one-dimensional array is and how you declare one in Java.
Think about a list of items stored in a single row.
You got /3 concepts.
Describe how to access and modify elements in a one-dimensional array in Java.
Imagine picking or changing an item in a numbered list.
You got /3 concepts.
Practice
(1/5)
1.
What is the starting index of a one-dimensional array in Java?
easy
A. 1
B. 0
C. -1
D. Depends on the array size
Solution
Step 1: Understand Java array indexing
Java arrays always start indexing from 0, meaning the first element is at index 0.
Step 2: Confirm the starting index
Since the first element is at index 0, the starting index is 0 regardless of array size.
Final Answer:
0 -> Option B
Quick Check:
Array index starts at 0 [OK]
Hint: Remember, Java arrays always start at zero index [OK]
Common Mistakes:
Thinking arrays start at 1
Assuming negative indexes
Confusing with other languages
2.
Which of the following is the correct way to declare and create an integer array of size 5 in Java?
?
easy
A. int[] arr = int[5];
B. int arr = new int[5];
C. int arr[] = new int();
D. int[] arr = new int[5];
Solution
Step 1: Check array declaration syntax
In Java, arrays are declared with type[], for example int[]. The new keyword creates the array with size.
Step 2: Validate each option
int[] arr = new int[5]; uses correct syntax: int[] arr = new int[5]; Options A, B, and C have syntax errors or missing parts.
Final Answer:
int[] arr = new int[5]; -> Option D
Quick Check:
Correct array declaration uses new int[size] [OK]
Hint: Use 'type[] name = new type[size];' for arrays [OK]
Common Mistakes:
Missing new keyword
Wrong brackets placement
Using parentheses instead of brackets
3.
What will be the output of the following Java code?
The array 'numbers' has elements: index 0=10, 1=20, 2=30, 3=40.
Step 2: Access the element at index 2
numbers[2] refers to the third element, which is 30.
Final Answer:
30 -> Option C
Quick Check:
Index 2 value is 30 [OK]
Hint: Remember array index starts at 0, so 2 means third element [OK]
Common Mistakes:
Confusing index 2 with 1 or 3
Expecting 40 instead of 30
Thinking index starts at 1
4.
Find the error in the following code snippet:
int[] arr = new int[3];
arr[3] = 10;
System.out.println(arr[3]);
medium
A. Index 3 is out of bounds for array of size 3
B. Missing initialization of array elements
C. Array size should be 4
D. Syntax error in array declaration
Solution
Step 1: Check array size and valid indexes
The array size is 3, so valid indexes are 0, 1, and 2.
Step 2: Identify the invalid index usage
Accessing arr[3] is invalid and causes ArrayIndexOutOfBoundsException.
Final Answer:
Index 3 is out of bounds for array of size 3 -> Option A
Quick Check:
Array indexes go from 0 to size-1 [OK]
Hint: Array indexes must be less than size [OK]
Common Mistakes:
Using index equal to size
Assuming array auto-expands
Ignoring runtime exceptions
5.
Given an integer array arr of size 5 initialized as {2, 4, 6, 8, 10}, which code snippet correctly doubles each element's value?
?
hard
A. for(int i = 0; i < arr.length; i++) { arr[i] *= 2; }
B. for(int i = 0; i <= arr.length; i++) { arr[i] = arr[i] * 2; }
C. for(int i = 1; i < arr.length; i++) { arr[i] = arr[i] * 2; }
D. for(int i = 0; i < arr.length - 1; i++) { arr[i] = arr[i] * 2; }
Solution
Step 1: Understand array length and loop bounds
The array length is 5, so valid indexes are 0 to 4. Loop must run from 0 to less than length.
Step 2: Evaluate each option's loop condition
for(int i = 0; i <= arr.length; i++) { arr[i] = arr[i] * 2; } uses i <= arr.length which causes out of bounds error at i=5. for(int i = 0; i < arr.length; i++) { arr[i] *= 2; } correctly uses i < arr.length. for(int i = 1; i < arr.length; i++) { arr[i] = arr[i] * 2; } starts at 1, missing index 0. for(int i = 0; i < arr.length - 1; i++) { arr[i] = arr[i] * 2; } stops at length-1, missing last element.
Final Answer:
for(int i = 0; i < arr.length; i++) { arr[i] *= 2; } -> Option A
Quick Check:
Loop from 0 to length-1 doubles all elements [OK]
Hint: Use i < arr.length to avoid index errors [OK]
Common Mistakes:
Using <= arr.length causing errors
Starting loop at 1 missing first element
Stopping loop early missing last element
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.