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