Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 2: Using Objects
Start Practice TestPractice Test
About Exam
medium Solved by 2 students

Type Casting and Truncation

< Prev
Next >

Consider the following code segment:

double value = -7.8;
int rounded1 = (int)(value + 0.5);
int rounded2 = (int)(value - 0.5);
System.out.println(rounded1 + " " + rounded2);

What is the output of this code, and what does it demonstrate about the behavior of casting from double to int?

A

-7 -8, demonstrating that casting always truncates toward zero rather than rounding

B

-8 -8, proving that casting rounds negative numbers down

C

-8 -7, showing that the rounding formulas work correctly for negative numbers

D

-7 -7, indicating that both expressions produce the same result

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question
Mark for review

Question Leaderboard

Not enough data yet to show leaderboard.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|