Computer Applications
State the data type and values of a and b after the following segment is executed:
String s1 = "Computer", s2 = "Applications"
a = (s1.compareTo(s2));
b = (s1.equals(s2));
Java String Handling
ICSE 2012
27 Likes
Answer
Data type of a is int and value is 2. Data type of b is boolean and value is false.
Explanation
compareTo() method compares two strings lexicographically and returns the difference between the ASCII values of the first differing characters in the strings. Thus, a will be of int data type and store the value 2 as the difference between ASCII values of 'A' and 'C' is 2.
equals() method compares two String objects and returns true if the strings are same, else it returns false. Thus, b will be of boolean data type and result in false as both the strings are not equal.
Answered By
12 Likes
Related Questions
Write a program in Java to enter any sentence. Also ask the user to enter a word. Print the number of times the word entered is present in the sentence. If the word is not present in the sentence, then print an appropriate message.
Define a class to accept a string and convert the same to uppercase, create and display the new string by replacing each vowel by immediate next character and every consonant by the previous character. The other characters remain the same.
Example:
Input : #IMAGINATION@2024
Output : #JLBFJMBSJPM@2024The output of the statement "CONCENTRATION".indexOf('T') is:
- 9
- 7
- 6
- (-1)