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) Both of the mentioned
d) None of the mentioned
View Answer

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

2. Which of these 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.

4. What is the string contained in s after following lines of 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.

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.

6. What is the output of this program?

  1.     class output {
  2.         public static void main(String args[])
  3.         {
  4.             String a = "hello i love java";
  5.             System.out.println(indexof('i') + " " + indexof('o') + " " + lastIndexof('i') + " " + lastIndexof('o')));
  6.         }
  7.     }

a) 6 4 6 9
b) 5 4 5 9
c) 7 8 8 9
d) 4 3 6 9
View Answer

Answer:a
Explantion: 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:
$ javac output.java
$ java output
6 4 6 9

7. What is the output of this program?

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

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

Answer: c
Explanation: None.
Output:
$ javac output.java
$ java output
llo

8. What is the output of this program?

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

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 is the output of this program?

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

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 is the output of this program?

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

a) a is a lower case Letter
is White space character

b) b is a lower case Letter
is White space character

c) a is a lower case Letter
A is a upper case Letter

d) a is a lower case Letter
0 is a digit
View Answer

Answer:a
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 weather the given character is of specified type or not. They return true or false i:e Boolean variable.
Output:
$ javac output.java
$ java output
a is a lower case Letter
is White space character

Sanfoundry Global Education & Learning Series – Java Programming Language.

About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







Subscribe Newsletter & Posts
Sanfoundry Internships
Follow Manish & Sanfoundry