1. How many different way to create String object?
There are three different way to create the String object as follow:
- By converting Char array to String
- By String literal
- By new keyword
2. What is String Constant Pool (SCP) in Java?
String constant pool is a pool of String literal objects stored in Java heap memory.It doesn’t create a new instance if the string already exist in the string constant pool. String objects using new operator are stored in direct heap memory not in the String pool.
For example,
String s1="Durga";
String s2="Durga"; // Doesn't create a new instance
String s3=new String("Durga"); //creates a new instance
String s1 and s2 stores string object "Durga" in one particular location of String pool but String s3 uses new operator even the string object already present in the string pool it creates the new instance in direct heap memory.
3. Why String objects immutable in JAVA?
The string is immutable in Java because String objects are cached in String pool. Since cached String literals are shared between multiple clients, there is always a risk, where one client's action would affect all another client.
4. How many different methods to compare String in JAVA?
- compareTo() method - used to compare during sorting operation.
- == operator - used to compare the reference matching.
- equals() method - used to compare value during authentication.
5. What will be the output of following JAVA code?
class String_1{
public static void main(String args[]){
String s1="Yoga";
String s2=new String("Yoga");
String s3="YOGA";
System.out.println(s1.equals(s2));
System.out.println(s1==s2);
System.out.println(s1.equals(s3));
System.out.println(s1==s3);
System.out.println(s1.equalsIgnoreCase(s3));
}
}
Output:
true
false
false
false
true
Explanation: equals() method compares the objects value and == compares the object reference. Even though s1 and s2 have same value, s1 is string literal which stored in string pool and s2 uses new operator stored in heap memory.
6. What will be the output of following JAVA code?
class String_2{
public static void main(String args[]){
String s1="health";
String s2="health";
String s3="Health";
String s4="";
String s5="he";
String s6="we";
System.out.println(s1.compareTo(s2));
System.out.println(s1.compareTo(s3));
System.out.println(s3.compareTo(s1));
System.out.println(s5.compareTo(s4));
System.out.println(s4.compareTo(s5));
System.out.println(s5.compareTo(s1));
System.out.println(s6.compareTo(s1));
}
}
Output:
0
32
-32
2
-2
-4
15
Explanation:
s1.compareTo(s2) - returns 0 both are equal
s1.compareTo(s3) - returns 32 s1 greater than s2 (ASCII value distance between them)
s3.compareTo(s1) - returns -32 s1 lesser than s2 (ASCII value distance between them)
s5.compareTo(s4) - returns 2 s5 empty string compares the length of s4.
s4.compareTo(s5) - returns -2 length of s5 compares the s4 null string.
s5.compareTo(s1) - returns -4 s5 string length compares length of s1 after comparing character sequence.
s6.compareTo(s1) - returns 15 s6 compares ASIIC distance between first differed character of s1.
7. Find the output of following JAVA code.
class String_3{
public static void main(String args[]){
String s1="Learn with";
String s2=" Durga ";
System.out.println(s1+s2);
System.out.println(10+100+s2+100+10);
System.out.println(s1.concat(s2));
}
}
Output:
Learn with Durga
110 Durga 10010
Learn with Durga
Explanation:
System.out.println(s1+s2); // "+" concatenate String s1 and s2
System.out.println(10+100+s2+100+10); // "+" starts with integer addition results 110; now integer and string addition change "+" arithmetic operator into string concatenation results with string "110Durga "; After this change, "+" starts continue the string concatenation even integer value there in addition.
System.out.println(s1.concat(s2)); // string s2 added at the end of string s1
8. Find the output of following JAVA code.
class String_4{
public static void main(String args[]){
String s1="Learn with";
String s2=" Durga ";
char s3[]=new char[5];
String s4="Durga";
System.out.println(s1.substring(1));
System.out.println(s1.substring(1,5));
System.out.println(s2.charAt(1));
s1.getChars(1,5,s3,0);
System.out.println(s3);
s1.getChars(0,5,s3,0);
System.out.println(s3);
System.out.println(s1.contains("ear"));
System.out.println(s2.startsWith("D"));
System.out.println(s2.endsWith("A "));
System.out.println(s2.startsWith(" D"));
System.out.println(s2.endsWith("a "));
System.out.println(s4.toLowerCase());
System.out.println(s4.toUpperCase());
System.out.println(s1+s2);
System.out.println(s1+s2.trim());
System.out.println(String.join("#",s1,s2));
}
}
Output:
earn with
earn
D
earn
Learn
true
false
false
true
true
durga
DURGA
Learn with Durga
Learn withDurga
Learn with# Durga
Explanation:
s1 = "Learn with"
s2 = " Durga "
s3 // char array of size 5
s4 = "Durga"
s1.substring(1) → substring of s1 starts from starting index 1 → earn with
s1.substring(1,5) → substring of s1 starts from 1 and end excluding ending index 5 → earn
s2.charAt(1) → character in 1st index of string s2 → D
s3=s1.getChars(1,5,s3,0) → substring copied from s1 in range of index(1,5) to char array s3 → earn
s3=s1.getChars(0,5,s3,0) →substring copied from s1 in range of index(0,5) to char array s3 →Learn
s1.contains("ear") → checks substring "ear" present or not in s1 →true
s2.startsWith("D") → checks string s2 starts with character "D" or not → false // because its starts with space.
s2.endsWith("A ") →checks string s2 ends with character "A" or not → false // because its ends with lower case "a ".
s2.startsWith(" D") →checks string s2 starts with character " D" or not → true // because its starts with space.
s2.endsWith("a ") →checks string s2 ends with character "a " or not → true // because its ends with lower case "a ".
s4.toLowerCase() → changes upper case into lower case → durga
s4.toUpperCase() → changes lower case into upper case → DURGA
s1+s2 → concatenate s1 and s2 before trim → Learn with Durga
s1+s2.trim() →concatenate s1 and space removed at ends of s2 → Learn withDurga
String.join("#",s1,s2) → joins s1 and s2 with symbal "#" → Learn with# Durga
PREVIOUS NEXT
So informative & Helpul ...Thank You
ReplyDelete