MCQModerate1 mark
1
Choose the correct answers to the questions from the given options. (Do not copy the questions, write only the correct answers.)
What will be the output of the following code? System.out.println("Lucknow".substring(0, 4));
Topic: substring() with two arguments
Previous-year question practice database
Try it
Pick an option and check your answer. The full worked answer is below either way.
Answer
Exam answer
(c) Luck.
Explanation
substring(0, 4) starts AT index 0 and stops JUST BEFORE index 4, so it takes indices 0, 1, 2 and 3 — L, u, c, k — giving "Luck". The second argument is exclusive, which is why the result has 4 - 0 = 4 characters. Note also that no String method changes the case, so option (d) can be ruled out at once.
In words
Luck.
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