Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the position of the first occurrence of "cat" in the text.
C Sharp (C#)
string text = "The cat is on the roof."; int position = text.[1]("cat"); Console.WriteLine(position);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Contains instead of IndexOf returns a boolean, not a position.
✗ Incorrect
The IndexOf method returns the position of the first occurrence of the specified substring.
2fill in blank
mediumComplete the code to extract the substring "dog" from the text.
C Sharp (C#)
string text = "The dog is barking."; string animal = text.[1](4, 3); Console.WriteLine(animal);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using IndexOf returns an integer, not a substring.
✗ Incorrect
Substring(startIndex, length) extracts a part of the string starting at startIndex with the given length.
3fill in blank
hardFix the error in the code to check if the text contains the word "bird".
C Sharp (C#)
string text = "A bird is flying."; bool hasBird = text.[1]("bird"); Console.WriteLine(hasBird);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using IndexOf returns an integer, which needs comparison to check presence.
✗ Incorrect
Contains returns true if the substring exists in the string, which is suitable for this check.
4fill in blank
hardFill both blanks to extract the substring after the first space in the text.
C Sharp (C#)
string text = "Hello World"; int spaceIndex = text.[1](' '); string afterSpace = text.[2](spaceIndex + 1); Console.WriteLine(afterSpace);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using LastIndexOf instead of IndexOf finds the last space, not the first.
✗ Incorrect
IndexOf finds the first space position; Substring extracts from that position plus one to the end.
5fill in blank
hardFill all three blanks to create a dictionary with words as keys and their lengths as values, only for words longer than 3 characters.
C Sharp (C#)
string[] words = {"apple", "bat", "carrot", "dog"};
var lengths = words.ToDictionary([1] => [1], [2] => [2].Length).Where(kv => kv.Value [3] 3).ToDictionary(kv => kv.Key, kv => kv.Value);
foreach (var item in lengths) Console.WriteLine($"{item.Key}: {item.Value}"); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for key and value selectors causes errors.
Using '<' instead of '>' filters wrong words.
✗ Incorrect
We use 'word' as the key selector and value selector, and filter lengths greater than 3.