Short answerModerate2 marks
(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
Previous-year question practice database
Answer
Exam 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.
Explanation
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).
More from String Manipulation
To extract the word NOW from the word "ACKNOWLEDGEMENT", the Java statement "ACKNOWLEDGEMENT".substring(3, ____) is used. Choose the correct number to fill in the blank.2026String a[] = {"Atasi", "Aditi", "Anant", "Amit", "Ahana"};
System.out.println(a[1].charAt(1) + "*" + a[2].charAt(2));
The output of the above statement is:2026Which of the following String methods returns a negative value?2026Assertion (A): The substring() method modifies the original String.
Reason (R): The substring() method can extract part of a String starting from a specific index.2026Give the output of the following program segment:
String S = "GRACIOUS".substring(4);
System.out.println(S);
System.out.println("GLAMOROUS".endsWith(S));2026Name the following:
(a) The return data type of the method equals().
(b) The String method which has no parameter and returns a String.2026