Java Programming - Java.lang Class - Discussion
Discussion Forum : Java.lang Class - Finding the output (Q.No. 24)
24.
What will be the output of the program?
String a = "ABCD";
String b = a.toLowerCase();
b.replace('a','d');
b.replace('b','c');
System.out.println(b);
Answer: Option
Explanation:
String objects are immutable, they cannot be changed, in this case we are talking about the replace method which returns a new String object resulting from replacing all occurrences of oldChar in this string with newChar.
b.replace(char oldChar, char newChar);
But since this is only a temporary String it must either be put to use straight away i.e.
System.out.println(b.replace('a','d'));
Or a new variable must be assigned its value i.e.
String c = b.replace('a','d');
Discussion:
5 comments Page 1 of 1.
Prabu said:
1 decade ago
Option A is correct because
a=ABCD immutable means cannot be modified,
b=abcd it means we are converting upper to lower and storing in b
After that replace cannot be work, because immutable means we cannot modified.
a=ABCD immutable means cannot be modified,
b=abcd it means we are converting upper to lower and storing in b
After that replace cannot be work, because immutable means we cannot modified.
Ami said:
1 decade ago
But Why new Object doesn't change their value? if not create object then.
Bedada beyene said:
10 years ago
Why the answer not ABCD OR DCCD?
Vino said:
10 years ago
Why the answer is abcd?
Manoj said:
9 years ago
Because b variable declared first.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers