Java Multiple Choice Questions & Answers on StringBuffer Methods for Freshers

1. Which of these method of class StringBuffer is used to extract a substring from a String object?

a) substring()
b) Substring()
c) SubString()
d) None of the mentioned
Answer: a
2. What will s2 contain after following lines of code?
StringBuffer s1 = “one”;
StringBuffer s2 = s1.append(“two”)

a) one
b) two
c) onetwo
d) twoone
Answer: c

Explanation: Two strings can be concatenated by using append() method.
3. Which of these method of class StringBuffer is used to reverse sequence of characters?

a) reverse()
b) reverseall()
c) Reverse()
d) reverseAll()
Answer: a

Explanation: reverse() method reverses all characters. It returns the reversed object on which it was called.
4. Which of these method of class StringBuffer is used to get the length of sequence of characters?

a) length()
b) capacity()
c) Length()
d) Capacity()
Answer: a

Explanation: length()- returns the length of String the StringBuffer would create whereas capacity() returns total number of characters that can be supported before it is grown.
5. Which of the following are incorrect form of StringBuffer class constructor?

a) StringBuffer()
b) StringBuffer(int size)
c) StringBuffer(String str)
d) StringBuffer(int size , String str)
Answer: d
6. 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.              System.out.println(c.length());
  6.         }
  7.     }
a) 4
b) 5
c) 6
d) 7
Answer: b

Explanation: length() method is used to obtain length of StringBuffer object, length of “Hello” is 5.
7. What is the output of this program?
  1.   class output {  
  2.       public static void main(String args[]) {  
  3.           StringBuffer sb=new StringBuffer("Hello");  
  4.           sb.replace(1,3,"Java");  
  5.           System.out.println(sb);
  6.       }  
  7.   }
a) Hello java
b) Hellojava
c) HJavalo
d) Hjava
Answer: c

Explanation: The replace() method replaces the given string from the specified beginIndex and endIndex.
8. 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.            s1.setCharAt(1,'x');
  6.            System.out.println(s1);
  7.         }
  8.     }
a) xello
b) xxxxx
c) Hxllo
d) Hexlo
Answer: c
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 World");
  5.            s1.insert(6 , "Good ");
  6.            System.out.println(s1);
  7.         }
  8.    }
a) HelloGoodWorld
b) HellGoodoWorld
c) HellGood oWorld
d) Hello Good World
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”.
10. 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.            s1.insert(1,"Java");
  6.            System.out.println(s1);
  7.         }
  8.     }
a) hello
b) java
c) Hello Java
d) HelloJava
Answer: d

Explanation: Insert method will insert string at a specified position

Related

C# Multiple Choice Questions & Answers on Integer Data Types for Freshers

1. How many Bytes are stored by ‘Long’ Datatype in C# .net? a) 8b) 4c) 2d) 1 Answer: a Explanation : ‘Long’ is the datatype keyword used for storing data of unlimited length so by definition its ...

CSS Multiple Choice Questions & Answers on CSS3 Animation with Responsive Web Design for Freshers

1. Which of the following css property should be used to make a responsive image? a) floatb) max-widthc) margin-rightd) all of the mentioned Answer: d 2. What should be written in the blank of th...

Java Multiple Choice Questions & Answers on Thread class for Freshers

1. Which of these class is used to make a thread? a) Stringb) Systemc) Threadd) Runnable Answer: c Explanation: Thread class is used to make threads in java, Thread encapsulates a thread of execu...

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