0
0
C Sharp (C#)programming~20 mins

Array methods (Sort, Reverse, IndexOf) in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this C# code using Array.Sort and Array.IndexOf?

Look at the code below. What will be printed to the console?

C Sharp (C#)
using System;

class Program {
    static void Main() {
        int[] numbers = {5, 3, 8, 1, 2};
        Array.Sort(numbers);
        int index = Array.IndexOf(numbers, 3);
        Console.WriteLine(index);
    }
}
A1
B3
C2
D0
Attempts:
2 left
💡 Hint

Remember that Array.Sort arranges the array in ascending order before searching.

Predict Output
intermediate
2:00remaining
What does this code print after reversing the array?

What will be the output of the following C# program?

C Sharp (C#)
using System;

class Program {
    static void Main() {
        string[] fruits = {"apple", "banana", "cherry"};
        Array.Reverse(fruits);
        Console.WriteLine(fruits[1]);
    }
}
Abanana
Bapple
Ccherry
DIndexOutOfRangeException
Attempts:
2 left
💡 Hint

Think about how reversing changes the order of elements.

🔧 Debug
advanced
2:00remaining
Why does this code throw an exception?

Examine the code below. It throws an exception when run. What is the cause?

C Sharp (C#)
using System;

class Program {
    static void Main() {
        int[] values = {10, 20, 30};
        Array.Reverse(values, 1, 5);
        Console.WriteLine(string.Join(",", values));
    }
}
AArgumentOutOfRangeException because the length parameter exceeds array bounds
BNullReferenceException because the array is null
CInvalidOperationException because Reverse cannot be used on int arrays
DNo exception, output is '10,30,20'
Attempts:
2 left
💡 Hint

Check the parameters passed to Array.Reverse carefully.

Predict Output
advanced
2:00remaining
What is the output of this code using Array.IndexOf with a missing element?

What will this program print?

C Sharp (C#)
using System;

class Program {
    static void Main() {
        char[] letters = {'a', 'b', 'c', 'd'};
        int pos = Array.IndexOf(letters, 'z');
        Console.WriteLine(pos);
    }
}
AIndexOutOfRangeException
B-1
C0
D4
Attempts:
2 left
💡 Hint

What does Array.IndexOf return if the item is not found?

🧠 Conceptual
expert
2:00remaining
How many elements are in the array after these operations?

Consider this C# code snippet:

int[] arr = {4, 7, 1, 9};
Array.Sort(arr);
Array.Reverse(arr);
int index = Array.IndexOf(arr, 7);

After running this code, how many elements does arr contain?

A0
B3
C1
D4
Attempts:
2 left
💡 Hint

Sorting and reversing do not change the number of elements in the array.