KnowledgeBoat Logo

Computer Applications

Write the output of the following String methods:

(a) "ARTIFICIAL".indexOf('I')

(b) "DOG and PUPPY".trim().length()

Java

Java String Handling

ICSE Sp 2024

46 Likes

Answer

(a)

3

Working

indexOf() returns the index of the first occurrence of the specified character within the string or -1 if the character is not present. First occurrence of 'I' in "ARTIFICIAL" is at index 3 (a string begins at index 0).

(b)

13

Working

trim() removes all leading and trailing space from the string and length() returns the length of the string i.e., the number of characters present in the string. Thus, the output is 13.

Answered By

22 Likes


Related Questions