Thursday, February 16, 2023

Frequently Asked Questions and Answers related to the String topic in Java Interviews:


FAQs on String topic



1. What is a String in Java?

Answer: A String is an object in Java that represents a sequence of characters.


2. How do you declare a String variable in Java?

Answer: You can declare a String variable in Java by using the String data type and providing a name for the variable, like this: String name = "John";


3. How do you concatenate two Strings in Java?

Answer: You can concatenate two Strings in Java using the + operator, like this: String fullName = firstName + " " + lastName;


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

Answer: The equals() method compares the values of two objects for equality, while the == operator compares the references of two objects for equality.


5. What is the difference between the String, StringBuilder, and StringBuffer classes in Java?

Answer: The String class is immutable, while the StringBuilder and StringBuffer classes are mutable. StringBuilder is not thread-safe, while StringBuffer is thread-safe.


6. How do you reverse a String in Java?

Answer: You can reverse a String in Java by using the reverse() method of the StringBuilder class, like this: StringBuilder str = new StringBuilder("Hello"); str.reverse();


7. How do you find the length of a String in Java?

Answer: You can find the length of a String in Java by using the length() method, like this: String str = "Hello"; int length = str.length();


8. How do you convert a String to an int in Java?

Answer: You can convert a String to an int in Java by using the parseInt() method of the Integer class, like this: String str = "123"; int num = Integer.parseInt(str);


9. How do you compare two Strings in Java?

Answer: You can compare two Strings in Java by using the equals() method, which compares the values of the Strings, like this: String str1 = "Hello"; String str2 = "Hello"; boolean isEqual = str1.equals(str2);


10. How do you split a String in Java?

Answer: You can split a String in Java by using the split() method, which splits the String into an array of substrings based on a specified delimiter, like this: String str = "apple,banana,orange"; String[] fruits = str.split(",");


11. How do you convert a String to a double in Java?

Answer: You can convert a String to a double in Java by using the parseDouble() method of the Double class, like this: String str = "3.14"; double num = Double.parseDouble(str);


12. How do you check if a String contains a specific substring in Java?

Answer: You can check if a String contains a specific substring in Java by using the contains() method, which returns a boolean value indicating whether or not the substring is present in the String, like this: String str = "Hello, world!"; boolean containsWorld = str.contains("world");


13. How do you convert a String to uppercase in Java?

Answer: You can convert a String to uppercase in Java by using the toUpperCase() method, which returns a new String that contains the original String in all uppercase letters, like this: String str = "hello"; String uppercaseStr = str.toUpperCase();


14. How do you remove whitespace from a String in Java?

Answer: You can remove whitespace from a String in Java by using the trim() method, which returns a new String that contains the original String with leading and trailing whitespace removed, like this: String str = " hello "; String trimmedStr = str.trim();


15. How do you check if two Strings are equal, ignoring case?

Answer: You can check if two Strings are equal, ignoring case, in Java by using the equalsIgnoreCase() method, which compares the values of the Strings while ignoring differences in case, like this: String str1 = "hello"; String str2 = "HELLO"; boolean isEqualIgnoringCase = str1.equalsIgnoreCase(str2);


=========Thank You..!=========

Java language books:

Core JavaData structure algorithmCodding interview

No comments:

Post a Comment

Spring Framework Interview Questions

  Q 1. What is Spring Framework and what are its key features? A: Spring Framework is an open-source Java framework used to build robust and...