Mostly Asked Java Strings Interview Questions and Answers

Mostly Asked Java Strings Interview Questions and Answers

If you are preparing for a Java interview, it is essential to familiarize yourself with the concept of strings and their commonly asked interview questions. As one of the most widely used data types in Java, strings are important to understand. If you are not familiar with strings in Java, I recommend visiting this article on strings in Java for an introduction.


In this article, we’ll explore the top interview questions and answers on strings in Java. So let's get into the discussion.


     Java Strings Interview Questions:


1. What is a String in Java?

A String is a sequence of characters in Java, which can be represented as an object of the String class. It is an immutable data type, which means that once a string object is created, it cannot be modified.


2. What is the difference between String, StringBuffer, and StringBuilder?

String is an immutable class, whereas StringBuffer and StringBuilder are mutable classes. This means that once a string is created, it cannot be modified, while the content of StringBuffer and StringBuilder can be modified.

StringBuffer is synchronized and hence it is thread-safe, while StringBuilder is not synchronized and hence it is not thread-safe. StringBuilder is faster than StringBuffer because it is not synchronized.


3. Can we use the "==" operator to compare two strings in Java?

No, we cannot use the "==" operator to compare two strings in Java. The "==" operator checks whether the two objects refer to the same memory location. To compare two strings in value, we should use the equals() method.


4. What is the difference between equals() and == in Java?

The equals() method checks whether the content of two objects is equal, while the "==" operator checks whether the two objects refer to the same memory location.


5. How to find the length of a string in Java?

We can find the length of a string in Java using the length() method.

Example:

String str = "Hello World!";
int len = str.length();
System.out.println(len);

Output: 12


6. How to reverse a string in Java?

We can reverse a string in Java using the reverse() method of the StringBuilder class.

Example:

String str = "CodeForHunger";
StringBuilder sb = new StringBuilder(str);
sb.reverse();
System.out.println(sb.toString()); 

Output: regnuHroFedoC


7. How to concatenate two strings in Java?

We can concatenate two strings in Java using the + operator or the concat() method.

Example:

String str1 = "Hello";
String str2 = "World";
String result = str1 + " " + str2;
System.out.println(result); 
Output: Hello World


8. Explain the substring() method in Java.

The substring() method in Java is used to retrieve a substring from a given string. It takes two arguments: the starting index and the ending index of the substring.

Example:

String str = "Hello Java";
String subStr = str.substring(0, 5);
System.out.println(subStr); 
Output: Hello


9. What is the compareTo() method in Java?

The compareTo() method in Java is used to compare two strings lexicographically. It returns a negative integer, zero, or a positive integer depending on whether the first string is less than, equal to, or greater than the second string.

Example:

String str1 = "Hello";
String str2 = "World";
int result = str1.compareTo(str2);
System.out.println(result); 
Output: -15


10. What is the intern() method in Java?

The intern() method in Java is used to return the string object with the same value as the given string, from the string pool. If the given string is already in the pool, a reference to the string in the pool is returned.

Example:

String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");
System.out.println(str1 == str2); // Output: true
System.out.println(str1 == str3); // Output: false
System.out.println(str1 == str3.intern()); //Output: true
Output: true
               false
               true 


11. What is the indexOf() method in Java?

The indexOf() method in Java is used to find the index of the first occurrence of a specified character or substring in a string. If the character or substring is not found, it returns -1.

Example:

String str = "Hello World";
int index = str.indexOf('o');
System.out.println(index); 
Output: 4


12. How to convert a string to uppercase in Java?

We can convert a string to uppercase in Java using the toUpperCase() method.

Example:

String str = "Hello World";
String upper = str.toUpperCase();
System.out.println(upper); 
Output: HELLO WORLD


13. How to split a string in Java?

We can split a string in Java using the split() method. This method takes a regular expression as an argument and returns an array of substrings.

Example:

String str = "Hello World";
String[] arr = str.split(" ");
for (String s : arr) {
    System.out.println(s); 
}
Output: Hello World


14. How to replace a character in a string in Java?

We can replace a character in a string in Java using the replace() method.

Example:

String str = "Hello World";
String result = str.replace('e', 'i');
System.out.println(result); 
Output: Hillo World


15. How to convert a string to an integer in Java?

We can convert a string to an integer in Java using the parseInt() method of the Integer class.

Example:

String str = "123";
int num = Integer.parseInt(str);
System.out.println(num); 
Output: 123


16. Why String is immutable in Java explain it with example?

As we know, when we create a string, it gets stored in the string pool, which makes it immutable. The reason behind this is that string literals are accessed by multiple clients. Therefore, there is a chance that one client's action may affect all other clients. For example, if one client changes its address from Mumbai to Pune, there is a possibility that all the remaining clients address references pointing to that address or object in the string pool may also change. 

Another reason for making strings immutable is that If we allow strings to be changed after they are created, it can create a significant security risk for the application.


In conclusion, it is essential to prepare yourself well before attending a Java interview. Familiarizing yourself with the commonly asked interview questions on strings in Java can give you an edge over other candidates. Remember to practice and gain practical experience in coding to ace your Java interview.


    Related Articles:

FAQs

What is the use of the String class in Java? 

Ans. The string class in Java is used to create and manipulate strings in Java.


What is the difference between StringBuilder and StringBuffer in Java? 

Ans. StringBuilder is not thread-safe, while StringBuffer is thread-safe.


Are strings mutable in Java? 

Ans. No, strings are immutable in Java.


Can we modify a string in Java? 

Ans. No, we cannot modify a string in Java directly because strings are immutable. We need to create a new string to modify the content of a string.


No comments:

Post a Comment

If you have any doubts, please discuss here...👇