JAVA INTERVIEW QUESTIONS - STRING HANDLING

STRING HANDLING

1.Which package does define String and StringBuffer classes?
Ans: java.lang package.

2.Which method can be used to obtain the length of the String?
Ans : length( ) method.

3.How do you concatenate Strings?
Ans: By using " + " operator.

4.Which method can be used to compare two strings for equality?
Ans : equals( ) method.

5.Which method can be used to perform a comparison between strings that ignores case differences?
Ans : equalsIgnoreCase( ) method.

6.What is the use of valueOf( ) method?
Ans : valueOf( ) method converts data from its internal format into a human-readable form.

7.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.

8.Which method can be used to find out the total allocated capacity of a StrinBuffer?
Ans : capacity( ) method.

9.Which method can be used to set the length of the buffer within a StringBuffer object?
Ans : setLength( ).

10.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.

11.What are wrapper classes?
Ans: Wrapper classes are classes that allow primitive types to be accessed as objects.

12.Which of the following is not a wrapper class?
String
Integer
Boolean
Character
Ans: a.

13.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);
}
}
abcdefabcdef
abcabcDEFDEF
abcdefabcDEF
None of the above
ANS : c.

14.Which of the following methods are methods of the String class?
delete( )
append( )
reverse( )
replace( )
Ans : d.

15.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.

16.String is a wrapper class?
True
False
Ans: b.

17) 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));
Bic
ic
c) icy
d) error: no method matching substring(int,char)
Ans : b.

18) 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?
s3=s1 + s2;
s3=s1 - s2;
c) s3=s1 & s2
d) s3=s1 && s2
Ans : a.

19) Which of the following statements are true?
The String class is implemented as a char array, elements are addressed using the stringname[] convention
b) Strings are a primitive type in Java that overloads the + operator for concatenation
c) Strings are a primitive type in Java and the StringBuffer is used as the matching wrapper type
d) The size of a string can be retrieved using the length property.

Ans: b.
Powered by Blogger.