(a) Name one String method which results in positive integer only. (b) Name one String method which results in a character.
Topic: Return types of String methods
Answer
(a) length() — it returns the number of characters in the string, which can never be negative. (b) charAt(int index) — it returns the single character standing at the given position.
The question is really asking you to classify String methods by return type. (a) length() returns an int that is 0 or more, so it is never negative. Note that indexOf() also returns an int, but it returns -1 when the search fails, so it is NOT an acceptable answer here — that is the distinction being tested. (b) charAt() is the only common String method returning a char. Others for comparison: substring() and toUpperCase() return String, equals() and startsWith() return boolean, compareTo() returns int (possibly negative).