Computer Applications
Give the output of the following program segment:
String S = "GRACIOUS".substring(4);
System.out.println(S);
System.out.println("GLAMOROUS".endsWith(S));
Answer
Output
IOUS
false
Explanation
The
substring()method is used to extract a part of a string starting from a specified index. In the given statement,"GRACIOUS".substring(4)extracts characters from index 4 to the end, resulting in"IOUS".The
endsWith()method checks whether a string ends with a specified sequence of characters and returns a boolean value. Here,"GLAMOROUS".endsWith("IOUS")checks if the string ends with"IOUS", which is false, so it prints false.
Related Questions
Write the Java expression to find the sum of cube root of x and the absolute value of y.
Users must be above 10 years to open a self-operated bank account. Write this logic using a ternary operator and store the result (the eligibility message) in a String variable named idStatus and print it.
Give the output of the following program segment and mention how many times the loop is executed.
int K = 1; do {K += 2; System.out.println(K); } while (K <= 6);The following program segment calculates and displays the factorial of a number. [Example : Factorial of 5 is 1 x 2 x 3 x 4 x 5 = 120]
int p, n = 5, f = 0; for(p = n; p > 0; p--) f *= p; System.out.println(f);Name the type of error if any; correct the statement to get the desired output.