In Java Using the following declarations, what is the output of each snippet of
ID: 3886782 • Letter: I
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 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);
Explanation / Answer
1) System.out.println(s4.lenght());
Answer:15
2) System.out.println(s1.charAt(2));
Answer:3
3) if (s2.equals(s3))
System.out.println("same");
else
System.out.println("different");
Answer:different
4) if (s1 == "123456789")
System.out.println("same");
else
System.out.println("different");
Answer:different
5) if (s1.startsWith("234", 1))
System.out.println("yes");
else
System.out.println("no");
Answer:yes
6) System.out.println(s4.lastIndexOf('a'));
Answer:3
7) System.out.println(s4.substring(8, 11));
Answer:Awe
8) sb1.append(s3).insert(0, s2).append(s1);
System.out.println(sb1);
Answer:abcdeABCDE12345
9) String[] tokens = s4.split(" ");
for (String token : tokens)
System.out.printf("%s--", token);
System.out.println();
Answer:Java--is--Awesome--
10) sb2.setCharAt(4, '-');
System.out.println(sb2);
Answer:Java-is Awesome
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.