replace() compareTo() – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Exam Questions-ICSE2018-03G #JAVA#4063    siteicon   siteicon  

Problem Statement - replace() compareTo()

Give the output of the following string functions:
(i) “ACHIEVEMENT”.replace(‘E’,’A’)
(ii)”DEDICATE”.compareTo(“DEVOTE”)

Solution

TC++ #4063

The outputs will be as follows
“ACHIEVEMENT”.replace(‘E’,’A’) will yield
ACHIAVAMANT   //All occurances of character ‘E’ will be replaced by character ‘A’

“DEDICATE”.compareTo(“DEVOTE”) will yield
-18         //The string do not compare and the comparison fails at third character D. Since D is smaller than V the value will be negative. also the difference between ASCII of V(86) and D(68) which is 18 would be printed in its negative form.


Share

sunmitra| Created: 25-Mar-2018 | Updated: 12-Apr-2019|ICSE2018






Back