📖 Scenario: You are working on a simple text analysis tool. You want to count how many words in a list start with a certain prefix. This helps in searching and filtering words quickly.
🎯 Goal: Build a Java program that counts how many words in a given list start with a specific prefix.
📋 What You'll Learn
Create a list of words with exact values
Create a string variable for the prefix
Use a loop to count words starting with the prefix
Print the count of words with the prefix
💡 Why This Matters
🌍 Real World
Counting words with a specific prefix is useful in search engines, autocomplete features, and text analysis tools.
💼 Career
This skill helps in roles involving data processing, software development, and building search or filtering features.
Progress0 / 4 steps
1
Create the list of words
Create a List<String> called words with these exact entries: "apple", "application", "banana", "appetite", "grape"
DSA Java
Hint
Use List.of(...) to create an immutable list with the exact words.
2
Create the prefix variable
Create a String variable called prefix and set it to "app"
DSA Java
Hint
Use String prefix = "app"; to create the prefix variable.
3
Count words starting with the prefix
Create an int variable called count and set it to 0. Use a for loop with variable word to iterate over words. Inside the loop, if word.startsWith(prefix) is true, increase count by 1.
DSA Java
Hint
Use for (String word : words) to loop through the list and word.startsWith(prefix) to check the prefix.
4
Print the count
Use System.out.println to print the value of count
DSA Java
Hint
Use System.out.println(count); to show the final count.
Practice
(1/5)
1. What does the problem "Count Words with Given Prefix" ask you to do?
easy
A. Count how many words in a list start with a specific prefix
B. Find the longest word in a list
C. Count the total number of characters in all words
D. Sort the words alphabetically
Solution
Step 1: Understand the prefix concept
The prefix is the starting part of a word. We need to check if words start with this prefix.
Step 2: Count matching words
We count all words that begin with the given prefix from the list.
Final Answer:
Count how many words in a list start with a specific prefix -> Option A
Quick Check:
Prefix means start of word = Count matching words [OK]
Hint: Focus on words starting exactly with the prefix [OK]
Common Mistakes:
Counting words containing prefix anywhere, not just start
Confusing prefix with suffix
Counting characters instead of words
2. Which of the following Java code snippets correctly checks if a string word starts with a prefix pref?
easy
A. word.endsWith(pref)
B. word.contains(pref)
C. word.equals(pref)
D. word.startsWith(pref)
Solution
Step 1: Recall Java String methods
Java's startsWith method checks if a string begins with a given prefix.
Step 2: Match method to requirement
Since we want to check the start, word.startsWith(pref) is correct.
Hint: Use startsWith() to check prefix in Java strings [OK]
Common Mistakes:
Using contains() which checks anywhere in string
Using equals() which checks full string equality
Using endsWith() which checks suffix
3. What is the output of the following Java code?
String[] words = {"apple", "app", "application", "banana"};
String prefix = "app";
int count = 0;
for (String w : words) {
if (w.startsWith(prefix)) {
count++;
}
}
System.out.println(count);
medium
A. 3
B. 1
C. 2
D. 4
Solution
Step 1: Check each word against prefix "app"
Words starting with "app" are "apple", "app", and "application" (3 words).
Step 2: Count matching words
Count increments 3 times for these words.
Final Answer:
3 -> Option A
Quick Check:
3 words start with "app" = 3 [OK]
Hint: Count words starting with prefix by checking each word [OK]
Common Mistakes:
Counting only exact matches
Counting words containing prefix anywhere
Off-by-one errors in counting
4. The following code is intended to count words starting with a prefix but has a bug. What is the bug?
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++;
}
}
System.out.println(count);
medium
A. Prefix variable is not initialized
B. startsWith method is used incorrectly
C. Loop condition should be i < words.length, not i <= words.length
D. Array words is empty
Solution
Step 1: Analyze loop condition
The loop runs from i = 0 to i <= words.length, which causes an IndexOutOfBoundsException because last index is words.length - 1.
Step 2: Correct loop condition
Change condition to i < words.length to avoid accessing invalid index.
Final Answer:
Loop condition should be i < words.length, not i <= words.length -> Option C
Quick Check:
Array index max is length-1 = fix loop condition [OK]
Hint: Use i < array.length to avoid index errors [OK]
Common Mistakes:
Using <= instead of < in loop condition
Ignoring array index limits
Misusing startsWith method
5. Given a list of words ["tree", "trie", "trigger", "trip", "trick"] and prefix "tri", which Java code snippet correctly counts the words starting with the prefix using streams?
hard
A. int count = Arrays.stream(words).map(w -> w.startsWith(prefix)).count();
B. long count = Arrays.stream(words).filter(w -> w.startsWith(prefix)).count();
C. long count = Arrays.stream(words).filter(w -> w.contains(prefix)).count();
D. int count = Arrays.stream(words).filter(w -> w.endsWith(prefix)).count();
Solution
Step 1: Use stream filter with startsWith
The filter should keep words starting with the prefix using w.startsWith(prefix).
Step 2: Count filtered words
Use count() to get the number of matching words. The result is a long type.
Final Answer:
long count = Arrays.stream(words).filter(w -> w.startsWith(prefix)).count(); -> Option B
Quick Check:
Filter with startsWith + count() = long count = Arrays.stream(words).filter(w -> w.startsWith(prefix)).count(); [OK]
Hint: Use stream filter with startsWith and count() for prefix count [OK]
Common Mistakes:
Using map instead of filter
Using contains instead of startsWith
Using endsWith instead of startsWith
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.