Java String Handling Interview Questions With Answers
Java String Handling Interview Questions With Answers
Answers
String s2 = "Hello";
String s3 = "Hello";
Ans: Only one object will be created in the string constant pool.
14. How many total objects will create in the following code?
String s = new String("Hello");
Ans: A total of two objects will create, one in the heap area and
another in string constant pool.
15. How many total objects will be created in the following
code?
String s1 = new String("Scientech");
String s3 = "Scientech";
String s4 = "Scientech";
Ans: A total of three objects will be created, two in the heap area
and one in string constant pool.
16. Can we use reflection to clear a string object?
Ans: Yes, we can use reflection to clear string object from the
memory but it is not recommended to do.
17. Why string class is declared as final in java?
Ans: String class has been marked as final so that we could not
override the immutable behavior of string class.
String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1.equals(s3));
System.out.println(s1.equals(s4));
System.out.println(s1.equals(args));
System.out.println(s1.equals(null));
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));
Ans: Output:
False because content is the same but the case is different.
True
26. What will be the output of the following program?
public class Test {
String s1 = "Cricket";
String s2 = "Cricket";
System.out.println(s1==s2);
System.out.println(s1==s3);
System.out.println(st1.compareTo(st2));
System.out.println(st1.compareTo(st3));
System.out.println(st3.compareTo(st1));
System.out.println(st2.compareTo(st4));
Output:
-1
String s1 = "Java";
s1.concat("Programming");
System.out.println(s1);
Output:
Java
b)
public class StringConTest {
s1.concat(" Core");
s1 = s1.concat(" Technology");
System.out.println(s1);
}
Output:
Java Technology
c)
public class StringConTest {
System.out.println(s1);
System.out.println(s1.concat(s2));
Output:
50 Text 14
50 Text 14Text2
s.substring(5);
System.out.println(s);
System.out.parental(s2);
echnology
Ans b: A total of 4 objects will create, one in the string constant pool
and three in the heap area.
31. How to check a string is empty or not?
Ans: String class provides isEmpty() method to check the string is
empty or not. If the length of the string is 0, the string is empty and
will return true otherwise false. For example:
public class StringEmptyTest {
Output:
String s2 = "text";
String s3 = s.toUpperCase();
System.out.println(s==s3);
System.out.println(s.equals(s2));
System.out.println(s2.equalsIgnoreCase(s3));
Output:
false
true
true
sb.append("Java");
sb.append("Programming");
Ans: This code will create two reference variables and two string
objects in the memory. The two reference variables are str1 and
str2. The two string objects are: Technology, and TechnologyJava.
46. What will be the output when you compile and execute
the below code?
public class Test {
System.out.println(str2);
}
Ans: On compiling and executing the above code, the value of str2
will be XdZPQR. It is an example of chained methods. In line 1, the
expression will execute from left to right.
First, str1 is concatenated to pqr and so the value is xyzpqr. Then,
the value is converted into uppercase and the value becomes
XYZPQR. At last, the character ‘Y’ is replaced with d and the value
becomes XdZPQR.
47. What is immutable class in Java?
Ans: An immutable class in Java is a class whose state of an object
cannot be changed or modified after it is created. All the wrapper
classes in java like Integer, Long, Short, Byte, Boolean, Float, and
Double are immutable class.
48. What is a mutable class and mutable object in Java?
Ans: A mutable class in Java is a class whose objects are mutable
(i.e., modifiable). If the state of an object can be changed or
mutated after it has been created, it is called mutable object in java.
Java.util.Date, StringBuffer and StringBuilder are the examples of
mutable classes in Java.
49. How to create/make an Immutable Class in Java?
Ans: To get an exact answer, go to this tutorial: Immutable class in
Java
50. What is the use of toString() method in Java?
Ans: The toString() method of String class returns the string
representation of any object. Java compiler internally calls toString()
method on the object if you print any object.
51. What is the purpose of intern() method in Java?
Ans: The purpose of intern() method is to add the unique copy of the
string object to the string constant pool manually. When we create a
string using a new keyword, JVM stores it in the heap memory and
also store the unique copy of that string object in the string pool
using the intern() method.
When we do the same thing again, JVM will check if the string object
with the same content is present in the string constant pool or not.
If a string object with the same content is present, JVM will simply
point the reference of that object to the respective string variable.
If a string object with the same content is not present in the string
pool, JVM creates a string object with the same content in the String
pool and returns its reference to the string variable.
52. What is the purpose of format() method in Java String?
Or, what is the difference between format() method and printf()
method?
Ans: The main purpose of both format() and printf() methods is to
format the string. The only difference is that the format() method
returns the formatted string, whereas printf() method prints the
formatted string.
That’s why, if you want the formatted string in the program, then
use the format() method. And if you want to just print the formatted
string, use the printf() method.
53. Is String is ‘thread-safe’ in Java?
Ans: Yes, the string is thread-safe in Java.
int n1 = Integer.parseInt(str);
int n2 = Integer.valueOf(str);
System.out.println();
String s = String.valueOf(num);
Output:
System.out.println(substring);
}
}
Output:
What
is
your
name?
strJoiner.add("Red");
strJoiner.add("Green");
strJoiner.add("Blue");
System.out.println(strJoiner);
}
The above code creates a StringJoiner. It uses comma symbol (,) as
delimiter and square brackets ([ ]) as prefix and suffix. Then, it calls
add() method with some string values. Hence, this code displays the
following output:
Output:
[Red,Green,Blue]