Computer Applications
The output of the following code is:
System.out.println(Math.ceil(6.4) + Math.floor(-1-2));
- 3.0
- 4
- 3
- 4.0
Answer
4.0
Reason — Let's evaluate the expression step by step:
System.out.println(Math.ceil(6.4) + Math.floor(-1 - 2));
Step 1: Evaluate Math.ceil(6.4)
- The
ceil
method rounds a number up to the nearest integer. Math.ceil(6.4)
→7.0
(asceil
returns adouble
).
Step 2: Evaluate -1 - 2
-1 - 2 = -3
Step 3: Evaluate Math.floor(-3)
Math.floor(-3)
: Thefloor
method rounds a number down to the nearest integer.Math.floor(-3)
→-3.0
(asfloor
also returns adouble
).
Step 4: Add the Results
Math.ceil(6.4)
+Math.floor(-3)
→7.0 + (-3.0)
- Result =
4.0
Related Questions
Identify the operator that gets the highest precedence while evaluating the given expression:
a + b % c * d - e
- +
- %
- -
- *
Which of the following is a valid java keyword?
- If
- BOOLEAN
- static
- Switch
Which of the following returns a String?
- length()
- charAt(int)
- replace(char, char)
- indexOf(String)
Which of the following is not true with regards to a switch statement?
- checks for an equality between the input and the case labels
- supports floating point constants
- break is used to exit from the switch block
- case labels are unique