Computer Applications
Answer
length()
Reason — Analyzing the given options:
1. lastIndexOf(char ch)
:
- Returns the last index of the specified character in the string.
- If the character is not found, it returns
-1
, which can be less than0
. - Not always greater than or equal to 0.
2. length()
:
- Returns the number of characters in the string.
- This is always greater than or equal to 0, as an empty string has a length of
0
.
3. compareTo(String s)
:
- Compares two strings lexicographically.
- Can return negative, 0, or positive values, depending on the comparison result.
- Not always greater than or equal to 0.
4. equalsIgnoreCase(String s)
:
- Returns a boolean value (
true
orfalse
), not a numeric value.
Related Questions
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@2024Consider the following program segment in which the statements are jumbled, choose the correct order of statements to check if a given word is Palindrome or not.
boolean palin(String w) { boolean isPalin; w = w.toUpperCase(); int l = w.length(); isPalin = false; // Stmt (1) for (int i = 0; i < l / 2; i++) { char c1 = w.charAt(i), c2 = w.charAt(l - 1 - i); // Stmt (2) if (c1 != c2) { break; // Stmt (3) isPalin = true; // Stmt (4) } } return isPalin; }
Which of the following returns a String?
- length()
- charAt(int)
- replace(char, char)
- indexOf(String)
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.