Discussion
What will be the output of the program?
public class Listing17
{
public static void main(String[] args)
{
long up = Math.round(2.5);
long down = Math.round(-2.5);
System.out.println(up + " " + down);
}
}
Answer: A
Math.round adds one half and takes the floor, so a value exactly halfway always moves toward positive infinity. That sends 2.5 up to 3 and -2.5 up to -2. Expecting symmetry around zero is the trap; only the positive case matches the everyday rule.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development