Top 35+ Interview Questions with Answers On Java String

String Interview Questions with Answers

In the previous article, I wrote about Java Interview Question on Inner classes. In this article, I am going to share Top 35+ Interview Questions with Answers On Java String frequently asked on core Java Interview. You can expect many questions from this topic in the interview. This topic is one of the favorites of the interviewer.

Q1. What do you understand by String in Java programming?
Ans:
String is a class in java available inside a lang package. It is a final class so it cannot be extended by any of the classes.

Q2. Tell the ways to create the object of the string in java?
Ans:
String object can be created in two different ways:

  • Firstly, with the new operator. String s1 = new String(“xadmin”);
  • Secondly, without a new operator. String s2 = ”xadmin”; here, s1 = s1+xyz;

In this case of string whenever we perform concatenation then always it is going to create a new string literal.

Q3. Is string immutable or mutable in nature and how?
Ans:
String object is immutable in nature, once the string object is created that cannot be changed later on.

Q4. Can we extend the string class in java?
Ans:
It is a final class so it cannot be extended by any of the classes.

Q5. What is a string constant pool?
Ans: The memory space in heap memory specially allocated to store the string objects created using string literals. String uses the special memory location for the reusability of the string literals. The special memory location is called a string constant pool.

Q6. Define the Concept of creating a string object with a new operator?
Ans:
When you are creating a string object with a new operator. Firstly, it will check whether the corresponding literal is available inside the string constant pool or not. If it is not available then, it will create new string literal inside string constant pool and it will create another literal outside the string constant pool (i.e in the heap memory) and that reference will be assigned to the corresponding reference variable. Now, if that string literal is already available inside the constant pool then it will not create new literal inside the string constant pool. It will create one literal outside the string constant pool and that string reference will be assigned to a corresponding reference variable.

Q7. Define the Concept of creating a string object without a new operator:
Ans:
When you are creating a string object without using new operator then firstly, it will check whether string literal is available inside the string constant pool or not. If that literal is not present inside string constant pool then it will create a string literal inside the pool and it will point to that created literally. If a string literal is present inside the string constant pool then it will point to that literal itself and it is not going to create a new literal inside or outside the string constant pool.

Q8. What does toUpperCase() and toLowerCase() method does?
Ans:
This String toUpperCase() method converts the string into upper case letter and method toLowerCase() convert the input string to the lower case letter. For example

String s1 = “Xadmin”;
System.out.println(s1.toUpperCase());
 // output- XADMINSystem.out.println(s1.toLowerCase ());
 // output- xadminSystem.out.println(s1); 
// output- Xadmin (there is no change in orginal string value)

Q9. What does trim() method do of string class?
Ans:
This string method removes white spaces before the string and after the string.
Sample code

String s1= Xadmin;
System.out.println(s1); // Xadmin
System.out.println(s1.trim()); //Xadmin

Q10. What is startsWith() and endsWith() method of string?
Ans:
This method startsWith() check if a string starts with the particular string letter or not. It returns true if the string starts with the given string and return false if not. EndsWith() method checks if a string ends with the given string letter or not. It returns true if the string ends with the given string letter and returns false if not. For example

String s1 = “Xadmin”;
System.out.println(s1.startsWith(“Xa”)); // true
System.out.println(s1.endsWith(“n”)); // true

Q11. What do the charAt() method of String class in java?
Ans:
This method of string class returns a character at the specified index value. For example:

String s1=”Xadmin”;
System.out.println(s1.charAt(0)); // X
System.out.println(s1.charAt(3)); // d

Q12. What is String Buffer in java?
Ans:
It is a class which is available inside java.lang package, it is also a final class as a string class so, it cannot be extended by any of its subclasses. We cannot write any new implementation of the string buffer class, unlike the string class.

Q13. Does String buffer uses string constant pool to store?
Ans: String buffer doesn’t use any new memory like string constant pool concept. It uses the capacity concept to manage the string literals.

Q14. A string buffer is mutable or immutable in nature?
Ans: String buffer object is mutable in nature i.e if you try to perform any changes then it will be done in that specific memory location.

Q15. What is the initial capacity of the string buffer?
Ans: Initial capacity of StringBuffer is 16.
Capacity increases by = ( initial capacity +1) x 2. i.e (16+1)x2=34;
If the length increase then the capacity of StringBuffer then StringBuffer capacity get increased automatically by (initial capacity +1) x 2.

Q16. In how many ways to create the string buffer object?
Ans: StringBuffer object can be created only by the new operator. Like StringBuffer sb = new StringBuffer(“xadmin”);

Q17. Is string Buffer synchronized in nature?
Ans: Yes, Almost all the member of the StringBuffer is completely synchronized so multiply thread can’t be accessible at the same time.

Q18. What do know about string builder?
Ans: In Java, StringBuilder is a final class and is used to create a mutable (modifiable) string. The StringBuilder class in java is the same as StringBuffer class except that it is non-synchronized. It is introduced in JDK 1.5.

Q19. Which is the final class in these three classes – String, StringBuffer and StringBuilder?
Ans: All three are final. (Interviewer will ask this type of questions to confuse you, so be clear with your concepts and reply confidently).

Q20. Tell the different constructor of the string builder class?
Ans: The constructors of the string builder are  as follows:

  1. StringBuilder(): It will create an empty string Builder with the initial capacity of 16.
  2. StringBuilder(String str): It will create a string Builder with the specified provided string.
  3. StringBuilder(int length): This will create an empty string Builder with the specified capacity as length.

Q21. Why StringBuffer and StringBuilder classes are introduced in java when there already exist String class to represent the set of characters?
Ans: As we know, The objects of String class are immutable in nature. i.e you can’t modify them once they are created. If you try to modify them, a new object will be created with modified content. This may cause memory and performance issues if you are performing lots of string modifications in your code. To overcome these issues, StringBuffer and StringBuilder classes are introduced in java.

Q22. How many objects will be created in the following code and where they will be stored in the memory?
Ans:

String s1 = "abc"; 
String s2 = "abc";

For the above code, Only one object will be created and this object will be stored in the string constant pool.

Q23. Can we create mutable string objects?
Ans:
Yes, we can create, with the help of StringBuilder and StringBuffer.

Q24.What is the difference between “==” and equals() method of string objects and which is preferable to compare the two string objects?
Ans: “==” operator, it checks the only reference of two objects are equal or not. Equals() method will compare two string objects based on their content. I prefer equals() method because it provides a more logical comparison of two string objects as it compares the content of the object. If you use “==” operator, it checks only references of two objects are equal or not. It may not be suitable in all situations. So, use equals() method to compare two string objects.

Q25. How do you convert given the string to char array?
Ans: We can convert a given string to char array by using toCharArray() method.

Q26. How many objects will be created in the following code and where they will be stored?
Ans:

String s1 = new String("abc");
String s2 = "abc";

Here, two string objects will be created. An object created using new operator(s1) will be stored in the heap memory. The object created using a string literal(s2) is stored in the string constant pool.

Q27. What do you understand by string intern in java?
Ans:
String object inside the string constant pool is called a String Intern. In this, you can create an exact copy of heap memory string object in string constant pool. intern() method is used for interning.

Q28 What is interning in Java?
Ans:
This process of creating an exact copy of a heap memory string object in the string constant pool is called interning. An intern() method is used for interning.

Q29. Difference between java string and C++ string?
Ans: In C++, a string is typically just an array of (or a pointer to) chars, terminated with a NULL (\0) character. You can process a string by indexing also as you would process any array.
But in Java, strings are not arrays. Java strings are objects of type java.lang.String so You cannot process them by indexing.

Q30. Tell the reason, why strings have been made immutable in java?
Ans:
a) In Java, Immutable strings increase security. As they can’t be modified once they are created, so we can use them to store sensitive data like username, password, etc.

b) Immutable strings are thread-safe. So, we can use them in a multi-threaded code without synchronization.

c) String objects in java are used in the loading of the class. If strings are mutable, it is possible that the wrong class is being loaded as mutable objects are modifiable.

Q31. Can you tell how many objects will be created for the below code:

String str = "abc";
String strs = new String("abc");

Ans:
In the above code, two objects are created. The first object created using new operator is stored in the heap memory (strs).
The second object created using String literal str is stored in the string constant pool.

Q32. Can you tell how many objects will be created for the below code:

String str = "abc";
String strs = "abc";

Ans: In the above code, only one object is created. Here string str will create a new object in String constant pool, and String strs will create a reference to the String str.

Q33. Can you tell how many objects will be created for the below code:

String str = new String("abc");
String strs = new String("abc");

Ans:  In the above code, three objects are created. For the first statement(str1) two objects are created one in String constant pool and other in the heap memory.
But for the second statement(strs), one new object is created in heap memory but no new object is created in string constant pool as it is already present in string constant pool

Q34. Do you know to create an immutable class in java?
Ans: Yes I know, to create immutable class in java by implementing the following points:

1. Make your java class as final so this class can’t be extended by any other class. i.e can’t be inherited

2. Make all fields of the class as private so that no one can access them from outside the class.

3. Do not write setter methods for the variables of the class.

4. Declare all mutable fields as final so that it’s value can’t be changed further.

Q35. Is it possible to use string for switch statement?
Ans: Yes, From JDK 7, we can use string as a switch condition. Before version 6, we can not use string as a switch condition.

// java 7 only!
switch (str1.toUpperCase()) {
      case "A":
           value = 10;
           break;
      case "B":
           value = 20;
           break;
}

Q36. Can you convert string to int?
Ans: Yes, by using
int x = Integer.parseInt(“20”);

Conclusion:
This article will help you with Top 35+ Interview Questions with Answers On Java String to the freshers and as well as to the experienced candidates. Read the article carefully, I believe this article is going to help you a lot to tackle the question fired by the interviewer. Stay tuned!  for the more interview question. Happy Learning! All the best!