Computer Applications
What will be the output of the following program snippet?
str1 = "AMAN";
str2 = "AMIT";
System.out.println(str1.compareTo(str2));
- 8
- 0
- -8
- 2
Answer
-8
Reason — compareTo() returns a negative value if the first string is smaller than the second string in terms of the ASCII values of the corresponding characters.
"AMAN" and "AMIT" differ at the third character with 'A' and 'I', respectively. So, output of compareTo()
method will be ASCII Code of 'A' - ASCII Code of 'I' ⇒ 65 - 73 ⇒ -8.
Related Questions
Which of the following functions is used to remove leading and trailing white spaces from the string?
- trim( )
- trail( )
- truncate( )
- slice( )
What will be the output of the following program snippet?
s1 = "COMPUTER";
s2 = "computer";
System.out.println(s1.equals(s2));- True
- False
- 0
- 1
Which of the following packages contain string functions?
- java.awt
- java.string
- java.math
- java.lang
Fill in the blanks:
_________ method is used to join two strings.