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(a.indexOf('i')+" "+a.indexOf('o')+" "+a.lastIndexOf('i')+" "+a.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: 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 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.             {
  7.                    if(Character.isDigit(c[i]))
  8.                        System.out.println(c[i]+" is a digit");
  9.                    if(Character.isWhitespace(c[i]))
  10.                        System.out.println(c[i]+" is a Whitespace character");
  11.                    if(Character.isUpperCase(c[i]))
  12.                        System.out.println(c[i]+" is an Upper case Letter");
  13.                    if(Character.isLowerCase(c[i]))
  14.                        System.out.println(c[i]+" is a lower case Letter");
  15.                    i=i+3;
  16.             }
  17.         }
  18.     }

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: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 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
A is an Upper Case Letter

Sanfoundry Global Education & Learning Series – Java Programming Language.

Subscribe Newsletter & Posts
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.







1000 Java MCQs | Quiz

Integer and Floating Data Types
Character and Boolean Data Types
Literals & Variables
Type Conversions, Promotions and Castings
Arrays
Arithmetic Operators
Bitwise Operators
Relational Operators and Boolean Logic Operators
Assignment Operators and Operator Precedence
Control Statements
Class Fundamentals & Declaring objects
Introduction to Methods
Constructors & Garbage Collection
Overloading Methods & Argument Passing
Access Control
Arrays Revisited & Keyword static
String Class
Inheritance
Method overriding
The Object Class
String Handling Basics
Character Extraction
String Comparison
Searching & Modifying a String
StringBuffer class
StringBuffer Methods
Packages
Interfaces
Java.lang Introduction
Java.lang - Integer, Long And Character Wrappers
Java.lang - Void, Process and System Class
Java.lang - Object & Math Class
Java.io Introduction
Java.io Byte Streams
Java.io Character Streams
Serialization
Serialization & Deserialization
Networking Basics
URL class
HttpResponse & URLConnection Class
networking - Server, Sockets & httpd Class
networking - httpd.java Class
networking - Datagrams
Java.util - ArrayList Class
Java.util - LinkedList, HashSet & TreeSet Class
Java.util - Maps
Java.util - Array Class
Java.util - Vectors & Stack
Java.util - Dictionary, Hashtable & Properties
Java.util - BitSet & Date class
Methods Taking Parameters
Exceptional Handling Basics
Exceptions Types
Throw, Throws & Nested Try
isAlive(), Join() & Thread Synchronization
Implementing Runnable interface for Threads
Thread class
Multithreading Basics
Networking Basics
Finally & Built in Exceptions
Creating Threads
Input & Output Basics
Reading Console Input
Writing Console Output
Reading & Writing Files
Applets Fundamentals
Core Java API Packages
Remote Method Invocation (RMI)
Text Formatting
Event Handling Basics
ActionEvent & AdjustmentEvent Class
ComponentEvent, ContainerEvent & FocusEvent Class
MouseEvent, TextEvent & WindowEvent Class
Event Listeners Interfaces
Java.lang - Rounding Functions
Java.lang - Miscellaneous Math Methods & StrictMath Class
Java.lang - ThreadGroup class & Runnable Interface
Java.lang - System Class Advance
Java.lang - Double & Float Wrappers
Java.lang - Byte & Short Wrappers
Java.lang - Character Wrapper Advance
Java.lang - Boolean Wrapper Advance
Java.lang - Runtime & ClassLoader Classes
java.lang - Class
Collection Framework Overview
Collections Interface
Collection Algorithms
Iterators
Locale & Random Classes
Observable & Timer Class
Restrictions On Generics
Wildcards
Recursion
Command Line Arguments
Inheritance - Abstract Class and Super
Try & Catch
Creating Exceptions
Generics
Generic Methods
Type Interface
Subscribe Newsletter & Posts
Follow Manish & Sanfoundry