Java Questions on Strings

savitry.in
0

 Common interview questions related to String, StringBuffer, and StringBuilder in Java

1. String Questions

  1. What is a String in Java?

    • String is an immutable sequence of characters in Java, represented by the java.lang.String class.

  2. Why is String immutable in Java?

    • For security, thread safety, caching (String Pool), and performance optimization.

  3. What is the String Pool?

    • A special memory area in the heap where Java stores String literals to optimize memory usage.

  4. Difference between String s = "hello" and String s = new String("hello")?

    • String s = "hello" checks the String Pool first; new String("hello") creates a new object in heap memory.

  5. How to compare two Strings in Java?

    • Using equals() (content comparison) or == (reference comparison).

  6. Why should char[] be preferred over String for passwords?

    • Strings are immutable and stay in memory longer, while char[] can be wiped after use.

  7. What are the ways to create an immutable class in Java?

    • Make the class final, fields private and final, and avoid setters.

  8. How does String concatenation work in Java?

    • Using + operator (internally uses StringBuilder in loops) or concat() method.


2. StringBuffer Questions

  1. What is StringBuffer?

    • A mutable, thread-safe (synchronized) class for string manipulation.

  2. Why is StringBuffer thread-safe?

    • Its methods are synchronized.

  3. When should you use StringBuffer instead of StringBuilder?

    • In multithreaded environments where thread safety is required.

  4. Difference between String and StringBuffer?

    • String is immutable, StringBuffer is mutable and thread-safe.

  5. How does StringBuffer improve performance over String?

    • Avoids creating multiple objects during modifications.

  6. What is the default capacity of StringBuffer?

    • 16 characters (if not specified).

  7. How does StringBuffer increase its capacity?

    • New capacity = (current_capacity + 1) * 2.


3. StringBuilder Questions

  1. What is StringBuilder?

    • A mutable, non-thread-safe alternative to StringBuffer for faster performance.

  2. Why is StringBuilder faster than StringBuffer?

    • Because it is not synchronized.

  3. When should you use StringBuilder?

    • In single-threaded environments where performance is critical.

  4. Difference between StringBuffer and StringBuilder?

    • StringBuffer is thread-safe (synchronized), StringBuilder is not.

  5. Which one is better for concatenation in a loop: String, StringBuffer, or StringBuilder?

    • StringBuilder (due to mutability and no synchronization overhead).

  6. Is StringBuilder more memory-efficient than String?

    • Yes, because it avoids creating intermediate String objects.

  7. Can we convert StringBuilder to String?

    • Yes, using toString() method.


Comparison Questions

  1. String vs StringBuffer vs StringBuilder?

    • String: Immutable, thread-safe (due to immutability).

    • StringBuffer: Mutable, thread-safe (synchronized).

    • StringBuilder: Mutable, not thread-safe, faster.

  2. Which is the best choice among String, StringBuffer, and StringBuilder?

    • Depends on use case:

      • Use String for constants.

      • Use StringBuffer for multithreaded scenarios.

      • Use StringBuilder for single-threaded performance-critical cases.

  3. How does Java handle String concatenation with + operator?

    • Uses StringBuilder internally in loops (since Java 5).

  4. Can we use StringBuffer or StringBuilder as keys in HashMap?

    • Not recommended because they are mutable, and hash codes may change.


Code-Based Questions

  1. Predict the output:

    java
    Copy
    Download
    String s1 = "Java";
    String s2 = new String("Java");
    System.out.println(s1 == s2); // false (different references)
  2. What happens here?

    java
    Copy
    Download
    String str = "Hello";
    str.concat(" World");
    System.out.println(str); // "Hello" (Strings are immutable)
  3. Efficient way to reverse a String?

    java
    Copy
    Download
    new StringBuilder(str).reverse().toString();
  4. How many objects are created here?

    java
    Copy
    Download
    String s1 = "A";
    String s2 = "A";
    String s3 = new String("A");
    • Answer: 2 (one in String Pool, one in heap via new).


Advanced Questions

  1. How does intern() method work?

    • Returns a canonical representation of the String from the String Pool.

  2. Why is String popular as a HashMap key?

    • Because it's immutable, ensuring hashcode consistency.

  3. Impact of using + in a loop vs StringBuilder?

    • + in a loop creates multiple intermediate objects, while StringBuilder is efficient.

  4. Can we inherit String class?

    • No, because it's final.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !