Bird
Raised Fist0

What will be the output of this C# code?

medium📝 Predict Output Q5 of Q15
C Sharp (C#) - Collections
What will be the output of this C# code?
int[] numbers = new int[] {1, 2, 3};
Array.Resize(ref numbers, 5);
numbers[3] = 4;
Console.WriteLine(numbers.Length);
A4
B5
C3
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand Array.Resize effect

    Array.Resize changes the array size to 5, extending it from 3.
  2. Step 2: Check Length property

    After resizing, Length reflects new size, which is 5.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    Array length after Resize = 5 [OK]
Quick Trick: Array.Resize changes size; Length updates accordingly [OK]
Common Mistakes:
MISTAKES
  • Assuming array size is fixed without Resize
  • Confusing Length with number of assigned elements
  • Expecting runtime error on Resize

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes