English

What will be the output of the given Java code after the calculation [note the data types]? double mh = Math.sqrt (Math.max (6+3*10, 30−2*5+5)); System.out.println ("MH=" + mh);

Advertisements
Advertisements

Question

What will be the output of the given Java code after the calculation [note the data types]?

double mh = Math.sqrt (Math.max (6+3*10, 30−2*5+5));

System.out.println ("MH=" + mh);

Sum
Advertisements

Solution

(6+3*10):

Multiplication (*) has higher precedence: 3 × 10 = 30.

Addition complete: 6 + 30 = 36.

(30−2*5+5):

Multiplication (*) has higher precedence: 2 × 5 = 10.

Subtraction and addition are evaluated left-to-right: 30 − 10 = 20, then 20 + 5 = 25.

Math.max(36, 25):

This compares 36 and 25 and returns the larger value, which is 36.0 (promoted to a double).

Math.sqrt(36.0):

The square root of 36.0 is calculated.

Math.sqrt() always returns a double, which gives MH = 6.0.

shaalaa.com
  Is there an error in this question or solution?
Chapter 1: Revision of Class 9 Syllabus - Exercises [Page 46]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 1 Revision of Class 9 Syllabus
Exercises | Q 5. (vi) | Page 46
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×