Short answerModerate2 marks
Write the return data type of the following functions: (i) startsWith() (ii) random()
Topic: Return types of library methods
Previous-year question practice database
Answer
Exam answer
(i) boolean (ii) double
Explanation
(i) startsWith() answers whether a string begins with the given text — a yes-or-no question, so boolean. The same holds for endsWith(), equals() and the Character is... methods. (ii) Math.random() returns a double in the range 0.0 up to (but not including) 1.0. To obtain a whole number in a range you scale and cast, as in (int)(Math.random() * 100) for 0 to 99. Return types are asked every year — group the methods by what they answer rather than memorising a list.
More from Library Classes
What is the output of the statement Math.pow(36, 6/5);?2026The output of the statement:
System.out.println(Character.toUpperCase('b') + 2); is:2026What is the type of parameter to be given for the method parseInt()?2026Write the Java expression to find the sum of cube root of x and the absolute value of y.2026Character class methods are found in the package called:2025The output of Math.max(-7, Math.min(-5, -4)) is2025