Short answerEasy2 marks
Write Java expression for: |a + b| / sqrt(a^2 + b^2)
Topic: Math class expressions
Previous-year question practice database
Answer
Exam answer
Math.abs(a + b) / Math.sqrt(a * a + b * b)
Explanation
Translate the mathematics piece by piece.
The modulus bars | a + b | become Math.abs(a + b).
The square root becomes Math.sqrt(...), and a^2 + b^2 is written a * a + b * b (Math.pow works too but returns a double and is clumsier here). The horizontal bar of the fraction becomes the / operator, and the whole denominator must sit inside its own brackets — writing Math.abs(a + b) / Math.sqrt(a * a) + b * b would divide by only part of it. Bracket the denominator whenever it is more than a single term.
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