Interview #24: Common Java String manipulation programs

String manipulation is a popular topic in QA testing interviews, especially for roles that require automation using Java. Here are some common Java string manipulation programs that are frequently asked in QA interviews:

Disclaimer: For QA-Testing Jobs, WhatsApp us @ 91-6232667387

1. Reverse a String

  • Problem: Write a Java program to reverse a string.
  • Example: Input: "Hello", Output: "olleH"
  • Solution: Use a for loop to iterate from the end of the string, or use StringBuilder.reverse() method.

public static String reverseString(String input) {
return new StringBuilder(input).reverse().toString();
}

2. Check if a String is a Palindrome

  • Problem: Write a Java program to check if a given string is a palindrome.
  • Example: Input: "madam", Output: true
  • Solution: Compare the string with its reversed version.

public static boolean isPalindrome(String input) {
String reversed = new StringBuilder(input).reverse().toString();
return input.equals(reversed);
}

3. Count Occurrences of a Character in a String

  • Problem: Write a Java program to count occurrences of a specific character in a string.
  • Example: Input: "banana", 'a', Output: 3
  • Solution: Loop through the string and count the specific character.

public static int countCharacter(String input, char ch) {
int count = 0;
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == ch) {
count++;
}
}
return count;
}

4. Remove Duplicates from a String

  • Problem: Write a Java program to remove duplicate characters from a string.
  • Example: Input: "programming", Output: "progamin"
  • Solution: Use a LinkedHashSet to maintain character order without duplicates.

public static String removeDuplicates(String input) {
Set<Character> chars = new LinkedHashSet<>();
for (char c : input.toCharArray()) {
chars.add(c);
}
StringBuilder sb = new StringBuilder();
for (char c : chars) {
sb.append(c);
}
return sb.toString();
}

5. Find the First Non-Repeated Character in a String

  • Problem: Write a Java program to find the first non-repeated character in a string.
  • Example: Input: "swiss", Output: 'w'
  • Solution: Use a LinkedHashMap to track character counts, then retrieve the first entry with count 1.

public static Character firstNonRepeatedCharacter(String input) {
Map<Character, Integer> charCountMap = new LinkedHashMap<>();
for (char c : input.toCharArray()) {
charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
}
for (Map.Entry<Character, Integer> entry : charCountMap.entrySet()) {
if (entry.getValue() == 1) {
return entry.getKey();
}
}
return null; // No non-repeated character
}

6. Count Vowels and Consonants in a String

  • Problem: Write a Java program to count the number of vowels and consonants in a string.
  • Example: Input: "hello", Output: Vowels: 2, Consonants: 3
  • Solution: Use loops and conditional checks to separate vowels and consonants.

public static Map<String, Integer> countVowelsAndConsonants(String input) {
int vowels = 0, consonants = 0;
input = input.toLowerCase();
for (char c : input.toCharArray()) {
if (c >= 'a' && c <= 'z') {
if ("aeiou".indexOf(c) != -1) {
vowels++;
} else {
consonants++;
}
}
}
Map<String, Integer> counts = new HashMap<>();
counts.put("Vowels", vowels);
counts.put("Consonants", consonants);
return counts;
}

7. Check if Two Strings are Anagrams

  • Problem: Write a Java program to check if two strings are anagrams (contain the same characters in different orders).
  • Example: Input: "listen", "silent", Output: true
  • Solution: Sort both strings and check if they are equal.

public static boolean areAnagrams(String str1, String str2) {
if (str1.length() != str2.length()) return false;
char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
return Arrays.equals(arr1, arr2);
}

8. Find the Longest Word in a Sentence

  • Problem: Write a Java program to find the longest word in a given sentence.
  • Example: Input: "I love programming", Output: "programming"
  • Solution: Split the string by spaces and compare lengths.

public static String longestWord(String sentence) {
String[] words = sentence.split(" ");
String longest = "";
for (String word : words) {
if (word.length() > longest.length()) {
longest = word;
}
}
return longest;
}

9. Count Words in a String

  • Problem: Write a Java program to count the number of words in a string.
  • Example: Input: "This is a test", Output: 4
  • Solution: Split the string by spaces and return the length of the resulting array.

public static int countWords(String input) {
if (input == null || input.isEmpty()) return 0;
String[] words = input.trim().split("\\s+");
return words.length;
}

10. Remove a Specific Character from a String

  • Problem: Write a Java program to remove all instances of a specific character from a string.
  • Example: Input: "hello", 'l', Output: "heo"
  • Solution: Use replaceAll() or StringBuilder.

public static String removeCharacter(String input, char ch) {
return input.replaceAll(Character.toString(ch), "");
}

11. Check if a String Contains Only Digits

  • Problem: Write a Java program to check if a string contains only digits.
  • Example: Input: "12345", Output: true
  • Solution: Use regular expression or Character.isDigit().

public static boolean containsOnlyDigits(String input) {
return input.matches("\\d+");
}

12. Convert the First Letter of Each Word to Uppercase

  • Problem: Write a Java program to capitalize the first letter of each word in a sentence.
  • Example: Input: "hello world", Output: "Hello World"
  • Solution: Split by spaces and capitalize each word.

public static String capitalizeWords(String input) {
String[] words = input.split(" ");
StringBuilder capitalized = new StringBuilder();
for (String word : words) {
capitalized.append(Character.toUpperCase(word.charAt(0))).append(word.substring(1)).append(" ");
}
return capitalized.toString().trim();
}

These programs cover a range of string manipulations that test your understanding of Java string methods, regular expressions, loops, and data structures, all of which are commonly used in QA automation testing.

Previous: Interview #23: Explain the difference between HTTP methods - GET, POST, PUT, and DELETE

Interview #35: Write a Selenium script that checks for broken links on a webpage.

To check for broken links on a webpage using Selenium, you can follow a systematic approach: Retrieve all anchor (<a>) tags with href ...

Most Popular