Java.lang Class

Q1: What will be the output of the program?
String s = 'ABC'; 
s.toLowerCase(); 
s += 'def'; 
System.out.println(s);

A ABC

B abc

C ABCdef

D Compile Error

ANS:A - ABC

String objects are immutable. The object s above is set to 'ABC'. Now ask yourself if this object is changed and if so where - remember strings are immutable. Line 2 returns a string object but does not change the originag string object s, so after line 2 s is still 'ABC'. So what's happening on line 3? Java will treat line 3 like the following: s = new StringBuffer().append(s).append('def').toString(); This effectively creates a new String object and stores its reference in the variable s, the old String object containing 'ABC' is no longer referenced by a live thread and becomes available for garbage collection.



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.