13-03-25 ----------- Q)What is the output of the following piece of code? String s1=new String("hello"); String s2=new String("hello"); if(s1==s2) System.out.println("strings are equal"); else System.out.println("strings are not equal"); Ans:- strings are not equal Reason:- whenever we create a String object using new keyword, a brand new object is created in the heap area with its own hashcode. Q)What is the output of the following piece of code? String s1=new String("hello"); String s2=new String("hello"); if(s1.equals(s2)) System.out.println("strings are equal"); else System.out.println("strings are not equal"); Ans:- strings are equal Q)What is the output of the following piece of code? String s1="hello"; String s2="hello"; if(s1==s2) System.out.println("strings are equal"); else System.out.println("strings are not equal"); Ans:- strings are equal Reason:- In string constant pool only one string object is created. Therefore both s1&s2 reference the same object. I.e. the hashcode is the same. Q)What is the output of the following piece of code? String s1="hello"; String s2="hello"; if(s1.equals(s2)) System.out.println("strings are equal"); else System.out.println("strings are not equal"); Ans:- strings are equal Q)What is the difference between the following 2 statements? String s=null; String s1=""; =>In case of String s=null , no string object is created. Only reference is created that holds null value(i.e. pointing to no object) =>In case of String s1="", An empty string object is created. System.out.println(s1.length());//prints 0 Q)What is a mutable string? =>A string is said to be mutable, if its content in its orignal location is modifiable. Q)How to represent a mutable string in Java? =>by using two Java classes mutable strings are represented. 1)java.lang.StringBuffer 2)java.lang.StringBuilder =>Whenever modification of strings is more in the application, we go for StringBuffer or StringBuilder to represent strings. Q)Java application to perform appending,insertion and delete operations on a string. public class Test { public static void main(String[] args) { StringBuffer stringBuffer=new StringBuffer("hello"); stringBuffer.append("! how are you"); System.out.println(stringBuffer); stringBuffer.insert(0,"w"); System.out.println(stringBuffer); stringBuffer.delete(0, 7); System.out.println(stringBuffer); } } =>java.lang.StringBuffer is thread-safe. I.e all its methods are synchronized. =>library methods of StringBuffer and StringBuilder are the same.But, StringBuilder is thread-safe. I.e. its methods are non-synchronized. Q)Java program to find if a word is a palindrome. import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.print("Enter a word:"); String word=scanner.next(); StringBuffer stringBuffer=new StringBuffer(word); stringBuffer.reverse(); String reversedword=new String(stringBuffer); if(word.equals(reversedword)) System.out.println("It is a palindrome"); else System.out.println("It is not a palindrome"); scanner.close(); } }