LIWA

Mathematical Library Methods

ICSE Class 9 · Computer Applications · Chapter 7

What this chapter covers

Key definitions

java.lang is the package that Java imports automatically into every program, which is why it is called the default package.

Key points

Questions answered

Name the package that contains the `Math` class. Why is no `import` statement needed to use it?
The `Math` class is in the `java.lang` package. This package is imported automatically into every Java program, so it is called the default package and needs no `import` statement.
Why are the methods of the `Math` class called with the class name, as in `Math.sqrt(x)`, without creating an object?
They are static methods, which belong to the class itself. A static method is called using the class name followed by a dot, so no object of the `Math` class is needed.
Write the output: (a) `Math.pow(3, 4)` (b) `Math.sqrt(0.64)` (c) `Math.cbrt(-125)` (d) `Math.sqrt(-9)`
(a) `81.0` (b) `0.8` (c) `-5.0` (d) `NaN`, because a negative number has no real square root.
Give the output of `Math.ceil(x)`, `Math.floor(x)`, `Math.round(x)` and `Math.rint(x)` when x = -3.5.
`Math.ceil(-3.5)` is `-3.0`; `Math.floor(-3.5)` is `-4.0`; `Math.round(-3.5)` is `-3`, the floor of -3.0; `Math.rint(-3.5)` is `-4.0`, because -4 is the even neighbour.
State the return type of: (a) `Math.round(5.7)` (b) `Math.round(5.7f)` (c) `Math.abs(-12)` (d) `Math.ceil(5.2)`
(a) `long` (b) `int` (c) `int` (d) `double`.
What is the difference between `Math.round()` and `Math.rint()`? Give one value for which they differ.
Both return the nearest whole number. At an exact halfway value, `round()` goes to the larger number and returns a `long` or `int`, while `rint()` goes to the even number and returns a `double`. For 2.5, `Math.round(2.5)` is `3` but `Math.rint(2.5)` is `2.0`.
Write the output: `System.out.println(Math.max(-7, Math.min(-2, -5)) + Math.abs(-3.0));`
`Math.min(-2, -5)` is -5; `Math.max(-7, -5)` is -5; `Math.abs(-3.0)` is 3.0. The sum is -2.0, so the output is `-2.0`.
Write a Java expression that gives a random whole number from 20 to 30, both included.
`(int)(Math.random() * 11) + 20`, because there are 30 - 20 + 1 = 11 whole numbers in the range.

Past board exam questions

ICSE 2027 sample paper · 1 mark

Which Math method accepts a double argument and returns an Integer?

  1. ceil
  2. floor
  3. random
  4. round
Show answer
round

ICSE 2027 sample paper · 1 mark

The output of the Java statement Math.addExact(7, 9); is ____________.

  1. 15
  2. 16
  3. 17
  4. 18
Show answer
16