Index 3 is outside the array bounds, causing an exception.
Final Answer:
IndexOutOfRangeException -> Option B
Quick Check:
Accessing arr[3] = IndexOutOfRangeException [OK]
Hint: Indexes must be less than array length [OK]
Common Mistakes:
Expecting last element value instead of error
Assuming default zero value returned
Confusing index 3 with valid index
4. Consider this code snippet:
int[] data = new int[5];
data[5] = 100;
What is the issue and how to fix it?
medium
A. Syntax error due to array declaration.
B. No issue, code runs fine.
C. IndexOutOfRangeException because index 5 is invalid; fix by using index 4.
D. NullReferenceException because array is not initialized.
Solution
Step 1: Analyze array size and valid indexes
Array size is 5, valid indexes are 0 to 4.
Step 2: Identify invalid index usage
Accessing data[5] is outside bounds, causing IndexOutOfRangeException.
Step 3: Correct the index
Use data[4] to access the last element safely.
Final Answer:
IndexOutOfRangeException because index 5 is invalid; fix by using index 4. -> Option C
Quick Check:
Index 5 invalid, max index 4 [OK]
Hint: Last index is length minus one [OK]
Common Mistakes:
Assuming index 5 is valid for length 5
Confusing syntax error with runtime error
Thinking array is uninitialized
5. You have an array int[] scores = {5, 10, 15};. You want to safely print the element at index i without causing an exception. Which code snippet correctly checks bounds before accessing?
hard
A. if (i >= 0 && i < scores.Length) Console.WriteLine(scores[i]); else Console.WriteLine("Invalid index");
B. if (i > 0 && i <= scores.Length) Console.WriteLine(scores[i]); else Console.WriteLine("Invalid index");
C. if (i >= 1 && i < scores.Length) Console.WriteLine(scores[i]); else Console.WriteLine("Invalid index");
D. if (i > 0 && i < scores.Length - 1) Console.WriteLine(scores[i]); else Console.WriteLine("Invalid index");
Solution
Step 1: Understand valid index range for array
Valid indexes are from 0 up to Length - 1.
Step 2: Check each condition
if (i >= 0 && i < scores.Length) Console.WriteLine(scores[i]); else Console.WriteLine("Invalid index"); correctly checks i >= 0 and i < Length, ensuring safe access.
Step 3: Identify incorrect conditions
Other options exclude index 0 or allow invalid upper bounds.
Final Answer:
if (i >= 0 && i < scores.Length) Console.WriteLine(scores[i]); else Console.WriteLine("Invalid index"); -> Option A
Quick Check:
Bounds check: 0 ≤ i < Length [OK]
Hint: Check index >= 0 and < array length [OK]
Common Mistakes:
Using > 0 instead of >= 0
Allowing index equal to length
Ignoring zero index as valid
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.