Computer Applications
What will be the output of the following code snippet when combined with suitable declarations and run?
StringBuffer city = new StringBuffer("Madras");
StringBuffer string = new StringBuffer( );
string.append(new String(city));
string.insert(0, "Central ");
String.out.println(string);
Java
Java String Handling
2 Likes
Answer
The given snippet will generate an error in the statement — String.out.println(string);
as the correct statement for printing is — System.out.println(string);
. After this correction is done, the output will be as follows:
Central Madras
Working
Statement | Remark |
---|---|
StringBuffer city = new StringBuffer("Madras"); | It creates a StringBuffer objectcity which stores "Madras". |
StringBuffer string = new StringBuffer( ); | It creates an empty StringBuffer object string . |
string.append(new String(city)); | It adds the value of city i.e. "Madras" at the end of the StringBuffer object string . string = " Madras". |
string.insert(0, "Central "); | It modifies StringBuffer object string and adds "Central" at 0 index or beginning. Now, string = "Central Madras". |
System.out.println(string); | It prints "Central Madras". |
Answered By
1 Like
Related Questions
If:
String x = "Computer"; String y = "Applications";
What do the following functions return?
(i) System.out.println(x.substring(1,5));
(ii) System.out.println(x.indexOf(x.charAt(4)));
(iii) System.out.println(y + x.substring(5));
(iv) System.out.println(x.equals(y));
What will be the output for the following program segment?
String s = new String ("abc"); System.out.println(s.toUpperCase( ));
State the method that:
(i) converts a string to a primitive float data type.
(ii) determines if the specified character is an uppercase character.
Give the output of the following program :
class MainString { public static void main(String[ ] args) { StringBuffer s = new StringBuffer("String"); if((s.length( ) > 5) && (s.append("Buffer").equals("X"))) ; // empty statement System.out.println(s); } }