Java String Basics

Last Updated :
Discuss
Comments

Question 1

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

  • String str = 'Hello';


  • String str = new String("Hello");

  • String str = String("Hello");

  • 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); 
  • true

  • false

  • Compilation Error


  • Runtime Exception

Question 3

How can you make String mutable in Java?

  • Using StringBuilder or StringBuffer

  • Using final keyword

  • By modifying the existing String object

  • By using String.concat()

Question 4

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

  • It removes duplicate Strings from memory

  • It ensures that the String is stored in the String Pool

  • It modifies the original String

  • It converts the String into a StringBuffer

Question 5

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

  • String is faster than StringBuilder


  • String is synchronized

  • String is immutable and therefore thread-safe

  • String can be modified in place


Question 6

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


  • ""

  • null

  • " "

  • undefined

Question 7

Why are strings immutable in Java?

  • To enable multiple inheritance

  • To avoid garbage collection

  • For security and thread safety

  • All of the above

Question 8

Which operator is used for string concatenation?


  • +

  • &

  • .

  • *

Question 9

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


  • "Hello" + "World"

  • "Hello".concat("World")

  • Both a and b

  • Hello + World

Question 10

What happens when you concatenate two strings in Java?

  • A new object is created

  • The original string is changed

  • Compile-time error

  • JVM crashes

There are 10 questions to complete.

Take a part in the ongoing discussion