Core Java - Interview Questions and Answers for 'Stringbuffer' - 10 question(s) found - Order By Newest Very frequently asked in different variations. Frequently asked in Deloitte ( 2 feedback ) , HCL Tech ( 3 feedback ), TCS and Coginizant (CTS) Q1. Explain the scenerios to choose between String , StringBuilder and StringBuffer ?
or
What is the difference between String , StringBuilder and StringBuffer ? Core Java
Ans. If the Object value will not change, use String Class because a String object is immutable.
If the Object value can change and will only be modified from a single thread, use StringBuilder because StringBuilder is unsynchronized(means faster).
If the Object value may change, and can be modified by multiple threads, use a StringBuffer because StringBuffer is thread safe(synchronized). Sample Code for String Sample Code for StringBuffer Sample Code for StringBuilder Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string class   string   stringbuilder   stringbuffer   String vs StringBuffer   String vs StringBuilder   String vs StringBuilder vs StringBuffer   StringBuffer vs stringBuilder Asked in 29 Companies basic   frequent Try 3 Question(s) TestVery frequently asked in HCL Tech ( Based of 4 inputs ) Q2. Write a Program to reverse a string iteratively and recursively Core Java
Ans. Using String method -
new StringBuffer(str).reverse().toString();
Iterative -
Strategy - Loop through each character of a String from last to first and append the character to StringBuilder / StringBuffer
public static String getReverseString(String str){
StringBuffer strBuffer = new StringBuffer(str.length);
for(int counter=str.length -1 ; counter>=0;counter--){
strBuffer.append(str.charAt(counter));
}
return strBuffer;
}
Recursive -
Strategy - Call the method with substring starting from 2nd character recursively till we have just 1 character.
public static String getReverseString(String str){
if(str.length <= 1){
return str;
}
return (getReverseString(str.subString(1)) + str.charAt(0);
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  string  StringBuffer  recursion  for loop  control statements  loop statement  stringbuffer.append  java.lang.String  java.lang.StringBuffer  String Manipulation Asked in 8 Companies   frequent Q3. Which String class does not override the equals() and hashCode() methods, inheriting them directly from class Object? Core Java
Ans. java.lang.StringBuffer. Sample Code for StringBuffer Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   object class   stringbuffer expert   rare Very frequently asked. Usually asked in different variants like Diff between StringBuffer , String Builder ; Difference between StringBuilder and String class; Choice between these classes etc. Q4. What is the difference between StringBuffer and String class ? Core Java
Ans. A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.
The String class represents character strings. All string literals in Java programs, such as "abc" are constant and implemented as instances of this class; their values cannot be changed after they are created. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string   stringbuffer   frequently asked Asked in 18 Companies basic   frequent Try 1 Question(s) TestVery frequently asked. Usually difference between String,StringBuffer and StringBuilder is asked in different variations. Q5. Difference between StringBuffer and StringBuilder ? Core Java
Ans. StringBuffer is synchronized whereas StringBuilder is not. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string   stringbuffer   string class   stringbuilder   synchronized   basic interview question   infosys technologies Asked in 17 Companies basic   frequent Try 1 Question(s) TestVery frequently asked in HCL Tech ( Based of 4 inputs ) Q6. Write a program to reverse a string iteratively and recursively ? Core Java
Ans. Using String method -
new StringBuffer(str).reverse().toString();
Iterative -
public static String getReverseString(String str){
StringBuffer strBuffer = new StringBuffer(str.length);
for(int counter=str.length -1 ; counter>=0;counter--){
strBuffer.append(str.charAt(counter));
}
return strBuffer;
}
Recursive -
public static String getReverseString(String str){
if(str.length <= 1){
return str;
}
return (getReverseString(str.subString(1)) + str.charAt(0);
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string   reverse   stringbuffer   string class   code Asked in 6 Companies   frequent Q7. what will be the output of this code ?
public static void main(String[] args){
StringBuffer s1=new StringBuffer("Buggy");
test(s1);
System.out.println(s1);
}
private static void test(StringBuffer s){
s.append("Bread");
} Core Java
Ans. BuggyBread Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   stringbuffer   string   method calling   pass by reference Q8. what will be the output of this code ?
public static void main(String[] args) {
String s1=new String("Buggy");
test(s1);
System.out.println(s1);
}
private static void test(StringBuffer s){
s.append("Bread");
} Core Java
Ans. Buggy Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   stringbuffer   string   method calling   pass by reference Q9. what will be the output of this code ?
public static void main(String[] args) {
StringBuffer s1=new StringBuffer("Buggy");
test(s1);
System.out.println(s1);
}
private static void test(StringBuffer s){
s=new StringBuffer("Bread");
} Core Java
Ans. Buggy Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   stringbuffer   string   method calling   pass by reference Q10. Can StringBuffer and StringBuilder in Java be inherited? Core Java
Ans. No, they have been declared final and hence cannot be extended. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  StringBuffer  StringBuilder a. String b. StringBuffer c. StringBuilder d. None of these create immutable objects.Ans.a. String
a. String b. StringBuffer c. StringBuilder d. None of theseAns.b. StringBuffer
Q13. Which of the following class cannot be extended or inherited ?
StringBuffer , StringBuilder Core Java
a. StringBuffer b. StringBuilder c. Both cannot be extended d. Both can be extendedAns.c. Both cannot be extended