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
Sam executes the following program segment and the answer displayed is zero irrespective of any non zero values are given. Name the error. How the program can be modified to get the correct answer?
void triangle(double b, double h) { double a; a = 1/2 * b * h; System.out.println("Area=" + a); }
How many times will the following loop execute? What value will be returned?
int x = 2; int y = 50; do{ ++x; y -= x++; } while(x <= 10); return y;
Name any two jump statements.
Predict the output of the following code snippet:
String a = "20"; String b = "23"; int p = Integer.parseInt(a); int q = Integer.parseInt(b); System.out.print(a + "*" + b);