Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort the array in ascending order using Array.Sort.
DSA C#
int[] numbers = {5, 3, 8, 1, 2};
Array.[1](numbers);
foreach (int num in numbers) {
Console.Write(num + " ");
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Array.Reverse instead of Array.Sort.
Trying to sort without calling any method.
✗ Incorrect
Array.Sort sorts the array in ascending order. Reverse would reverse the array, Clear would reset elements, and Copy copies elements.
2fill in blank
mediumComplete the code to find if the number 4 exists in the sorted array using Array.BinarySearch.
DSA C#
int[] sortedNumbers = {1, 2, 3, 5, 8};
int index = Array.[1](sortedNumbers, 4);
Console.WriteLine(index >= 0 ? "Found" : "Not Found");Attempts:
3 left
💡 Hint
Common Mistakes
Using IndexOf, which performs linear search.
Using Find which is not a static Array method.
✗ Incorrect
Array.BinarySearch searches a sorted array efficiently (logarithmic time). Find is not a static method on Array, IndexOf performs linear search, and Search does not exist.
3fill in blank
hardFix the error in the code to correctly sort the list of strings alphabetically.
DSA C#
List<string> fruits = new List<string> {"banana", "apple", "cherry"};
fruits.[1]();
foreach (string fruit in fruits) {
Console.Write(fruit + " ");
}Attempts:
3 left
💡 Hint
Common Mistakes
Using OrderBy without assigning the result back.
Using non-existent methods like Arrange or SortBy.
✗ Incorrect
List has a Sort() method to sort elements in place. OrderBy is a LINQ method that returns a new sequence, Arrange and SortBy do not exist.
4fill in blank
hardFill both blanks to create a dictionary that maps each word to its length.
DSA C#
string[] words = {"cat", "elephant", "dog", "lion"};
var wordLengths = words.ToDictionary(word => [1], word => word.[2]);Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word.Length' for BLANK_1, which would make keys the lengths instead of words.
Using Count which is not a property of string.
✗ Incorrect
The key selector is the word itself, so 'word'. The value selector is the length property, 'Length'. 'word.Length' does not fit the blank format, as the blank expects a simple identifier.
5fill in blank
hardFill all three blanks to create a dictionary of words and their lengths, but only include words longer than 3 characters.
DSA C#
string[] words = {"cat", "elephant", "dog", "lion"};
var filtered = words.Where(word => word.[1] > [2]);
var wordLengths = filtered.ToDictionary([3] => [3], [3] => [3].Length);Attempts:
3 left
💡 Hint
Common Mistakes
Using Count instead of Length for strings.
Using a number other than 3 for filtering.
✗ Incorrect
The Where clause filters words with Length > 3. The ToDictionary uses 'word' as the key selector and value selector uses word.Length.
