Java Questions & Answers – StringBuffer Methods

This section of our 1000+ Java MCQs focuses on StringBuffer class’s methods of Java Programming Language.

1. Which of these methods of class StringBuffer is used to extract a substring from a String object?
a) substring()
b) Substring()
c) SubString()
d) None of the mentioned
View Answer

Answer: a
Explanation: None.

2. What will s2 contain after following lines of Java code?

 StringBuffer s1 = "one";
StringBuffer s2 = s1.append("two")

a) one
b) two
c) onetwo
d) twoone
View Answer

Answer: c
Explanation: Two strings can be concatenated by using append() method.
advertisement
advertisement

3. Which of this method of class StringBuffer is used to reverse sequence of characters?
a) reverse()
b) reverseall()
c) Reverse()
d) reverseAll()
View Answer

Answer: a
Explanation: reverse() method reverses all characters. It returns the reversed object on which it was called.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

4. Which of this method of class StringBuffer is used to get the length of the sequence of characters?
a) length()
b) capacity()
c) Length()
d) Capacity()
View Answer

Answer: a
Explanation: length()- returns the length of String the StringBuffer would create whereas capacity() returns a total number of characters that can be supported before it is grown.

5. Which of the following are incorrect form of StringBuffer class constructor?
a) StringBuffer()
b) StringBuffer(int size)
c) StringBuffer(String str)
d) StringBuffer(int size , String str)
View Answer

Answer: d
Explanation: None.
advertisement

6. What will be the output of the following Java code?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.              StringBuffer c = new StringBuffer("Hello");
  6.              System.out.println(c.length());
  7.         }
  8.     }

a) 4
b) 5
c) 6
d) 7
View Answer

Answer: b
Explanation: length() method is used to obtain length of StringBuffer object, length of “Hello” is 5.
Output:

advertisement
$ javac output.java
$ java output
5

7. What will be the output of the following Java code?

  1.   class output 
  2.   {  
  3.       public static void main(String args[]) 
  4.       {  
  5.           StringBuffer sb=new StringBuffer("Hello");  
  6.           sb.replace(1,3,"Java");  
  7.           System.out.println(sb);
  8.       }  
  9.   }

a) Hello java
b) Hellojava
c) HJavalo
d) Hjava
View Answer

Answer: c
Explanation: The replace() method replaces the given string from the specified beginIndex and endIndex.

$ javac output.java
$ java output
HJavalo

8. What will be the output of the following Java code?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.            StringBuffer s1 = new StringBuffer("Hello");
  6.            s1.setCharAt(1,'x');
  7.            System.out.println(s1);
  8.         }
  9.     }

a) xello
b) xxxxx
c) Hxllo
d) Hexlo
View Answer

Answer: c
Explanation: None.
Output:

$ javac output.java
$ java output
Hxllo

9. What will be the output of the following Java code?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.            StringBuffer s1 = new StringBuffer("Hello World");
  6.            s1.insert(6 , "Good ");
  7.            System.out.println(s1);
  8.         }
  9.    }

a) HelloGoodWorld
b) HellGoodoWorld
c) HellGood oWorld
d) Hello Good World
View Answer

Answer: d
Explanation: The insert() method inserts one string into another. It is overloaded to accept values of all simple types, plus String and Objects. Sting is inserted into invoking object at specified position. “Good ” is inserted in “Hello World” T index 6 giving “Hello Good World”.
output:

$ javac output.java
$ java output 
Hello Good World

10. What will be the output of the following Java code?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.            StringBuffer s1 = new StringBuffer("Hello");
  6.            s1.insert(1,"Java");
  7.            System.out.println(s1);
  8.         }
  9.     }

a) hello
b) java
c) Hello Java
d) HJavaello
View Answer

Answer: d
Explanation: Insert method will insert string at a specified position
Output:

$ javac output.java
$ java output
HJavaello

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.