Bird
Raised Fist0
DSA Javaprogramming~20 mins

Array Insertion at End in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after inserting elements at the end of the array?
Consider the following Java code that inserts elements at the end of an array. What will be printed?
DSA Java
public class Main {
    public static void main(String[] args) {
        int[] arr = new int[5];
        int size = 0;
        // Insert 10
        arr[size++] = 10;
        // Insert 20
        arr[size++] = 20;
        // Insert 30
        arr[size++] = 30;
        // Print array elements up to size
        for (int i = 0; i < size; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
A10 20 30 0
B10 20 30
C10 20 30 0 0
D0 0 0
Attempts:
2 left
💡 Hint
Focus on how many elements are printed and what the size variable controls.
❓ Predict Output
intermediate
2:00remaining
What happens if you insert beyond the array size?
What error or output will this Java code produce when inserting beyond the array's capacity?
DSA Java
public class Main {
    public static void main(String[] args) {
        int[] arr = new int[3];
        int size = 0;
        arr[size++] = 1;
        arr[size++] = 2;
        arr[size++] = 3;
        arr[size++] = 4; // Inserting beyond capacity
        for (int i = 0; i < size; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
A1 2 3
B1 2 3 4
CArrayIndexOutOfBoundsException
DCompilation error
Attempts:
2 left
💡 Hint
Think about what happens when you try to access an index outside the array length.
🧠 Conceptual
advanced
1:30remaining
Why is tracking 'size' important when inserting at the end of an array?
In array insertion at the end, why do we keep a separate 'size' variable instead of relying on array.length?
ABecause array.length gives the total capacity, not the number of inserted elements
BBecause size is needed to resize the array automatically
CBecause size stores the last inserted element's value
DBecause array.length changes dynamically when inserting elements
Attempts:
2 left
💡 Hint
Think about the difference between capacity and actual data stored.
❓ Predict Output
advanced
2:00remaining
What is the output after multiple insertions and printing the array?
What will be printed after running this Java code that inserts elements at the end and prints the array?
DSA Java
public class Main {
    public static void main(String[] args) {
        int[] arr = new int[4];
        int size = 0;
        arr[size++] = 5;
        arr[size++] = 10;
        arr[size++] = 15;
        arr[size++] = 20;
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
A5 10 15 20
B5 10 15 0
C0 0 0 0
D5 10 15
Attempts:
2 left
💡 Hint
The array is fully filled; printing all elements shows all inserted values.
🔧 Debug
expert
3:00remaining
Identify the bug in this array insertion code
What is the bug in this Java code that inserts elements at the end of an array?
DSA Java
public class Main {
    public static void main(String[] args) {
        int[] arr = new int[3];
        int size = 0;
        insert(arr, size, 1);
        insert(arr, size, 2);
        insert(arr, size, 3);
        for (int i = 0; i < size; i++) {
            System.out.print(arr[i] + " ");
        }
    }
    public static void insert(int[] arr, int size, int value) {
        arr[size] = value;
        size++;
    }
}
AThe array size is too small for the insertions
BThe for loop should run until arr.length instead of size
CThe insert method should return the new size but does not
DThe size variable is not updated outside the insert method, so size remains 0
Attempts:
2 left
💡 Hint
Consider how Java passes primitive variables to methods.

Practice

(1/5)
1. What happens when you insert an element at the end of a fixed-size array in Java without resizing?
easy
A. The insertion throws an error if the array is full
B. The element is added at the next free position if available
C. The array automatically resizes to fit the new element
D. The element replaces the last element if the array is full

Solution

  1. Step 1: Understand fixed-size array behavior

    Java arrays have a fixed size and cannot grow automatically. Attempting arr[size] where size == length throws ArrayIndexOutOfBoundsException.
  2. Step 2: Inserting at end when full

    Standard insertion arr[size++] = element; if full (size == arr.length), accesses arr[length] and throws runtime error.
  3. Final Answer:

    The insertion throws an error if the array is full -> Option A
  4. Quick Check:

    Fixed-size array throws error when full [OK]
Hint: Fixed arrays don't resize; throws error if full [OK]
Common Mistakes:
  • Assuming arrays resize automatically
  • Thinking insertion overwrites last element
  • Believing new element is added beyond array size
2. Which of the following is the correct syntax to insert an element at the end of an array in Java if the array has space?
easy
A. arr[size] = newElement; size++;
B. arr[arr.length - 1] = newElement;
C. arr[arr.length] = newElement;
D. arr.add(newElement);

Solution

  1. Step 1: Understand array indexing and size tracking

    Arrays use zero-based indexing; last index is length-1. We track current size separately.
  2. Step 2: Insert element at next free position

    Use size as index to insert, then increment size to reflect new element count.
  3. Final Answer:

    arr[size] = newElement; size++; -> Option A
  4. Quick Check:

    Use size as index and increment after insertion [OK]
Hint: Use size variable as index, then increment size [OK]
Common Mistakes:
  • Using arr.length as index (out of bounds)
  • Trying to use arr.add() which is for lists
  • Overwriting last element without size tracking
3. What will be the output of the following Java code?
int[] arr = new int[5];
int size = 0;
arr[size++] = 10;
arr[size++] = 20;
arr[size++] = 30;
System.out.print(java.util.Arrays.toString(arr));
medium
A. [10, 20, 30]
B. [10, 20, 30, 0, 0]
C. [10, 20, 30, 30, 30]
D. [0, 10, 20, 30, 0]

Solution

  1. Step 1: Trace insertions with size increment

    Elements 10, 20, 30 are inserted at indexes 0, 1, 2 respectively; size increments after each insertion.
  2. Step 2: Understand default array values

    Remaining positions (indexes 3 and 4) remain 0 as default for int arrays.
  3. Final Answer:

    [10, 20, 30, 0, 0] -> Option B
  4. Quick Check:

    Inserted elements at start; rest are zeros [OK]
Hint: Inserted elements fill start; rest remain default zero [OK]
Common Mistakes:
  • Assuming array shrinks to inserted elements only
  • Confusing index positions
  • Expecting uninitialized elements to be null
4. Identify the error in the following code that tries to insert an element at the end of an array:
int[] arr = new int[3];
int size = 3;
arr[size] = 5;
medium
A. Compilation error due to wrong syntax
B. No error; element inserted at the end
C. ArrayIndexOutOfBoundsException because size equals array length
D. Logical error; element overwrites first element

Solution

  1. Step 1: Check array size and index used

    Array length is 3, valid indexes are 0,1,2. Using index 3 causes error.
  2. Step 2: Understand runtime exception

    Accessing arr[3] throws ArrayIndexOutOfBoundsException at runtime.
  3. Final Answer:

    ArrayIndexOutOfBoundsException because size equals array length -> Option C
  4. Quick Check:

    Index equals length causes runtime error [OK]
Hint: Index must be less than array length to avoid error [OK]
Common Mistakes:
  • Assuming index == length is valid
  • Expecting compile-time error instead of runtime
  • Ignoring size variable correctness
5. You have an integer array of size 4: int[] arr = {1, 2, 3, 4}; and a variable size = 4;. You want to insert 5 at the end but the array is full. Which approach correctly adds 5 at the end without losing existing data?
hard
A. Overwrite arr[3] with 5 directly
B. Increase size variable to 5 and assign arr[4] = 5
C. Use arr[size] = 5 without resizing
D. Create a new array of size 5, copy old elements, then add 5 at index 4

Solution

  1. Step 1: Recognize fixed array size limitation

    Original array size is 4; cannot add element beyond index 3 without resizing.
  2. Step 2: Create new larger array and copy elements

    Create new array of size 5, copy existing elements, then insert 5 at index 4.
  3. Final Answer:

    Create a new array of size 5, copy old elements, then add 5 at index 4 -> Option D
  4. Quick Check:

    Resize by new array and copy before insertion [OK]
Hint: Resize array by copying before adding new element [OK]
Common Mistakes:
  • Trying to insert without resizing
  • Overwriting last element losing data
  • Assuming size variable controls array length
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