Array traversal means visiting each element in an array one by one, usually from the first element to the last.
Click to reveal answer
beginner
How do you traverse an array forwards in C#?
Use a for loop starting from index 0 up to the last index, accessing each element by its index.
Click to reveal answer
beginner
What is reverse traversal of an array?
Reverse traversal means visiting array elements starting from the last element down to the first element.
Click to reveal answer
intermediate
Why is it important to know different array traversal patterns?
Different problems need different ways to visit array elements, like forwards, backwards, skipping elements, or jumping by steps.
Click to reveal answer
beginner
Show a simple C# code snippet to traverse an array and print all elements.
for (int i = 0; i < arr.Length; i++) { Console.WriteLine(arr[i]); }
Click to reveal answer
Which index does array traversal usually start with?
A-1
B1
C0
DLength of array
✗ Incorrect
Array indexes in C# start at 0, so traversal usually begins at index 0.
What happens if you traverse an array backwards?
AYou visit elements from last to first
BYou skip every other element
CYou visit elements randomly
DYou only visit the first element
✗ Incorrect
Backward traversal means visiting elements starting from the last index down to the first.
Which loop is commonly used for array traversal in C#?
Aforeach loop
Bwhile loop
Cdo-while loop
Dfor loop
✗ Incorrect
The for loop is commonly used because it gives control over the index.
What is the last index of an array with length 5?
A4
B5
C0
D6
✗ Incorrect
Since indexing starts at 0, the last index is length - 1, which is 4.
Which traversal pattern would you use to print elements in reverse order?
AStart from 0 and go to last index
BStart from last index and go to 0
CSkip every other element
DVisit elements randomly
✗ Incorrect
To print in reverse, start from the last element and move backwards to the first.
Explain how to traverse an array forwards and backwards with examples.
Think about the starting and ending index for each direction.
You got /3 concepts.
Describe why different array traversal patterns are useful in programming.
Consider real-life examples like reading a book front to back or back to front.
You got /4 concepts.
Practice
(1/5)
1. What does the following C# code do? int[] arr = {1, 2, 3, 4}; for (int i = 0; i < arr.Length; i++) { Console.Write(arr[i] + " "); }
easy
A. Prints all elements of the array from start to end
B. Prints all elements of the array in reverse order
C. Prints only the first element of the array
D. Causes a runtime error due to wrong loop condition
Solution
Step 1: Understand the loop range
The loop starts at 0 and runs while i is less than arr.Length (4), so i goes 0,1,2,3.
Step 2: Print elements in order
Each iteration prints arr[i], so it prints 1 2 3 4 in order.
Final Answer:
Prints all elements of the array from start to end -> Option A
Quick Check:
Loop from 0 to length-1 prints all elements [OK]
Hint: Loop from 0 to length-1 prints all elements in order [OK]
Common Mistakes:
Thinking loop prints in reverse
Confusing loop condition with <= instead of <
Assuming only first element prints
2. Which of the following is the correct syntax to traverse an array arr in C# using a foreach loop?
easy
A. foreach (int i in arr) { Console.Write(i); }
B. foreach (arr in int i) { Console.Write(i); }
C. foreach (int arr in i) { Console.Write(i); }
D. foreach (int i; arr) { Console.Write(i); }
Solution
Step 1: Recall foreach syntax
The correct syntax is: foreach (type variable in collection) { ... }
Step 2: Match syntax to options
foreach (int i in arr) { Console.Write(i); } matches correct syntax: foreach (int i in arr) { Console.Write(i); }
Final Answer:
foreach (int i in arr) { Console.Write(i); } -> Option A
Quick Check:
foreach loops use 'variable in collection' syntax [OK]
Hint: Remember foreach uses 'variable in collection' syntax [OK]
Common Mistakes:
Swapping variable and collection order
Using semicolon instead of 'in'
Incorrect variable declaration inside foreach
3. What is the output of the following code?
int[] arr = {10, 20, 30, 40};
for (int i = arr.Length - 1; i >= 0; i--) {
Console.Write(arr[i] + " ");
}
medium
A. 40 30 20
B. 10 20 30 40
C. 40 30 20 10
D. Runtime error due to loop condition
Solution
Step 1: Analyze loop start and end
Loop starts at arr.Length - 1 = 3 and runs while i >= 0, decrementing i each time.
Step 2: Trace printed elements
Prints arr[3]=40, arr[2]=30, arr[1]=20, arr[0]=10 in that order.
Final Answer:
40 30 20 10 -> Option C
Quick Check:
Loop from end to start prints reversed array [OK]
Hint: Loop from length-1 down to 0 prints array in reverse [OK]
Common Mistakes:
Confusing loop direction and printing forward
Off-by-one errors in loop condition
Assuming runtime error due to negative index
4. Find the error in this array traversal code:
int[] arr = {5, 10, 15};
for (int i = 0; i <= arr.Length; i++) {
Console.Write(arr[i] + " ");
}
medium
A. Array declaration is incorrect
B. Variable i should start from 1, not 0
C. Console.Write should be Console.WriteLine
D. Loop condition should be i < arr.Length, not <= arr.Length
Solution
Step 1: Check loop condition
Loop runs while i <= arr.Length, but arr indices go from 0 to arr.Length - 1.
Step 2: Identify out-of-range access
When i == arr.Length (3), arr[3] is accessed causing runtime error (IndexOutOfRangeException).
Final Answer:
Loop condition should be i < arr.Length, not <= arr.Length -> Option D
Quick Check:
Array indices go 0 to length-1, use < not <= [OK]
Hint: Use i < arr.Length to avoid out-of-range errors [OK]
Common Mistakes:
Using <= instead of < in loop condition
Starting loop at 1 instead of 0
Confusing Write and WriteLine methods
5. Given an integer array arr, which code snippet correctly prints only the even-indexed elements (0, 2, 4, ...) of the array?
hard
A. foreach (int i in arr) { if (i % 2 == 0) Console.Write(i + " "); }
B. for (int i = 0; i < arr.Length; i += 2) { Console.Write(arr[i] + " "); }
C. for (int i = 1; i < arr.Length; i += 2) { Console.Write(arr[i] + " "); }
D. for (int i = 0; i < arr.Length; i++) { if (arr[i] % 2 == 0) Console.Write(arr[i] + " "); }
Solution
Step 1: Understand requirement
We want to print elements at even indexes: 0, 2, 4, ...
Step 2: Analyze options
for (int i = 0; i < arr.Length; i += 2) { Console.Write(arr[i] + " "); } increments i by 2 starting at 0, so it accesses even indexes correctly. for (int i = 1; i < arr.Length; i += 2) { Console.Write(arr[i] + " "); } accesses odd indexes. foreach (int i in arr) { if (i % 2 == 0) Console.Write(i + " "); } checks element values for evenness, not indexes. for (int i = 0; i < arr.Length; i++) { if (arr[i] % 2 == 0) Console.Write(arr[i] + " "); } prints elements with even values, not indexes.
Final Answer:
for (int i = 0; i < arr.Length; i += 2) { Console.Write(arr[i] + " "); } -> Option B
Quick Check:
Increment index by 2 from 0 to get even indexes [OK]
Hint: Increment loop index by 2 starting at 0 for even indexes [OK]
Common Mistakes:
Confusing even index with even element value
Starting loop at 1 for even indexes
Using foreach without index tracking
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.