Computer Applications
What is the output of the statement given below?
"ANGER".compareTo("ANGEL")
- 3
- -6
- 6
- 0
Answer
6
Reason — The compareTo() method compares two strings lexicographically by comparing their characters one by one. It returns the difference between the first pair of characters that differ in the two strings.
Here, comparing "ANGER" and "ANGEL":
- Characters
'A','N','G', and'E'are the same in both strings at positions 0 to 3. - At position 4,
'R'(ASCII value 82) in"ANGER"is compared with'L'(ASCII value 76) in"ANGEL". - The difference is
82 - 76 = 6.
Thus, the output is 6.
Related Questions
Consider the program segment:
int p = 0; for(p = 4; p > 0; p -= 2); System.out.print(p); System.out.println(p);The above statement will display:
- 42
- 4200
- 0
0 - 00
The output of the below statement is:
System.out.println("I said, \"It\'s wise to obey elders.\"");- I said, 'It is wise to obey elders.'
- I said, "It's wise to obey elders."
- I said, It's wise to elders.
- "'It's wise to obey elders.'"
Consider the following program segment in which the statements are jumbled.
Choose the correct order of statements to calculate and return the factorial of 4.for(k = 1; k <= 4; k++) → 1 return fa; → 2 long fa = 1, k; → 3 fa *= k; → 4- 1, 2, 3, 4
- 3, 1, 4, 2
- 3, 1, 2, 4
- 1, 3, 2, 4
Write the Java expression to find the product of square root of P and the square root of Q using the methods of Math class.