Tuesday, May 11, 2021

Java String Interview Questions and Answers

1) What is String in Java? Is String is data type?
String in Java is not a primitive data type like int, long or double. The string is a class or in more simple term a user-defined type. This is confusing for someone who comes from a C background. The string is defined in java.lang package and wrappers its content in a character array. String provides equals() method to compare two String and provides various other methods to operate on String like toUpperCase() to convert String into upper case, replace() to replace String contentssubstring() to get substring, split() to split long String into multiple String.

Java string interview questions

2) Why is String final in Java?
The string is final by design in Java, some of the points which make sense why String is final is Security, optimization and to maintain a pool of String in Java. 

3) What is the difference between String and StringBuffer in Java?
This is probably the most common question on String I have seen in Java interviews. Though String and StringBuffer are two different class they are used in the context of concatenating two Strings, Since String is immutable in Java every operation which changes String produces new String, which can be avoided by using StringBuffer.

4) What is the difference in String on C and Java?
If you have mentioned C in your resume, then you are likely to face this String interview question. Well, C String and Java String are completely different from each other, C String is a null-terminated character array while String in Java is an Object. Also, String is more feature rich in Java than C.

5) Why char array is better than String for storing password?
This String interview question is debatable and you might not agree with interviewer but this is also a chance to show that how deep and differently you can think of. One of the reasons which people give Why you should store a password in char array over String is related to immutability since it's not possible to erase contents of String but you can erase contents of a char array. See Why char array preferred over String for a password for a complete discussion.

6) How do you compare two String in Java?
This is another common String interview question which appears on fresher level interviews. There are multiple ways to compare two String like equals() method, equalsIgnoreCase() etc, You can also see 4 ways to compare String in Java for more examples. The main thing which interviewer checks is that whether candidate mentioned equality operator or not "==", comparing String with equality operator is a common mistake which works in some case and doesn't work in other. next String interview question is follow-up up of this.

7) Can we compare String using == operator? What is the risk?
As discussed in previous String question, You can compare String using equality operator but that is not suggested or advised because equality operator is used to compare primitives and equals() method should be used to compare objects. As we have seen in the pitfall of autoboxing in Java that how equality operator can cause a subtle issue while comparing primitive to Object, anyway String is free from that issue because it doesn't have a corresponding primitive type and not participate in autoboxing.
Almost all the time comparing String means comparing contents of String i.e. characters and equals() method is used to perform character-based comparison. equals() return true if two String points to the same object or two String has same contents while == operator returns true if two String object points to the same object but return false if two different String object contains same contents. That explains why sometimes it works and sometimes it doesn't.

8) How does substring method work in Java?
This is one of the tricky Java question relate to String and until you are familiar with the internals of String class, it's difficult to answer. Substring shares same character array as original String which can create a memory leak if original String is quite big and not required to retain in memory but unintentionally retained by substring which is very small in size and prevents large array from begin claimed during Garbage collection in Java.

Java string interview questions

9) What is String pool in Java?
Another tough Java question asked in String interview. String pool is a special storage area in Java heap, mostly located on PerGen space, to store String literals like "ABC". When Java program creates a new String using String literal, JVM checks for that String in the pool and if String literal is already present in the pool than the same object is returned instead of creating a whole new object. String pool check is only performed when you create String as literal, if you create String using new() operator, a new String object will be created even if String with the same content is available in the pool.

10) What does intern() method do in Java?
As discussed in previous String interview question, String object created by new() operator is by default not added in String pool as opposed to String literal. The intern method allows putting a String object into a pool.

11) Is string thread-safe in Java?
If you are familiar with the concept of immutability and thread-safety you can easily answer this String interview question in Java. Since String is immutable, it is thread-safe and it can be shared between multiple threads without external synchronization.

If you are seriously preparing for Java interviews and do not want to leave any stone unturned, I strongly suggest you go through questions given in Java Programming Interview Exposed, one of the rare book which covers all important topics for Java interviews.
String-based Coding Questions
These questions are mostly based upon Java's implementation of String and you can only answer them well if you have good knowledge of java.lang.String class. But, String is a very general data structure and you will find it in almost all programming and script language e.g. C, C++, C#, Python, Perl or Ruby. That's why I am going to share some more String based coding question, which is not Java specific. You can solve these question in any programming language as they are mostly logic based programming question.
Thrift Savings Plan (TSP) is the federal government's retirement savings and investment plan and offers many of the same types of savings and tax benefits as 401(k) plans used by private employers. TSP is administered by the Federal Retirement Thrift Investment Board and is the largest defined contribution plan in the world. As a defined contribution plan, the retirement income you receive from TSP depends on the contributions made during your working years.

Java string interview questions


1) Write a Java program to reverse String in Java without using any API?
This means you can not use StringBuffer's reverse() method or any of String utility method, all you can have is a character array for reversing contents.

2) Write a Program to check if a String is a palindrome or not? 
For example, a String e.g. "madam" is a palindrome but "book" is not a palindrome. You also need to solve this question without taking any help from Java String API.

3) Write a Java program to check if two String are Anagram or not? 
You need to write method e.g. isAnagram(String first, String second) which will return true if second String is an anagram of the first string. An anagram must contain the same number of characters and exactly the same characters but in different order e.g. top and pot, or army and mary.

4) Write a method in Java to remove any character from String?
For example, you need to write method remove(String word, char remove this), this method should return a String without character, which is asked to remove. you can use indexOf()substring() and similar methods from String class, but your method must handle corner cases e.g. passing null or empty String, String containing just one character etc.

5) Write a method to split a comma separated String in Java? 

6) Write Java program to print all permutations of a String e.g. passing "ABC" will print all permutations like "BCA", "CBA" etc

If you are hungry for more String based coding question, you can also check the Cracking the Coding Interview book, a collection of 189 programming questions and solutions from various programming job interviews of reputed tech companies like Amazon, Google, Facebook, and Microsoft.
See also: MySainsburys is the name of the official Sainsburys employee website. You will need to access the official Oursainsburys login page and about payslips, benefits, work schedules for Sainsburys.
Also, read:




No comments:

Post a Comment

Java String Interview Questions and Answers

1) What is String in Java? Is String is data type? String in Java is not a primitive data type like int, long or double. The string is a ...