Question: In Java Using the following declarations, what is the output of each snippet of code? String s1 = new String(12345); String s2 = abcde; String
In Java
Using the following declarations, what is the output of each snippet of code?
String s1 = new String("12345");
String s2 = "abcde";
String s3 = "ABCDE";
String s4 = "Java is Awesome";
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder(s4);
1) System.out.println(s4.lenght());
2) System.out.println(s1.charAt(2));
3) if (s2.equals(s3)) System.out.println("same"); else System.out.println("different");
4) if (s1 == "123456789") System.out.println("same"); else System.out.println("different");
5) if (s1.startsWith("234", 1)) System.out.println("yes"); else System.out.println("no");
6) System.out.println(s4.lastIndexOf('a'));
7) System.out.println(s4.substring(8, 11));
8) sb1.append(s3).insert(0, s2).append(s1); System.out.println(sb1);
9) String[] tokens = s4.split(" "); for (String token : tokens) System.out.printf("%s--", token); System.out.println();
10) sb2.setCharAt(4, '-'); System.out.println(sb2);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
