Core java Strings Interview Questions
Strings
Q -
Java provides two different string classes from which string objects can be
instantiated. What
are
they?
A -
The two classes are:
String
StringBuffer
Q -
The StringBuffer class is used for strings that are not allowed to change. The
String class is
used
for strings that are modified by the program: True or False. If false, explain
why.
A -
False. This statement is backwards. The String class is used for strings that
are not allowed to change. The
StringBuffer
class is used for strings that are modified by the program.
Q -
While the contents of a String object cannot be modified, a reference to a
String object can be
caused
to point to a different String object: True or False. If false, explain why.
A -
True.
Q -
The use of the new operator is required for instantiation of objects of type
String: True or
False?
If false, explain your answer.
A -
False. A String object can be instantiated using either of the following
statements:
String str1 = new String("String
named str2");
String str2 = "String named
str1";
Q -
The use of the new operator is required for instantiation of objects of type
StringBuffer: True
or
False? If false, explain your answer.
A -
True.
Q -
Provide a code fragment that illustrates how to instantiate an empty
StringBuffer object of a
default
length and then use a version of the append() method to put some data into the
object.
A -
See code fragment below:
StringBuffer str5 = new
StringBuffer();//accept default initial length
str5.append("StringBuffer named
str5");//modify length as needed
Q -
Without specifying any explicit numeric values, provide a code fragment that
will instantiate an
empty
StringBuffer object of the correct initial length to contain the string
"StringBuffer named str6"
and
then store that string in the object.
A -
See the following code fragment:
StringBuffer str6 = new
StringBuffer("StringBuffer named str6".length());
str6.append("StringBuffer named
str6");
0 comments:
Post a Comment