Bird
Raised Fist0
DSA C#programming~20 mins

Array Deletion at End in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Deletion 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 deleting the last element from the array?
Consider the following C# code that deletes the last element from an array by creating a new smaller array. What will be printed?
DSA C#
int[] arr = {10, 20, 30, 40, 50};
int n = arr.Length;
int[] newArr = new int[n - 1];
for (int i = 0; i < n - 1; i++)
{
    newArr[i] = arr[i];
}
foreach (int val in newArr)
{
    Console.Write(val + " ");
}
A20 30 40 50
B10 20 30 40 50
C10 20 30 40
D10 20 30 50
Attempts:
2 left
💡 Hint
The new array has one less element, copying all except the last.
❓ Predict Output
intermediate
2:00remaining
What happens if we try to delete the last element from an empty array?
Given this C# code snippet, what will happen when it runs?
DSA C#
int[] arr = {};
int n = arr.Length;
int[] newArr = new int[n - 1];
for (int i = 0; i < n - 1; i++)
{
    newArr[i] = arr[i];
}
Console.WriteLine(newArr.Length);
A0
BThrows ArgumentOutOfRangeException
CThrows System.IndexOutOfRangeException
DThrows OverflowException
Attempts:
2 left
💡 Hint
Array size cannot be negative.
🔧 Debug
advanced
2:00remaining
Why does this code throw an exception when deleting the last element?
Analyze the code below. It aims to delete the last element from an array but throws an exception. What is the cause?
DSA C#
int[] arr = {1, 2, 3};
int n = arr.Length;
int[] newArr = new int[n];
for (int i = 0; i < n - 1; i++)
{
    newArr[i] = arr[i];
}
Console.WriteLine(newArr[n - 1]);
ANo exception; output is 0 because last element is default int value
BThe newArr size is not reduced, so last element is default 0, no exception
CAccessing newArr[n - 1] which is uninitialized causes NullReferenceException
DArrayIndexOutOfBoundsException due to loop condition
Attempts:
2 left
💡 Hint
Check array size and initialization of elements.
🧠 Conceptual
advanced
1:00remaining
What is the time complexity of deleting the last element from a fixed-size array by copying?
If you delete the last element from an array by creating a new array and copying all elements except the last, what is the time complexity?
AO(n) - linear time
BO(1) - constant time
CO(log n) - logarithmic time
DO(n^2) - quadratic time
Attempts:
2 left
💡 Hint
Consider how many elements are copied.
🚀 Application
expert
2:30remaining
After deleting the last element from this array, what is the printed output?
Given the code below, what will be printed?
DSA C#
int[] arr = {5, 10, 15, 20, 25};
int n = arr.Length;
int[] newArr = new int[n - 1];
Array.Copy(arr, newArr, n - 1);
for (int i = 0; i < newArr.Length; i++)
{
    Console.Write(newArr[i] + " -> ");
}
Console.Write("null");
A5 -> 10 -> 15 -> 25 -> null
B5 -> 10 -> 15 -> 20 -> 25 -> null
C10 -> 15 -> 20 -> 25 -> null
D5 -> 10 -> 15 -> 20 -> null
Attempts:
2 left
💡 Hint
Array.Copy copies specified number of elements from source to destination.

Practice

(1/5)
1. What happens when you delete an element from the end of an array in C#?
easy
A. The array size remains the same but the last element is set to zero.
B. The first element is removed and the array size decreases by one.
C. All elements are shifted to the right.
D. The last element is removed and the array size decreases by one.

Solution

  1. Step 1: Understand array deletion at end

    Deleting at the end means removing the last element only.
  2. Step 2: Effect on array size

    Removing the last element reduces the array size by one.
  3. Final Answer:

    The last element is removed and the array size decreases by one. -> Option D
  4. Quick Check:

    Delete end element = size - 1 [OK]
Hint: Deleting at end removes last element and shrinks array [OK]
Common Mistakes:
  • Thinking the first element is removed
  • Assuming array size stays the same
  • Believing elements shift right
2. Which of the following C# code snippets correctly deletes the last element from an array named arr?
easy
A. arr.Delete(arr.Length);
B. arr.RemoveAt(arr.Length - 1);
C. arr = arr.Take(arr.Length - 1).ToArray();
D. arr.Pop();

Solution

  1. Step 1: Check valid array deletion methods

    Arrays in C# are fixed size; to delete last element, create new array excluding last.
  2. Step 2: Analyze options

    arr = arr.Take(arr.Length - 1).ToArray(); uses LINQ Take to get all but last element and converts to array, which is correct.
  3. Final Answer:

    arr = arr.Take(arr.Length - 1).ToArray(); -> Option C
  4. Quick Check:

    Use Take and ToArray to delete last element [OK]
Hint: Use Take(arr.Length - 1).ToArray() to remove last element [OK]
Common Mistakes:
  • Using RemoveAt on array (only for List)
  • Calling Pop() on array (not supported)
  • Using invalid Delete method
3. What is the output of the following C# code?
int[] arr = {10, 20, 30, 40};
arr = arr.Take(arr.Length - 1).ToArray();
Console.WriteLine(string.Join(", ", arr));
medium
A. 10, 20, 30
B. 10, 20, 30, 40
C. 20, 30, 40
D. 10, 20, 40

Solution

  1. Step 1: Understand the Take method

    arr.Take(arr.Length - 1) takes first 3 elements: 10, 20, 30.
  2. Step 2: Convert to array and print

    After conversion, arr has 3 elements: 10, 20, 30, printed with commas.
  3. Final Answer:

    10, 20, 30 -> Option A
  4. Quick Check:

    Take length-1 elements = 10, 20, 30 [OK]
Hint: Take length-1 elements to remove last [OK]
Common Mistakes:
  • Printing original array without deletion
  • Confusing which elements are taken
  • Assuming last element remains
4. Identify the error in this C# code that tries to delete the last element of an array:
int[] arr = {1, 2, 3, 4};
arr.RemoveAt(arr.Length - 1);
medium
A. Arrays do not have RemoveAt method; use List instead.
B. RemoveAt index should be 0, not arr.Length - 1.
C. RemoveAt removes first element, not last.
D. RemoveAt requires two parameters.

Solution

  1. Step 1: Check array methods

    Arrays in C# do not support RemoveAt; it's a List method.
  2. Step 2: Correct approach

    To delete last element from array, create new array or use List instead.
  3. Final Answer:

    Arrays do not have RemoveAt method; use List instead. -> Option A
  4. Quick Check:

    RemoveAt not in array, only List [OK]
Hint: RemoveAt works on List, not array [OK]
Common Mistakes:
  • Trying RemoveAt on array
  • Wrong index for RemoveAt
  • Assuming RemoveAt needs two parameters
5. You have an array int[] arr = {5, 10, 15, 20, 25};. Write C# code to delete the last two elements and print the resulting array.
hard
A. arr = arr.Take(2).ToArray(); Console.WriteLine(string.Join(", ", arr));
B. arr = arr.Take(arr.Length - 2).ToArray(); Console.WriteLine(string.Join(", ", arr));
C. arr = arr.Skip(2).ToArray(); Console.WriteLine(string.Join(", ", arr));
D. arr.RemoveRange(arr.Length - 2, 2); Console.WriteLine(string.Join(", ", arr));

Solution

  1. Step 1: Remove last two elements using Take

    Take(arr.Length - 2) keeps first 3 elements: 5, 10, 15.
  2. Step 2: Convert to array and print

    Convert to array and print elements separated by commas.
  3. Final Answer:

    arr = arr.Take(arr.Length - 2).ToArray(); Console.WriteLine(string.Join(", ", arr)); -> Option B
  4. Quick Check:

    Take length-2 elements = 5, 10, 15 [OK]
Hint: Use Take(length - 2) to remove last two elements [OK]
Common Mistakes:
  • Using RemoveRange on array (only for List)
  • Using Skip to remove from start instead of end
  • Taking wrong number of elements
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