Top 10 Java String interview questions and answers

1.How many ways we can create string in java ?

There are two ways in which we can create string in java.

1 By String literal

2. By new Keyword

1.By string literal

String name=”placement”;

2.By new keyword

String name=new String(“placement”);

2.What is the difference between the StringBuffer and StringBuilder ?

StringBuffer:

StringBuffer is synchronised that means thread safe.

It is less faster than StringBuilder

StringBuilder:

StringBuilder is non-synchronised that means non-thread safe.

It is more faster than StringBuffer

3.What do you mean by Immutable and mutable objects ?

Immutable objects

Immutable objects simply refers the object whose value can not be changed.

It is like a constant objects ie final

mutable objects

mutable objects simply refers the object whose value can be changed.

It is like not a constant objects

4.How can you convert string into a char array ?

Using toCharArray( ),we can convert string into a sequence of character.

The length of string will same as length of character array.

Example

class StringtocharArray
{
public static void main(String[ ] args)
{
String s=”placement”;
char [ ] charArray=s.toCharArray( );

for(int i=0;i<charArray.length;i++)
{
System.out.print(charArray[i]+” “);
}
}
}

Output:

[ p l a c e m e n t ]

5.What is the difference main difference between the equals( ) method & == operator ?

equals( ) method 

1.equals( ) is method 

2.It compare the string on the basis of original content.

3.Example

public class EqualsMethod
{
public static void main(String[ ] args)
{
String s=”placement”;
String s1=new String(“placement”);
String s2=”placement”;

System.out.println(s.equals(s2));// true
System.out.println(s1.equals(s2)); // false
}
}

Output:

true

false

== operator

1.== is an operator

2.It compare the string on the basis of reference(address)

3.Example

public class DoubleEqualOperator
{
public static void main(String[ ] args)
{
String s=”placement”;
String s1=new String(“placement”);
String s2=new String(“placement”);

System.out.println(s1==s2); //true
System.out.println(s==s2); //false
}
}

Output:

true

false

6.How can you remove all white spaces from String in java ?

We can remove all white space from string in java using replaceAll( ) method.

Example:

public class RemoveWhiteSpace
{
public static void main(String[ ] args)
{
String name=”This is placement preparation”;
//using replaceAll( ) method
name=name.replaceAll(“\\s”,””);
System.out.println(name);
}
}

Output:

Thisisplacementpreparation

7.How many objects will  be created in the given below code ?

String s=”placement”;

String s1=”placement”;

According to,string pool concept

Only one object will create

8.Why string is thread-safe in java ?

Because strings are immutable, which means it value can’t be changed in a program & also it is used  in a multi-threading environment so it is thread-safe.

9.How can you convert String into integer and integer into String in java ?

We can convert string into integer using Integer.ParseInt( ).

int number=Integer.ParseInt(“10”);

We can convert integer into a string using Integer.toString( ).

String s=Integer.toString(number);

10.Is string a primitive or a Derived data type ?

A string is a primitive data type which has state and behaviour.some methods of strings are subString( ) ,indexOf( ) etc.

It is stored in a special region called string pool.

Thanks and Regards

Placement preparation

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *