| preferred AP College board partner for AP classes
AP Computer Science A/Unit 2: Using Objects
Start Practice TestPractice Test
Share
About Exam
medium Solved by 1 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 -7, indicating that both expressions produce the same result

B

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

C

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

D

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

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer

Question Leaderboard

Not enough data yet to show leaderboard.

AI Tutor

How can I help?

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