Java String Interview Questions & Programs (With Examples)
String questions are one of the most important topics in Java interviews.
If you're preparing for Java interviews, String-based questions are very common. They help interviewers test your logic, problem-solving, and understanding of core Java concepts.
Written by Shivkumar Udas – Engineering student sharing practical Java guides for beginners.
💡 Why String Questions Matter
Strings are used in almost every Java application, so interviewers often ask String-related questions.
These are some of the most frequently asked Java String interview questions.
Optimization Tip: In many cases, use StringBuilder instead of String for better performance.
1. Reverse a String in Java
String str = "Java";
StringBuilder sb = new StringBuilder(str);
System.out.println(sb.reverse());
Time Complexity: O(n)
Interview Variation: Reverse without using built-in methods.
2. Check Palindrome String
String str = "madam";
StringBuilder sb = new StringBuilder(str);
if(str.equals(sb.reverse().toString())) {
System.out.println("Palindrome");
}
Time Complexity: O(n)
3. Count Vowels and Consonants
String str = "Java Programming";
int vowels = 0, consonants = 0;
for(char c : str.toLowerCase().toCharArray()) {
if(c >= 'a' && c <= 'z') {
if("aeiou".indexOf(c) != -1)
vowels++;
else
consonants++;
}
}
System.out.println("Vowels: " + vowels);
System.out.println("Consonants: " + consonants);
Time Complexity: O(n)
4. Check Anagram Strings
import java.util.Arrays;
String str1 = "listen";
String str2 = "silent";
char[] a = str1.toCharArray();
char[] b = str2.toCharArray();
Arrays.sort(a);
Arrays.sort(b);
if(Arrays.equals(a, b)) {
System.out.println("Anagram");
}
Time Complexity: O(n log n)
5. Remove White Spaces
String str = "Java Programming";
str = str.replaceAll("\\s", "");
System.out.println(str);
Time Complexity: O(n)
6. Find Duplicate Characters
String str = "programming";
for(int i = 0; i < str.length(); i++) {
for(int j = i + 1; j < str.length(); j++) {
if(str.charAt(i) == str.charAt(j)) {
System.out.println(str.charAt(j));
break;
}
}
}
Time Complexity: O(n²)
7. Check if String Contains Only Digits
String str = "12345";
boolean isDigit = str.matches("\\d+");
System.out.println(isDigit);
Time Complexity: O(n)
8. Convert String to Integer
String str = "123";
int num = Integer.parseInt(str);
System.out.println(num);
Time Complexity: O(n)
9. Count Frequency of Characters
String str = "java";
for(char c : str.toCharArray()) {
int count = 0;
for(char ch : str.toCharArray()) {
if(c == ch) count++;
}
System.out.println(c + " : " + count);
}
Time Complexity: O(n²)
10. Remove Duplicate Characters
String str = "programming";
String result = "";
for(char c : str.toCharArray()) {
if(result.indexOf(c) == -1) {
result += c;
}
}
System.out.println(result);
Time Complexity: O(n²)
📌 Most Important String Questions
- Reverse String
- Palindrome
- Anagram
- Duplicate Characters
- Frequency Count
🎯 Interview Tip
Focus on writing logic clearly. Many interviewers ask you to optimize String programs.
Understanding String methods like length(), charAt(), and equals() is very important.
🧪 Practice Task
Try solving these programs without looking at the code and test with different inputs.
🔗 Related Guides
🎯 Conclusion
String questions are very important for Java interviews and help improve your logical thinking.
Practice regularly and try to optimize your solutions.
Post a Comment