Java Multiple Choice Questions & Answers on StringBuffer Class for Freshers

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
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()
Answer: b
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
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
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.
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('e')+" "+a.indexOf('a')+" "+a.lastIndexOf('l')+" "+a.lastIndexOf('v'));
  6.         }
  7.     }
a) 6 4 6 9
b) 5 4 5 9
c) 7 8 8 9
d) 1 14 8 15
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.
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
Answer: d

Explanation: delete(0,2) is used to delete the characters from 0 th position to 1 st position.
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
Answer: d

Explanation: append() method of class StringBuffer is used to concatenate the string representation to the end of invoking string.
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
Answer: b

Explanation: reverse() method reverses all characters. It returns the reversed object on which it was called.
10. What is the output of this program?
  1.     class output {
  2.     class output {
  3.         public static void main(String args[])
  4.         {
  5.             char c[]={'A', '1', 'b' ,' ' ,'a' , '0'};
  6.             for (int i = 0; i < 5; ++i)
  7.             {
  8.                    i++; 
  9.                    if(Character.isDigit(c[i]))
  10.                        System.out.println(c[i]+" is a digit");
  11.                    if(Character.isWhitespace(c[i]))
  12.                        System.out.println(c[i]+" is a Whitespace character");
  13.                    if(Character.isUpperCase(c[i]))
  14.                        System.out.println(c[i]+" is an Upper case Letter");
  15.                    if(Character.isLowerCase(c[i]))
  16.                        System.out.println(c[i]+" is a lower case Letter");
  17.                    i++;
  18.             }
  19.         }
  20.     }
a) a is a lower case Letter
is White space character
b) b is a lower case Letter
is White space character
c) 1 is a digit
a is a lower case Letter
d) a is a lower case Letter
0 is a digit
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 whether the given character is of specified type or not. They return true or false i:e Boolean variable.

Related

C# Questions & Answers on Inheritance Implementation for Freshers

1. Select the correct output for the given set of code? class sample { public int i; void display() { Console.WriteLine(i); } } class sample1 : s...

C# Questions & Answers on Method Overloading for Freshers

1. The process of defining two or more methods within the same class that have same name but different parameters list? a) Method overloadingb) Method overridingc) Encapsulationd) None of the menti...

C# Questions & Answers on Fundamentals of Inheritance for Freshers

1. Which procedure among the following should be used to implement a ‘Has a’ or a ‘Kind of’ relationship between two entities? a) Polymorphismb) Inheritancec) Templatesd) All of the mentioned Answ...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item