Cognizant Technology Solutions
String and StringBuffer
Exercise
CATP Java Team
5/20/2012
String and StringBuffer Exercise 2012
For The Associates:
The documents details two flavors of problem statements
Statement # 1: Few problem solutions have been provided for associates should analyze the
program and write down the program output. This will enhance the analyzing skills of associates
and also understand “why” part of java programming feature. The associates can then try
running the program in eclipse and check if the output with what they have written.
Stamen # 2: There are some problem statements provided similar to the final assessment and
associates need to solve it. This will enhance the programming skills of the associates.
IMPORTANT: These exercises will gear you up for the core java assessment so please
develop/analyze the exercise independently. In case you are stuck up reach out to the trainers.
Exercises:
1. What will be the output?
public class Tester {
public static void main(String[] args) {
String stmt = "Java is a Programming Language";
for (String token : stmt.split("//s")) {
System.out.print(token + " ");
}
}
2. What will be the output
public static void main(String[] args) {
}
boolean stmt1 = "hello" == "hello";
boolean stmt2 = new String("hello") == "hello";
boolean stmt3 = new String("hello") == new
String("hello");
System.out.println(stmt1 && stmt2 || stmt3);
}
2
String and StringBuffer Exercise 2012
3. What will be the output ?
public static void main(String[] args) {
StringBuffer buffer1 = new StringBuffer("java");
StringBuffer buffer2 = new StringBuffer(buffer1);
if (buffer1.equals(buffer2))
System.out.println("true");
else
System.out.println("false");
}
4. Which of the boolean variables will evaluate to true?
public class Tester {
public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer(“java”);
StringBuffer sb2 = new StringBuffer(“java”);
boolean stmt1 =sb1.equals(sb2) ;
boolean stmt2 = sb1 == sb2;
String s1 = new String(“java”);
String s2 = new String(“java”);
boolean stmt3 = s1.equals(s2);
boolean stmt4 = s1 == s2;
}
}
3
String and StringBuffer Exercise 2012
5. What is the result of compiling and running the following code?
public class Tester {
public static void main(String[] args) {
String str = "java";
StringBuffer sb = new StringBuffer("java");
sb.insert(9, "programming");
str.concat("exercise");
if (sb.length() < 6 || str.equals("java")) {
System.out.print(sb);
}
sb.delete(2, 7);
System.out.print(sb);
}
}
6. What is the result of compiling and running the following code?
public static void main(String[] args) {
String s1 = null;
String s2 = null;
if (s1 == s2)
System.out.print("A");
if (s1.equals(s2))
System.out.print("B");
}
4
String and StringBuffer Exercise 2012
7. What is the result of compiling and running the following code?
public class Tester {
public static void main(String[] args) {
System.out.print(“1”);
try {
return;
} catch (Exception e) {
System.out.print(“2”);
} finally {
System.out.print(“3”);
}
System.out.print(“4”);
}
}
8. Create a method which accepts a string and inserts a character at a specified position.
Class Name StringModifier
Method Name insertCharacter
Method Description Accepts a String and insert a character at a
specified position.
Argument String string , char c, int position
Return Type String : Resulting String after the insertion
Logic Accepts a String and insert a character at a
specified position.
9. Create a class containing method counts the occurrence of a character in a String
Cass Name CharacterCounter
Method Name countCharacter
Method Description Accepts a String and a character and count the
number of occurrence of the character in the
entered String
Argument String string, char ch
Return Type int count
Logic Accept a String and Character
Count the number of occurrence of the character in
the String and return the count value.
5
String and StringBuffer Exercise 2012
10. Create a class containing method which can accepts two string and a character and insert the
second string in the position of the character if present in first string
Cass Name StringManager
Method Name insertString
Method Description Accepts two Strings and a character and insert the
second string in the position of the character if
present in first string
Argument String string1, String string2, char ch
Return Type String
Logic Accepts two Strings and a character and insert the
second string in the position of the character if
present in first string.
For Example
If string1=”Hello World”
string2=”java”
ch=’W’
Output should be “Hello JavaWorld”