Java Questions & Answers – StringBuffer Class

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

1. Which of these class is used to create an object whose character sequence is mutable?
a) String()
b) StringBuffer()
c) String() & StringBuffer()
d) None of the mentioned
View Answer

Answer: b
Explanation: StringBuffer represents growable and writable character sequence.

2. Which of this method of class StringBuffer is used to concatenate the string representation to the end of invoking string?
a) concat()
b) append()
c) join()
d) concatenate()
View Answer

Answer: b
Explanation: None.

3. Which of these method of class StringBuffer is used to find the length of current character sequence?
a) length()
b) Length()
c) capacity()
d) Capacity()
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

4. What is the string contained in s after following lines of Java code?

StringBuffer s new StringBuffer("Hello");
s.deleteCharAt(0);

a) Hell
b) ello
c) Hel
d) llo
View Answer

Answer: b
Explanation: deleteCharAt() method deletes the character at the specified index location and returns the resulting StringBuffer object.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

5. Which of the following statement is correct?
a) reverse() method reverses all characters
b) reverseall() method reverses all characters
c) replace() method replaces first occurrence of a character in invoking string with another character
d) replace() method replaces last occurrence of a character in invoking string with another character
View Answer

Answer: a
Explanation: reverse() method reverses all characters. It returns the reversed object on which it was called.
advertisement

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

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String a = "hello i love java";
  6.             System.out.println(a.indexOf('e')+" "+a.indexOf('a')+" "+a.lastIndexOf('l')+" "+a.lastIndexOf('v'));
  7.         }
  8.     }

a) 6 4 6 9
b) 5 4 5 9
c) 7 8 8 9
d) 1 14 8 15
View Answer

Answer: d
Explanation: indexof(‘c’) and lastIndexof(‘c’) are pre defined function which are used to get the index of first and last occurrence of
the character pointed by c in the given array.
Output:

advertisement
$ javac output.java
$ java output
1 14 8 15

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

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

a) He
b) Hel
c) lo
d) llo
View Answer

Answer: d
Explanation: delete(0,2) is used to delete the characters from 0 th position to 1 st position.
Output:

$ javac output.java
$ java output
llo

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

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

a) Hello
b) World
c) Helloworld
d) Hello World
View Answer

Answer: d
Explanation: append() method of class StringBuffer is used to concatenate the string representation to the end of invoking string.
Output:

$ javac output.java
$ java output
Hello World

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

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

a) Hello
b) olleH
c) HelloolleH
d) olleHHello
View Answer

Answer: b
Explanation: reverse() method reverses all characters. It returns the reversed object on which it was called.
Output:

$ javac output.java
$ java output
olleH

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

  1.     class output 
  2.     {
  3.        class output 
  4.        {
  5.          public static void main(String args[])
  6.          {
  7.             char c[]={'A', '1', 'b' ,' ' ,'a' , '0'};
  8.             for (int i = 0; i < 5; ++i)
  9.             {
  10.                    i++; 
  11.                    if(Character.isDigit(c[i]))
  12.                        System.out.println(c[i]+" is a digit");
  13.                    if(Character.isWhitespace(c[i]))
  14.                        System.out.println(c[i]+" is a Whitespace character");
  15.                    if(Character.isUpperCase(c[i]))
  16.                        System.out.println(c[i]+" is an Upper case Letter");
  17.                    if(Character.isLowerCase(c[i]))
  18.                        System.out.println(c[i]+" is a lower case Letter");
  19.                    i++;
  20.             }
  21.         }
  22.     }

a)

   a is a lower case Letter
     is White space character

b)

   b is a lower case Letter
     is White space character

c)

   1 is a digit
   a is a lower case Letter

d)

   a is a lower case Letter
   0 is a digit
View Answer
Answer: c
Explanation: Character.isDigit(c[i]), Character.isUpperCase(c[i]), Character.isWhitespace(c[i]) are the function of library java.lang they are used to find whether the given character is of specified type or not. They return true or false i:e Boolean variable.
Output:

$ javac output.java
$ java output  
1 is a digit
a is a lower case Letter
 
 

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.