Java Strings Interview Questions

    Question 1: JAVA - String Immutability

    Why are Strings immutable in Java?

    Question 2: JAVA - String Object Creation

    Which of the following creates a new String object in the heap?

    Question 3: JAVA - String Concatenation

    What will this code print?

    String s1 = "Hello"; String s2 = "World"; String s3 = s1 + s2; System.out.println(s3);

    Question 4: JAVA - String equals() vs ==

    What will be the output?

    String s1 = "Java"; String s2 = new String("Java"); System.out.println(s1 == s2); System.out.println(s1.equals(s2));

    Question 5: JAVA - String Length

    What will be printed?

    String str = "Java Programming"; System.out.println(str.length());

    Question 6: JAVA - Substring Method

    What will this code print?

    String str = "Interview"; System.out.println(str.substring(0, 5));

    Question 7: JAVA - StringBuilder vs StringBuffer

    Which statement is correct?

    Question 8: JAVA - String Reverse Example

    What will this code print?

    StringBuilder sb = new StringBuilder("Java"); System.out.println(sb.reverse());

    Question 9: JAVA - String Interning

    What does the intern() method of String do?

    Question 10: JAVA - String isEmpty() vs isBlank()

    Which is true about isEmpty() vs isBlank()?