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
- 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?
Anagrams means if two String have same characters but in different order. For example: Angel and Angel are anagrams There are many ways to do it, some of them are:
- Using for loop
- Using recursion
- Using StringBuffer
3. How to check if two Strings are anagram in java?
There are many ways to check if Strings are anagrams. Some of them are:
- Using String methods
- Using array.sort
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
- Using indexOf and lastIndexOf methods of String
- By Using ascii value of 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.
- Create a new String with str3= str1 + str1
- Check if str3 contains str2 or not.
- if str3 contains str2 then str2 is rotation of str1 else it is not
6. Write a java program to find duplicate characters in String in java?
- Create a HashMap and character of String will be inserted as key and its count as value.
- If Hashamap already contains char,increase its count by 1, else put char in HashMap.
- 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
- Using indexOf and lastIndexOf methods.
8. Find all substrings of String in java?
For example: If input is “abb” then output should be “a”, “b”,”b”, “ab”, “bb”, “abb”
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?
String str1=new String("Java2Blog");
String str2=new String("Java2Blog");
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?
String str1= "java2blog";
String str2= "java2blog";
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?
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.
|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?
String str1= new String("java2blog");
String str2= new String("java2blog");
Three objects will be created here, two in heap memory and one in String constant pool.
You may also like:
- Core java interview questions
- Java Collections interview questions
- OOPs interview questions in java
- Java Multithreading interview questions
- Exceptional handling interview questions in java
- Java Serialization interview questions in java
- Method overloading and overriding interview questions
- web services interview questions
- restful web services interview questions
- Data structure and algorithm Interview Questions
- Spring interview questions
- Hibernate interview questions