Interview Q&A / Java

INTERVIEW Q&A · 72 QUESTIONS

Java interview questions

Collections, the JVM, strings, exceptions and threads, the backbone of most campus placement rounds.

  1. What is the difference between == and equals() in Java, and what is the String pool?

    == checks whether two references point to the same object, while equals() checks whether two objects have the same content (if the class overrides it, as String does). String literals are stored once in the String pool, which is why two identical literals can be == but new String("x") cannot.

    BeginnerIn-depth3 min read
  2. What is the difference between String, StringBuilder and StringBuffer?

    String is immutable, so every change creates a new object. StringBuilder and StringBuffer are mutable and change the same object in place. StringBuffer's methods are synchronized (thread-safe but slower), while StringBuilder is not synchronized and is the usual choice for building strings in a single thread.

    BeginnerIn-depth3 min read
  3. How many objects does new String("India") create in Java?

    Up to two. The literal "India" goes into the string pool if it is not already there, and new String(...) always creates a separate object on the heap. So two objects the first time, and one if the pool already had the literal.

    BeginnerQuick answer1 min read
  4. Why is the String class immutable in Java?

    Once created, a String cannot change. This makes the string pool safe to share, lets the hash code be cached for fast HashMap use, keeps strings thread-safe, and stops callers from altering values used for security, such as file paths or URLs.

    BeginnerQuick answer1 min read
  5. Why is using + to join strings inside a loop slow in Java?

    Each + creates a brand new String and copies all earlier characters, so n appends cost about O(n squared) in total. A StringBuilder keeps one growing buffer and appends in amortised O(1), making the whole loop O(n).

    BeginnerQuick answer1 min read
  6. Write a Java method that reverses the order of words in a sentence.

    Trim the text, split on one or more spaces, then join the pieces from last to first with a single space. It takes O(n) time and O(n) space for the word array and result.

    BeginnerQuick answerCoding1 min read
  7. Write a Java method to check whether a string is a palindrome, ignoring case and non-letters.

    Use two pointers from both ends, skip anything that is not a letter or digit, compare lowercase characters, and move inward. It runs in O(n) time with O(1) extra space.

    BeginnerQuick answerCoding1 min read
  8. Write a Java method to check whether two lowercase strings are anagrams.

    If the lengths differ the answer is no. Otherwise count each letter of the first string up and each letter of the second down in an int[26], and the strings are anagrams if every count ends at zero. O(n) time, O(1) space.

    BeginnerQuick answerCoding1 min read
  9. What does the intern() method of String do?

    intern() returns the pooled copy of a string. If the pool already holds an equal string it returns that one, otherwise it adds this string to the pool and returns it, so interned strings with equal text compare equal with ==.

    IntermediateQuick answer1 min read
  10. Why is a char array recommended over String for storing passwords in Java?

    A String is immutable and may stay in memory until garbage collection, so it cannot be wiped. A char array can be overwritten with blanks right after use, which shrinks the time a password sits in memory where a heap dump could expose it.

    IntermediateQuick answer1 min read