Hint: Count words starting with prefix by checking each word [OK]
Common Mistakes:
Counting only exact matches
Including words that contain prefix but don't start with it
Off-by-one counting errors
4. The following code is intended to count words starting with a prefix but gives wrong output. What is the error?
string[] words = {"cat", "car", "dog", "cart"};
string prefix = "car";
int count = 0;
for (int i = 0; i <= words.Length; i++) {
if (words[i].StartsWith(prefix)) {
count++;
}
}
Console.WriteLine(count);
medium
A. The loop condition should be i < words.Length instead of <=.
B. The prefix variable is incorrect.
C. The StartsWith method is used wrongly.
D. The count variable should be initialized inside the loop.
Solution
Step 1: Analyze the for loop condition
The loop runs from i = 0 to i <= words.Length, which causes an index out of range error because valid indices are 0 to words.Length - 1.
Step 2: Correct the loop condition
Change condition to i < words.Length to avoid accessing invalid index.
Final Answer:
The loop condition should be i < words.Length instead of <=. -> Option A
Quick Check:
Loop index must be less than array length [OK]
Hint: Use i < array.Length to avoid index errors in loops [OK]
Common Mistakes:
Using <= instead of < in loop condition
Reinitializing count inside loop
Misusing StartsWith method
5. Given a list of words ["tree", "trie", "trigger", "trip", "try", "matrix"] and prefix "tri", which C# code snippet correctly counts the words starting with the prefix and prints the count?
hard
A. int count = 0; foreach(var word in words) { if(word.Contains(prefix)) count++; } Console.WriteLine(count);
B. int count = words.Count(word => word.StartsWith(prefix)); Console.WriteLine(count);
C. int count = 0; for(int i=0; i<=words.Length; i++) { if(words[i].StartsWith(prefix)) count++; } Console.WriteLine(count);
D. int count = words.Length; Console.WriteLine(count);
Solution
Step 1: Understand the requirement
We need to count words starting exactly with prefix "tri".
Step 2: Evaluate each option
int count = words.Count(word => word.StartsWith(prefix)); Console.WriteLine(count); uses LINQ Count with StartsWith correctly. int count = 0; foreach(var word in words) { if(word.Contains(prefix)) count++; } Console.WriteLine(count); uses Contains which counts words containing prefix anywhere. int count = 0; for(int i=0; i<=words.Length; i++) { if(words[i].StartsWith(prefix)) count++; } Console.WriteLine(count); has loop with <= causing index error. int count = words.Length; Console.WriteLine(count); counts all words ignoring prefix.
Final Answer:
int count = words.Count(word => word.StartsWith(prefix)); Console.WriteLine(count); -> Option B
Quick Check:
Use LINQ Count with StartsWith for prefix count [OK]
Hint: Use LINQ Count with StartsWith for concise prefix counting [OK]
Common Mistakes:
Using Contains instead of StartsWith
Loop index out of range errors
Counting all words without prefix check
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.