Computer Applications
Give the output of the following:
(a) "ROSE".compareTo("ROJA")
(b) "DEDICATE".replace('D', 'L')
Java
Java String Handling
4 Likes
Answer
(a)
9
Working
The method compareTo() compares two strings lexicographically by evaluating the Unicode value of each character from left to right until a mismatch is found.
- 'R' (82) vs 'R' (82) → same.
- 'O' (79) vs 'O' (79) → same.
- 'S' (83) vs 'J' (74) → difference = 83 - 74 = 9.
Since a mismatch occurs at the third character, the comparison stops; therefore, the characters 'E' and 'A' are not compared.
Hence, the method returns 9.
(b)
LELICATE
Working
The replace('D', 'L') method substitutes every occurrence of the character 'D' in the string "DEDICATE" with 'L', resulting in "LELICATE".
Answered By
2 Likes
Related Questions
Give the output of the following program segment and mention how many times the loop is executed.
int k = 100; while (k>=10) { System.out.println(k); k-=40; }Consider the given array and answer the questions given below:
int z[ ] [ ] = { {2, 3, 4}, {5, 1, 2}, {7, 9, 3}};(a) What is the order of the array z[ ][ ]?
(b) What is the value in z[2][0]?
Consider the following array and answer the questions given below:
char ch [ ] = { ‘A’, ‘%’, ‘y’, ‘@’, ‘7’, ‘p’};(a) How many bytes does the array occupy?
(b) What is the output of the statement Character.isDigit(ch[4])?
class perform { int m; String name; perform(int x, String y) { m=x; name=y; } void print() { System.out.print(name + " " + m); } public static void main() { perform ob1=new perform(95 , "Xavier"); ob1.print(); } }(a) Give the output of the code given above.
(b) Name the type of the constructor used.