Java MCQs on StringBuffer class of Java Programming Language. Nội dung chính Show
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
Answer: b
Clarification: 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[]
Answer: b
Clarification: 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[]
Answer: a
Clarification: None.
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
Answer: b
Clarification: 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
Answer: a
Clarification: reverse[] method reverses all characters. It returns the reversed object on which it was called.
6. What will be the output of the following Java program?
- class output
- {
- public static void main[String args[]]
- {
- String a = "hello i love java";
- System.out.println[a.indexOf['e']+" "+a.indexOf['a']+" "+a.lastIndexOf['l']+" "+a.lastIndexOf['v']];
- }
- }
a] 6 4 6 9
b] 5 4 5 9
c] 7 8 8 9
d] 1 14 8 15
Answer: d
Clarification: 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 1 14 8 15
7. What will be the output of the following Java program?
- class output
- {
- public static void main[String args[]]
- {
- StringBuffer c = new StringBuffer["Hello"];
- c.delete[0,2];
- System.out.println[c];
- }
- }
a] He
b] Hel
c] lo
d]
llo
Answer: d
Clarification: 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?
- class output
- {
- public static void main[String args[]]
- {
- StringBuffer c = new StringBuffer["Hello"];
- StringBuffer c1 = new StringBuffer[" World"];
- c.append[c1];
- System.out.println[c];
- }
- }
a] Hello
b] World
c] Helloworld
d] Hello World
Answer: d
Clarification: 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?
- class output
- {
- public static void main[String args[]]
- {
- StringBuffer s1 = new StringBuffer["Hello"];
- StringBuffer s2 = s1.reverse[];
- System.out.println[s2];
- }
- }
a] Hello
b] olleH
c] HelloolleH
d] olleHHello
Answer: b
Clarification: 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?
Answer: c
Clarification: 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:
Which of this method of class StringBuffer is used to reverse sequence of characters?
In order to reverse the sequence of characters in a StringBuffer Object, we use the reverse[] method.
Which of this method of class StringBuffer is used to concentrate the String representation to the end of invoking String?
Clarification: append[] method of class StringBuffer is used to concatenate the string representation to the end of invoking string.
Which method can be used to set the length of the buffer within a StringBuffer object?
The setLength[int newLength] method is used to change the length of the StringBuffer Object. It sets the length of the character sequence. The character sequence is updated to a new one whose length is determined by the value passed as the parameter in the method.
Which of this method of class String is used to obtain a length of String object?
Which of this method of class String is used to obtain a length of String object? Explanation: Method length[] of string class is used to get the length of the object which invoked method length[].