Bird
Raised Fist0
Javaprogramming~10 mins

One-dimensional arrays in Java - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - One-dimensional arrays
Declare array variable
↓
Allocate memory for array
↓
Initialize elements
↓
Access elements by index
↓
Use elements in operations
↓
End or reuse array
This flow shows how a one-dimensional array is declared, created, filled with values, accessed by index, and used.
Execution Sample
Java
int[] numbers = new int[3];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
System.out.println(numbers[1]);
This code creates an array of 3 integers, assigns values, and prints the second element.
Execution Table
StepActionArray StateIndex AccessedOutput
1Declare array variable 'numbers'null (no array yet)N/AN/A
2Allocate array of size 3[0, 0, 0]N/AN/A
3Assign numbers[0] = 10[10, 0, 0]0N/A
4Assign numbers[1] = 20[10, 20, 0]1N/A
5Assign numbers[2] = 30[10, 20, 30]2N/A
6Print numbers[1][10, 20, 30]120
7End of program[10, 20, 30]N/AN/A
💡 Program ends after printing the value at index 1.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
numbersnull[0, 0, 0][10, 0, 0][10, 20, 0][10, 20, 30][10, 20, 30]
Key Moments - 3 Insights
Why does the array start with zeros after allocation?
In Java, when you create an int array, all elements are automatically set to 0 by default, as shown in step 2 of the execution_table.
What happens if I try to access an index outside the array size?
Accessing an index outside 0 to length-1 causes an ArrayIndexOutOfBoundsException. This is not shown here but is important to avoid.
Why do we use numbers[1] to get the second element?
Array indexes start at 0 in Java, so numbers[1] accesses the second element, as shown in step 6 where the output is 20.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the array state after step 4?
A[10, 20, 0]
B[0, 20, 0]
C[10, 0, 0]
D[10, 20, 30]
💡 Hint
Check the 'Array State' column in row with Step 4.
At which step is the value 20 assigned to the array?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look at the 'Action' column and see when numbers[1] is assigned.
If we tried to print numbers[3], what would happen?
APrint 0
BArrayIndexOutOfBoundsException
CPrint 30
DPrint null
💡 Hint
Array size is 3, valid indexes are 0,1,2 as shown in variable_tracker.
Concept Snapshot
One-dimensional arrays in Java:
- Declare: int[] arr;
- Create: arr = new int[size];
- Indexing starts at 0
- Default int values are 0
- Access with arr[index]
- Out-of-bounds causes error
Full Transcript
This visual trace shows how a one-dimensional array is declared, created with size 3, initialized with values 10, 20, 30 at indexes 0,1,2 respectively, and how accessing numbers[1] prints 20. The array starts with zeros after allocation. Indexing starts at zero, so numbers[1] is the second element. Accessing outside the array size causes an error.

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

  1. Step 1: Understand Java array indexing

    Java arrays always start indexing from 0, meaning the first element is at index 0.
  2. Step 2: Confirm the starting index

    Since the first element is at index 0, the starting index is 0 regardless of array size.
  3. Final Answer:

    0 -> Option B
  4. 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

  1. Step 1: Check array declaration syntax

    In Java, arrays are declared with type[], for example int[]. The new keyword creates the array with size.
  2. 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.
  3. Final Answer:

    int[] arr = new int[5]; -> Option D
  4. 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?

int[] numbers = {10, 20, 30, 40};
System.out.println(numbers[2]);
medium
A. 40
B. 20
C. 30
D. ArrayIndexOutOfBoundsException

Solution

  1. Step 1: Identify the array elements and indexes

    The array 'numbers' has elements: index 0=10, 1=20, 2=30, 3=40.
  2. Step 2: Access the element at index 2

    numbers[2] refers to the third element, which is 30.
  3. Final Answer:

    30 -> Option C
  4. 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

  1. Step 1: Check array size and valid indexes

    The array size is 3, so valid indexes are 0, 1, and 2.
  2. Step 2: Identify the invalid index usage

    Accessing arr[3] is invalid and causes ArrayIndexOutOfBoundsException.
  3. Final Answer:

    Index 3 is out of bounds for array of size 3 -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    for(int i = 0; i < arr.length; i++) { arr[i] *= 2; } -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard