Top 25 Java String Programming Interview Questions and Answers

In this post, I have collected Java string programming and coding interview questions and answers that may help you to ace your next interview. These questions can be asked either to entry-level developers or experienced developers. Let's dive deep into the topic:

Read Also: Top 50 Java String Interview Questions and Answers

Q1 How to reverse a string in Java? (with or without using built-in functions)

This question is perfect for entry-level Java developers. You can find the solution to reverse a string in Java here. I have also shared how to reverse a string in Java without using a built-in function.

Q2 Write a Java program to find the first non-repeated character in the string?

I have already shared the easiest way to find the first non-repeated character in the string in Java.

Q3 Write a Java program to count the number of words in a given string?

There are many ways to count the number of words in a given string. The simplest of them is given below:

public class CountNumberOfWords {
    public static void main(String args[]) {
        String input = "Alive is Awesome";
        String[] words = input.trim().split(" ");
        System.out.println(words.length);
    }
}


Q4 Write a Java program to count the number of occurrences of a given character in a given string?

The most common way of counting the number of occurrences of a given character in a given string is by using for loop. But in the below example, we are counting the number of occurrences of a given character in a given string without using for loop.

public class CountOccurrencesCharacter {
    public static void main(String args[]) {
        String input = "Alive is Awesome";
        char ch = 'i';
        int count = input.length() - input.replaceAll("i", "").length();
        System.out.println(count);
    }
}


Q5 Write a Java program to find the duplicate characters in a string?

This question can easily be solved using the HashSet class as shown below in the example. The interviewer may ask to solve this question without using Set/HashSet, so, be prepared for that also.

import java.util.HashSet;
import java.util.Set;

public class DuplicateCharacters {
    public static void main(String args[]) {
        String input = "Alive is Awesome";
        HashSet<Character> set = new HashSet<>();
        Set<Character> result = new HashSet<>();
        for(char ch : input.toCharArray())
        {
            if(!set.add(ch))
                result.add(ch);
        }
        System.out.println(result);       
    }
}


Q6 Write a Java program to count the number of occurrences of each character in a given string?

I have already shared the answer to this question in a separate post on how to count the number of occurrences of each character in a given string.

Q7 Write a Java program to remove all whitespaces from a given string.

There are two ways to remove all whitespaces from a given string. One is using built-in Java methods. Another way is without using built-in methods. You can prepare for both ways.

Q8 Write a Java program to check if one given string is an anagram of another given string.

I have shared in a separate post to check if one given string is an anagram of another given string in Java with examples.

Q9 Write a Java program to check if one given string is the rotation of another given string?

I have already shared the code with examples to check if one given string is the rotation of another given string in Java.

Q10 Write a Java program to find the count of characters that are not repeated in the string?

I have already shared the 3 ways to find the count of characters that are not repeated in the string in Java with examples.

Q11 Write a Java program to print the last unique character of the string.

This question is quite similar to Q2, here, we are looking to write a Java program to print the last unique character of the string.

Q12 Write a Java program to count the number of digits in a given string?

I have shared 2 ways to count the number of digits in a given string.

Q13 Check if a given string is an isogram or not.

This question is mainly asked to entry-level Java developers. I have already shared how to check if a given string is an isogram or not.

Q14 Write a Java program to get the first character of the string in Java

This question is mostly asked to entry-level Java developers. You can find the Java program to get the first character of the string in Java here.

Q15 Write a Java program to shuffle a string in Java.

There are two ways to shuffle a string in Java.

Q16 Write a Java program to count the number of commas in a string?

I have already shared 2 ways to count the number of commas in a string in Java.

Q17 Write a Java program to capitalize the first letter of a string in Java.

I have already shared the two ways to capitalize the first letter of a string in Java.

Q18 Write a Java program to find duplicate words in a string?

I shared 3 ways to find duplicate words in a string in Java.

Q19 Write a Java program to check if a string is a Pangram.

I already shared the Java program to check if a string is a Pangram.

Q20 Write a Java program to remove the first and last characters from the string.

You can find the Java program to remove the first and last characters from the string here.

Q21 Write a Java program to check if a string contains special characters.

I shared the Java program to check if a string contains special characters or not.

Q22 Write a Java program to find all substrings of a string in Java.

You can find the solution of the Java program to find all substrings of a string here.

Q23 Check if a string is an anagram in Java.

You can find the answer here.

Q24 Write a Java program to determine if a string has all unique characters.

You can find the Java program to determine if a string has all unique characters here.

Q25 Write a Java program to find the permutation and combination of a given string.

This is one of the most important questions and should be on your to-do list before appearing for the interview. Permutation and Combination Java program you can find it here.

That's all for today. Please mention in the comments if you face any other string programming question in your interview which is not present in the above list.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry