Java String Basics

Last Updated :
Discuss
Comments

Question 1

What is the correct way to declare a String in Java?

  • A

    String str = new String('Hello');

  • B

    String str = new String("Hello");

  • C

    String str = String("Hello");

  • D

    String str = Hello;

Question 2

What will be the output of the following code?

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

    true

  • B

    false

  • C

    Compilation Error


  • D

    Runtime Exception

Question 3

How can you make String mutable in Java?

  • A

    Using StringBuilder or StringBuffer

  • B

    Using final keyword

  • C

    By modifying the existing String object

  • D

    By using String.concat()

Question 4

How does the intern() method affect Strings in Java?

  • A

    It removes duplicate Strings from memory

  • B

    It ensures that the String is stored in the String Pool

  • C

    It modifies the original String

  • D

    It converts the String into a StringBuffer

Question 5

What is the advantage of using String over StringBuilder in multi-threaded applications?

  • A

    String is faster than StringBuilder


  • B

    String is synchronized

  • C

    String is immutable and therefore thread-safe

  • D

    String can be modified in place


Question 6

What is the default value of a String variable in Java?


  • A

    ""

  • B

    null

  • C

    " "

  • D

    undefined

Question 7

Why are strings immutable in Java?

  • A

    To enable multiple inheritance

  • B

    To avoid garbage collection

  • C

    For security and thread safety

  • D

    All of the above

Question 8

Which operator is used for string concatenation?


  • A

    +

  • B

    &

  • C

    .

  • D

    *

Question 9

Which of the following is a correct way to concatenate strings?


  • A

    "Hello" + "World"

  • B

    "Hello".concat("World")

  • C

    Both a and b

  • D

    Hello + World

Question 10

What happens when you concatenate two strings in Java?

  • A

    A new object is created

  • B

    The original string is changed

  • C

    Compile-time error

  • D

    JVM crashes

There are 10 questions to complete.

Take a part in the ongoing discussion