Banner_Ads

Interview Question-Answers | Tutorial For Beginners


tech4allsa2z:This blog provides you tutorial and most common Interview questions for beginners related to the following technology like java, python.

Top 20 Java String Question and Answers

 


Basic Java String Questions and Answers:

 

1. What is a String in Java?

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

2. How do you create a String object in Java?

   Answer: You can create a String object using either the `String` class constructor or by assigning a string literal enclosed in double quotes.

String Literal:

String_class variable_name =” Value”;

e.g., String name=” Rohan”;

Using new Keyword:

String_Class object=new String_class ();

e.g., String name=new String(“Rohan”);


3. What is the difference between `String` and `StringBuilder` in Java?

   Answer: `String` objects are immutable (unchangeable), while `StringBuilder` allows for mutable sequences of characters, making it more efficient for concatenation of strings.


 4. How do you concatenate strings in Java?

   Answer: Strings can be concatenated in Java using the `+` operator or by using the `concat()` method.


e.g.

String str1 = "Hello, ";

String str2 = "World!";

String result = str1.concat(str2); // result is "Hello, World!"

String result = str1.concat(“India”); // result is "Hello, India"

 

5. How can you compare two strings in Java?

   Answer: You can compare strings in Java using the `equals()` method for content comparison and `==` for reference comparison.


e.g.

String str1 = "Hello";

String str2 = "Hello";

boolean isEqual = str1.equals(str2); // isEqual is true

boolean isEqual = str1.equals(“hello”); // isEqual is false


 6. What is the `StringBuffer` class in Java used for?

   Answer: `StringBuffer` is a thread-safe, mutable sequence of characters used for manipulating strings. It's less efficient than `StringBuilder` but ensures thread safety.


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

   Answer: Use the `toUpperCase()` method to convert a String to uppercase in Java.


e.g.

 String str = "hello";

String upper = str.toUpperCase(); // upper is "HELLO"

 8. Explain the `charAt()` method in Java String.

   Answer: The `charAt()` method returns the character at a specified index within a string. Indexing starts at 0.


e.g.

String str = "Hello";

char ch = str.charAt(1); // ch is 'e'

 9. What is the purpose of the `length()` method in Java String?

   Answer: The `length()` method returns the length (number of characters) of a string.


e.g.

String str = "Hello, World!";

int length = str. length (); // length is 13

 10. How can you check if a string contains a specific character or substring in Java?

   Answer: You can use `contains()` to check if a string contains a specific substring and `indexOf()` to check for a specific character or substring occurrence.


e.g.

String str = "Hello, World!";

boolean containsHello = str.contains("Hello"); 

// containsHello is true

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

   Answer: Use the `String.valueOf()` method or concatenation with an empty string to convert an integer to a String.

12. Explain the `substring()` method in Java.


   Answer: The `substring()` method extracts a portion of a string based on the specified beginning and ending indices.

substring(int beginIndex)

e.g. String str = "Hello, World!";

String substr = str.substring(7); // substr is "World!"


substring(int beginIndex, int endIndex)

e.g. String str = "Hello, World!";

String substr = str.substring(7, 12); // substr is "World"

13. Can you reverse a String in Java? If yes, how?

   Answer: Yes, you can reverse a string in Java by using a loop, `StringBuilder`, or converting it to a character array.

 14. What is the purpose of the `trim()` method in Java String?

  Answer: The `trim()` method removes leading and trailing whitespace from a string.


e.g.

String original = "   Hello, there!   ";

String trimmed = original.trim();

System.out.println("Original String: '" + original + "'");//14 charecter

System.out.println("Trimmed String: '" + trimmed + "'");//12 character


 15. How do you split a String based on a specific delimiter in Java?

   Answer: Use the `split()` method, providing the delimiter as an argument, to split a string into an array of substrings.

 

 16. Explain the concept of immutability in Java String.

   Answer: Immutability means once a string object is created, its value cannot be changed. Any operation on a string returns a new string object.


 17. How can you convert a String to an array of characters in Java?

   Answer: Use the `toCharArray()` method to convert a String to an array of characters.

 

18. How do you check if a String is empty or null in Java?

   Answer: Use `isEmpty()` to check if a string is empty and `null` check to determine if it's null.


 19. Explain the difference between `String`, `StringBuffer`, and `StringBuilder`.

   Answer: `String` is immutable, `StringBuffer` is thread-safe but less efficient, while `StringBuilder` is not thread-safe but more efficient for string manipulation.


20. Can you convert a String to a numeric data type in Java?

   Answer: Yes, you can use methods like `Integer.parseInt()`, `Double.parseDouble()`, etc., to convert a String to a numeric data type in Java.

  


Post a Comment

0 Comments