ValueLabs Java Questions: String Handling

Download PDF of This Page (Size: 134K)


Which package does define String and StringBuffer classes?

Ans: Java. Lang package.

Which method can be used to obtain the length of the String?

Ans: Length () method.

How do you concatenate Strings?

Ans: By using “+” operator.

Which method can be used to compare two strings for equality?

Ans: Equals () method.

Which method can be used to perform a comparison between strings that ignores case differences?

Ans: EqualsIgnoreCase () method.

What is the use of valueOf () method?

Ans: ValueOf () method converts data from its internal format into a human-readable form.

What are the uses of toLowerCase () and toUpperCase () methods?

Ans: The method toLowerCase () converts all the characters in a string from uppercase to lowercase.

The method toUpperCase () converts all the characters in a string from lowercase to uppercase.

Which method can be used to find out the total allocated capacity of a StrinBuffer?

Ans: Capacity () method.

Which method can be used to set the length of the buffer within a StringBuffer object?

Ans: SetLength ().

What is the difference between String and StringBuffer?

Ans: String objects are constants, whereas StringBuffer objects are not.

String class supports constant strings, whereas StringBuffer class supports growable, modifiable strings.

What are wrapper classes?

Ans: Wrapper classes are classes that allow primitive types to be accessed as objects.

Which of the following is not a wrapper class?





Ans: a

What is the output of the following program?

public class Question { public static void main (String args[]) { String s1 = “abc” String s2 = “def” String s3 = s1. Concat (s2. ToUpperCase () ); System. Out. Println (s1 + s2 + s3); } }




None of the above

ANS: c

Which of the following methods are methods of the String class?

delete ()

append ()

reverse ()

replace ()

Ans: d

Which of the following methods cause the String object referenced by s to be changed?

s concat ()

s toUpperCase ()

s replace ()

s valueOf ()

Ans: a and b

String is a wrapper class?



Ans: b

  1. If you run the code below, what gets printed out? String s = new String ( “Bicycle” ); int iBegin = 1; char iEnd = 3; System. Out. Println (s. Substring (iBegin, iEnd) )

    1. Bic

    2. ic

    3. icy

    4. error: No method matching substring (int, char)

    Ans: b

  2. Given the following declarations String s1 = new String ( “Hello” ) String s2 = new String ( “there” ); String s3 = new String (); Which of the following are legal operations?

    1. s3 = s1 + s2

    2. s3 = s1 − s2

    3. s3 = s1 & s2

    4. s3 = s1 && s2

    Ans: a

  3. Which of the following statements are true?

    1. The String class is implemented as a char array, elements are addressed using the stringname[] convention

    2. Strings are a primitive type in Java that overloads the + operator for concatenation

    3. Strings are a primitive type in Java and the StringBuffer is used as the matching wrapper type

    4. The size of a string can be retrieved using the length property.

Ans: b