Java Training in Chennai – String, StringBuffer Puzzles

Java Training in Chennai – String, StringBuffer Puzzles

Here this blog explains about Java Training in Chennai – String, StringBuffer Puzzles . Some of them are discussed below :

____________________________________________________________________

 1 . What is the expected output?

public static void main(String[] args) {
boolean stmt1 = “payilagam” == “payilagam”;
boolean stmt2 = new String(“payilagam”) == “payilagam”;
boolean stmt3 = new String(“payilagam”) == new String(“payilagam”);
System.out.println(stmt1 && stmt2 || stmt3);
}

public static void main(String[] args) {
boolean stmt1 = “payilagam” == “payilagam”;
boolean stmt2 = new String(“payilagam”).equals(new String(“payilagam”));
boolean stmt3 = “payilagam”.toString()==”payilagam”;
System.out.println(stmt1 && stmt2 && stmt3);
}

Which of the statements would evaluate to true?
public class Tester {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer(“payilagam”);
String s = new String(“payilagam”);
boolean stmt1 = s.equals(sb) ;
boolean stmt2 = sb.equals(s) ;
boolean stmt3 = sb.toString() == s ;
boolean stmt4 = sb.toString().equals(s) ;
boolean stmt5 = s.equals(sb.toString()) ;
}
}

public class Tester {
public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer(“javachamp”);
StringBuffer sb2 = new StringBuffer(“javachamp”);
boolean stmt1 =sb1.equals(sb2) ;
boolean stmt2 = sb1 == sb2;
String s1 = new String(“javachamp”);
String s2 = new String(“javachamp”);
boolean stmt3 = s1.equals(s2);
boolean stmt4 = s1 == s2;
}
}

public static void main(String args []) {
String stmt = null;
System.out.print(null+stmt);
System.out.print(stmt+null);
}

What is the initial capacity of the following string builder?

StringBuilder sb = new StringBuilder(“Incredible India!”);

Consider the following string:
String word = “How is Chennai Today? It is too hot!”;

What is the value displayed by the expression word.length()?
What is the value returned by the method call word.charAt(12)?
Write an expression that refers to the letter b in the string referred to by hannah.

_______________________________________________________________________________________