Java String Interview questions and answers

Java String interview questions

In this post, we will see interview questions on java String. String is most important data type which we use in our programs very often.

1. Why String is declared final or immutable in java?

There are various reasons to make String immutable.

  • String pool
  • Thread Safe
  • Security
  • Class Loading
  • Cache hash value

You can refer why String is immutable in java for more details.

2. How to reverse a String in java? Can you write a program without using any java inbuilt methods?

There are many ways to do it, some of them are:

  • Using for loop
  • Using recursion
  • Using StringBuffer
Please refer to the solution at reverse a String in java

3. How to check if two Strings are anagram in java?

Anagrams means if two String have same characters but in different order. For example: Angel and Angel are anagrams
There are many ways to check if Strings are anagrams. Some of them are:

  1. Using String methods
  2. Using array.sort
Please refer to solution at check if two Strings are anagram in java.

4. How to check if String has all unique characters in java?

There are multiple ways to find if String has all unique characters or not.

  • By using [HashSet](https://java2blog.com/how-hashset-works-in-java/ “HashSet”)
  • Using indexOf and [lastIndexOf](https://java2blog.com/java-string-lastindexof-example/ “lastIndexOf”) methods of String
  • By Using ascii value of characters.
Please refer to complete solution at check if String has all unique characters.

5. How to check if one String is rotation of another String in java?

Lets say you need to check whether str1 and str2 is rotation of one another or not.

  1. Create a new String with str3= str1 + str1
  2. Check if str3 contains str2 or not.
  3. if str3 contains str2 then str2 is rotation of str1 else it is not
You can find complete solution at check if one String is rotation of another in java.

6. Write a java program to find duplicate characters in String in java?

  1. Create a HashMap and character of String will be inserted as key and its count as value.
  2. If Hashamap already contains char,increase its count by 1, else put char in HashMap.
  3. If value of Char is more than 1, that means it is duplicate character in that String.

7. Find first non repeated character in String in java?

There are may ways to find it.
Some of them are:

  • Using [LinkedHashMap](https://java2blog.com/linkedhashmap-in-java-with-example/ “LinkedHashMap”)
  • Using indexOf and lastIndexOf methods.
Please find complete solution at find first non repeated character in  a String.

8. Find all substrings of String in java?

Java program to find all substrings of a String.
For example: If input is “abb”  then output should be “a”, “b”,”b”, “ab”, “bb”, “abb”
We will use String class’s subString method to find all subString.
Please refer to complete solution at find all subStrings of String.

9. Find length of String without using any inbuilt method in java?

You can use try catch block for catching StringIndexOutOfBoundException and when this exception aries, you can simply return i(Index at which you will get the exception)
Please refer to complete solution at find length of String without inbuilt methods.

10. Write a java program to print all permutations of String in java?

Take out first character of String and insert into different places of permutations of remaining String recursively. Please find complete solution at how to find all permutations of String in java.

11. What is the difference between creating String as new() and literal?

If you create a String using new operator, it is not interned. It will  create new object in heap memory even if String object already exists with same content.
It will return false as str1 and str2 will point to different object
If you create a String using assignment operator, it goes to the String constant pool and it is interned. If you create another String with same content, both will reference to same object in String constant pool.

It will return true as str1 and str2 will point to same object in String constant pool.

12. How many objects will be created in below code?

Only one object will be created and will be stored in String constant pool.

13. How do you convert String to char array in java?

You can use string’s toCharArray() method to convert String to char array.

14. What is difference between StringBuffer and StringBuilder in java?

Parameter
StringBuffer
StringBuilder
Thread-safe
StringBuffer is thread safe. Two threads can not call methods of StringBuffer simultaneously.
StringBuilder is not thread safe, so two threads can call methods of StringBuilder simultaneously.
Performance
It is less performance efficient as it is thread-safe
It is more performance efficient as it is not thread-safe.

15. How many objects will be created in below code?

Three objects will be created here, two in heap memory and one in String constant pool.

You may also like:

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *