Java.lang Class

Q1: What will be the output of the program?
public class Test138 
{ 
    public static void stringReplace (String text) 
    {
        text = text.replace ('j' , 'c'); /* Line 5 */
    } 
    public static void bufferReplace (StringBuffer text) 
    { 
        text = text.append ('c');  /* Line 9 */
    } 
    public static void main (String args[]) 
    { 
        String textString = new String ('java'); 
        StringBuffer textBuffer = new StringBuffer ('java'); /* Line 14 */
        stringReplace(textString); 
        bufferReplace(textBuffer); 
        System.out.println (textString + textBuffer); 
    } 
}

A java

B javac

C javajavac

D Compile error

ANS:A - java

A string is immutable, it cannot be changed, that's the reason for the StringBuffer class. The stringReplace method does not change the string declared on line 14, so this remains set to 'java'. Method parameters are always passed by value - a copy is passed into the method - if the copy changes, the original remains intact, line 5 changes the reference i.e. text points to a new String object, however this is lost when the method completes. The textBuffer is a StringBuffer so it can be changed. This change is carried out on line 9, so 'java' becomes 'javac', the text reference on line 9 remains unchanged. This gives us the output of 'javajavac'



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.