• Courses
  • Tutorials
  • Practice
Switch to Dark Mode

Java String Classes

Here are 10 essential multiple-choice questions on Java String Classes, covering key concepts.

Last Updated : Mar 27, 2025
Discuss
Comments

Question 1

Which of the following is immutable in Java?

  • A

    String

  • B

    StringBuffer


  • C

    StringBuilder

  • D

    Both B and C

Question 2

What is the key difference between StringBuffer and StringBuilder?

  • A

    StringBuffer is thread-safe, StringBuilder is not

  • B

    StringBuilder is synchronized, StringBuffer is not

  • C

    StringBuffer is immutable, StringBuilder is mutable

  • D

    StringBuilder consumes more memory than StringBuffer


Question 3

Which of the following will compile successfully?

Java
StringBuffer sb = "Hello";
  • A

    No, it will not compile

  • B

    Yes, it will compile

  • C

    Yes, but with a warning

  • D

    Only if sb is declared as final

Question 4

Which class should be used when multiple threads need to modify the same string?


  • A

    String

  • B

    StringBuffer

  • C

    StringBuilder


  • D

    None of the above

Question 5

What will be the output of the following code?

Java
String s1 = "Java";
String s2 = s1.concat(" Programming");
System.out.println(s1);
  • A

    Java

  • B

    Java Programming


  • C

    Compilation Error

  • D

    Runtime Exception

Question 6

Which of the following methods exists in StringBuffer but not in String?

  • A

    append()

  • B

    concat()

  • C

    charAt()


  • D

    length()

Question 7

What will be the output of the following code?

Java
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb);
  • A

    Hello


  • B

    Hello World

  • C

    World

  • D

    Compilation Error

Question 8

Which is the fastest for string modifications in a single-threaded environment?

  • A

    String

  • B

    StringBuffer

  • C

    StringBuilder

  • D

    All are equally fast

Question 9

What happens if multiple threads try to modify a StringBuilder instance?

  • A

    It will throw a ConcurrentModificationException

  • B

    It may cause unexpected results due to lack of synchronization

  • C

    It will execute sequentially

  • D

    It will behave the same as StringBuffer

Question 10

What will be the output of the following code?

Java
StringBuffer sb1 = new StringBuffer("Java");
StringBuffer sb2 = sb1;
sb1.append(" Programming");
System.out.println(sb2);
  • A

    Java

  • B

    Java Programming

  • C

    Compilation Error

  • D

    Runtime Exception

There are 10 questions to complete.

Take a part in the ongoing discussion