Top 25 Java String Programming Interview Questions and Answers | Java Hungry

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:

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 . I have also shared how to .

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

I have already shared the easiest way to .

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 .

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

There are two ways to . 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 .

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 .

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 .

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 .

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

I have shared .

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 .

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 .

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

There are .

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

I have already shared .

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

I have already shared the .

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

I shared 3 ways to .

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

I already shared the .

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

You can find the .

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

I shared the .

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

You can find the solution of the .

Q23 Check if a string is an anagram in Java.

You can find the answer .

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

You can find the Java program to .

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. .

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.

Posting Komentar

Lebih baru Lebih lama