Bird
Raised Fist0
DSA C#programming~10 mins

Array Insertion at End in DSA C# - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to insert a new element at the end of the array.

DSA C#
int[] arr = new int[5] {1, 2, 3, 4, 0};
arr[[1]] = 5;
Console.WriteLine(string.Join(", ", arr));
Drag options to blanks, or click blank then click option'
A3
B4
C5
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 5 which is out of bounds.
Using index 0 which overwrites the first element.
2fill in blank
medium

Complete the code to increase the size of the array and insert a new element at the end.

DSA C#
int[] arr = new int[3] {1, 2, 3};
int[] newArr = new int[arr.Length + 1];
for (int i = 0; i < arr.Length; i++)
{
    newArr[i] = arr[i];
}
newArr[[1]] = 4;
arr = newArr;
Console.WriteLine(string.Join(", ", arr));
Drag options to blanks, or click blank then click option'
A0
B4
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using an index equal to newArr.Length which is out of bounds.
Using index 0 which overwrites the first element.
3fill in blank
hard

Fix the error in the code to correctly insert an element at the end of the array.

DSA C#
int[] arr = new int[4] {1, 2, 3, 0};
arr[[1]] = 4;
Console.WriteLine(string.Join(", ", arr));
Drag options to blanks, or click blank then click option'
A3
B4
C-1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 4 which is out of bounds.
Using negative index.
4fill in blank
hard

Fill both blanks to create a new array with one extra space and insert a new element at the end.

DSA C#
int[] arr = new int[2] {1, 2};
int[] newArr = new int[arr.[1] + 1];
for (int i = 0; i < arr.[2]; i++)
{
    newArr[i] = arr[i];
}
newArr[2] = 3;
arr = newArr;
Console.WriteLine(string.Join(", ", arr));
Drag options to blanks, or click blank then click option'
ALength
BCount
CSize
DCapacity
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Count' which is not valid for arrays.
Using 'Size' or 'Capacity' which do not exist for arrays.
5fill in blank
hard

Fill all three blanks to copy elements, insert a new element at the end, and update the original array.

DSA C#
int[] arr = new int[3] {10, 20, 30};
int[] newArr = new int[arr.[1] + 1];
for (int [2] = 0; [2] < arr.Length; [2]++)
{
    newArr[[2]] = arr[[2]];
}
newArr[arr.Length] = 40;
arr = newArr;
Console.WriteLine(string.Join(", ", arr));
Drag options to blanks, or click blank then click option'
ALength
Bi
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count' which is invalid for arrays.
Using different variable names inconsistently.

Practice

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

Solution

  1. Step 1: Understand fixed-size array behavior

    In C#, arrays have a fixed size once created and cannot grow automatically.
  2. Step 2: Inserting at the end without resizing

    Inserting at the end requires accessing index arr.Length, which is out of bounds and throws IndexOutOfRangeException.
  3. Final Answer:

    An error occurs because the array size is fixed -> Option A
  4. Quick Check:

    Fixed-size array insert at end causes error [OK]
Hint: Fixed arrays can't grow; insert at end causes error [OK]
Common Mistakes:
  • Assuming array resizes automatically
  • Thinking insertion shifts elements
  • Believing insertion adds new slot
2. Which of the following is the correct syntax to insert an element at the end of a List<int> named numbers in C#?
easy
A. numbers.Push(5);
B. numbers.InsertEnd(5);
C. numbers.Add(5);
D. numbers.Append(5);

Solution

  1. Step 1: Identify List insertion method

    In C#, List<T> uses the Add() method to insert elements at the end.
  2. Step 2: Check other options

    InsertEnd, Push, and Append are not valid List<T> methods in C#.
  3. Final Answer:

    numbers.Add(5); -> Option C
  4. Quick Check:

    List Add method inserts at end [OK]
Hint: Use Add() to insert at end of List [OK]
Common Mistakes:
  • Using non-existent methods like InsertEnd or Push
  • Confusing List with Stack methods
  • Trying Append which is for strings
3. What will be the output of the following C# code?
int[] arr = new int[3] {1, 2, 3};
arr[2] = 10;
Console.WriteLine(string.Join(", ", arr));
medium
A. 1, 2, 3
B. 1, 2, 10
C. 10, 2, 3
D. Runtime error

Solution

  1. Step 1: Understand array initialization

    The array arr has 3 elements: 1, 2, 3 at indexes 0, 1, 2.
  2. Step 2: Assign new value at last index

    arr[2] = 10 replaces the last element (3) with 10.
  3. Final Answer:

    1, 2, 10 -> Option B
  4. Quick Check:

    Last element replaced with 10 [OK]
Hint: Index 2 is last element; assignment replaces it [OK]
Common Mistakes:
  • Thinking array resizes or shifts elements
  • Expecting original array unchanged
  • Assuming runtime error on assignment
4. Identify the error in the following code that tries to insert 7 at the end of an array:
int[] arr = new int[3] {1, 2, 3};
arr[arr.Length] = 7;
medium
A. Syntax error due to missing semicolon
B. Null reference exception
C. No error, code runs fine
D. Index out of range error

Solution

  1. Step 1: Check array index used

    arr.Length is 3, but valid indexes are 0,1,2 only.
  2. Step 2: Understand index out of range

    Accessing arr[3] causes an IndexOutOfRangeException at runtime.
  3. Final Answer:

    Index out of range error -> Option D
  4. Quick Check:

    Array index must be less than length [OK]
Hint: Array indexes go 0 to Length-1 only [OK]
Common Mistakes:
  • Using arr.Length as index directly
  • Ignoring runtime exceptions
  • Confusing syntax errors with runtime errors
5. You have an array int[] arr = new int[3] {1, 2, 3};. You want to insert 4 at the end without losing existing elements. Which approach is correct?
hard
A. Create a new array of size 4, copy old elements, then add 4 at last index
B. Assign arr[3] = 4 directly
C. Use arr.Add(4) method
D. Resize arr using arr.Resize(4) method

Solution

  1. Step 1: Understand fixed array size

    Arrays in C# have fixed size; cannot add elements beyond initial size.
  2. Step 2: Correct way to add element

    Create a new larger array, copy existing elements, then insert new element at the end.
  3. Step 3: Check other options

    Assigning arr[3] causes error; arr.Add() doesn't exist for arrays; arr.Resize() is invalid.
  4. Final Answer:

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

    Fixed arrays need new array to grow [OK]
Hint: Arrays fixed size; create new bigger array to add [OK]
Common Mistakes:
  • Trying to assign beyond array length
  • Using List methods on arrays
  • Assuming Resize method exists
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