Java MCQs 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? Answer: a 2. What will s2 contain after following lines of Java code? a) one Answer: c 3. Which of this method of class StringBuffer is used to reverse sequence of characters? Answer: a 4. Which of this method of class StringBuffer is used to get the length of the sequence of characters? Answer: a 5. Which of the following are incorrect form of StringBuffer class constructor? Answer: d 6. What will be the output of the following Java code? a) 4 7. What will be the output of the following Java code? a) Hello java 8. What will be the output of the following Java code? a) xello 9. What will be the output of the following Java code? a) HelloGoodWorld 10. What will be the output of the following Java code? a) hello
a) substring()
b) Substring()
c) SubString()
d) None of the mentioned
Clarification: None. StringBuffer s1 = "one";
StringBuffer s2 = s1.append("two")
b) two
c) onetwo
d) twoone
Clarification: Two strings can be concatenated by using append() method.
a) reverse()
b) reverseall()
c) Reverse()
d) reverseAll()
Clarification: reverse() method reverses all characters. It returns the reversed object on which it was called.
a) length()
b) capacity()
c) Length()
d) Capacity()
Clarification: 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.
a) StringBuffer()
b) StringBuffer(int size)
c) StringBuffer(String str)
d) StringBuffer(int size , String str)
Clarification: None.
class output
{
public static void main(String args[])
{
StringBuffer c = new StringBuffer("Hello");
System.out.println(c.length());
}
}
b) 5
c) 6
d) 7
Clarification: length() method is used to obtain length of StringBuffer object, length of “Hello” is 5.
Output:
$ javac output.java
$ java output
5
class output
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("Hello");
sb.replace(1,3,"Java");
System.out.println(sb);
}
}
b) Hellojava
c) HJavalo
d) Hjava
Clarification: The replace() method replaces the given string from the specified beginIndex and endIndex.
$ javac output.java
$ java output
HJavalo
class output
{
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer("Hello");
s1.setCharAt(1,'x');
System.out.println(s1);
}
}
b) xxxxx
c) Hxllo
d) Hexlo
Clarification: None.
Output:
$ javac output.java
$ java output
Hxllo
class output
{
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer("Hello World");
s1.insert(6 , "Good ");
System.out.println(s1);
}
}
b) HellGoodoWorld
c) HellGood oWorld
d) Hello Good World
Clarification: 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
class output
{
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer("Hello");
s1.insert(1,"Java");
System.out.println(s1);
}
}
b) java
c) Hello Java
d) HJavaello
Clarification: Insert method will insert string at a specified position
Output:
$ javac output.java
$ java output
HJavaello