Give output of the following program segment:

double x = 2.9, y = 2.5;
System.out.println(Math.min(Math.floor(x), y));
System.out.println(Math.max(Math.ceil(x), y));

Single Choice
Views 96

Answer:

  • Math.min(Math.floor(x), y)

    • Math.floor(x): This function returns the largest integer less than or equal to x. For x = 2.9, Math.floor(2.9) returns 2.0.
    • Math.min(Math.floor(x), y): This function returns the smaller of the two values: Math.floor(x) and y. Therefore, Math.min(2.0, 2.5) returns 2.0.

    So, System.out.println(Math.min(Math.floor(x), y)); will print 2.0.

  • Math.max(Math.ceil(x), y)

    • Math.ceil(x): This function returns the smallest integer greater than or equal to x. For x = 2.9, Math.ceil(2.9) returns 3.0.
    • Math.max(Math.ceil(x), y): This function returns the larger of the two values: Math.ceil(x) and y. Therefore, Math.max(3.0, 2.5) returns 3.0.

    So, System.out.println(Math.max(Math.ceil(x), y)); will print 3.0.

Summary of Output:


2.0
3.0

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.