Searching for "StringBuffer"

Q:

In the following pieces of code, B and D will compile without any error. True or false ?

A: StringBuffer sb1 = "abcd";

B: Boolean b = new Boolean("abcd");

C: byte b = 255;

D: int x = 0x1234;

E: float fl = 1.2;

A) TRUE B) FALSE
Answer & Explanation Answer: A) TRUE

Explanation:

The code segments B and D will compile without any error. A is not a valid way to construct a StringBuffer, you need to create a StringBuffer object using "new". B is a valid construction of a Boolean (any string other than "true" or "false" to the Boolean constructor will result in a Boolean with a value of "false"). C will fail to compile because the valid range for a byte is -128 to +127 (i.e., 8 bits, signed). D is correct, 0x1234 is the hexadecimal representation in java. E fails to compile because the compiler interprets 1.2 as a double being assigned to a float (down-casting), which is not valid. You either need an explicit cast, as in "(float)1.2" or "1.2f", to indicate a float.

Report Error

View Answer Workspace Report Error Discuss

Q:

 What is the string contained in s after following lines of code?

StringBuffer s = new StringBuffer(“Hello”); s.deleteCharAt(0);  ?

A) llo B) Hllo
C) ello D) H
 
Answer & Explanation Answer: C) ello

Explanation:

deleteCharAt() method deletes the character at the specified index location and returns the resulting StringBuffer object.

So after deleting the character at 0 ie 'H', the string returns 'ello' as the output.

Report Error

View Answer Report Error Discuss

Filed Under: Java
Job Role: Analyst , IT Trainer

Q:

Which of the following are incorrect form of StringBuffer class constructor  ?

A) StringBuffer(int size , String str) B) StringBuffer(int size)
C) StringBuffer(String str) D) StringBuffer()
 
Answer & Explanation Answer: A) StringBuffer(int size , String str)

Explanation:
Report Error

View Answer Report Error Discuss

Filed Under: Java
Job Role: Database Administration , IT Trainer

Q:

What is different among String, StringBuffer, StringBuilder?

Answer

String class objects are immutable objects and they represents strings( sequence of characters)


StringBuffer class objects are mutable objects representing strings and they are Thread safe


StringBuild class objects are also mutable objects representing strings and they are not Thread safe

Report Error

View answer Workspace Report Error Discuss

Subject: Java