Consider the following code segment.
double num = 9 / 4;
System.out.print(num);
System.out.print(” ”);
System.out.print((int) num);
What is printed as a result of executing the code segment?
A 2 2
B 2.0 2
C 2.0 2.0
D 2.25 2
E 2.25 2.0
I chose A
I have absolutely no idea why i chose A, I knew it was B, and i remember distinctly thinking about it still being a double on the first one so it would have a .0, but i chose A. The correct answer is B.
Consider the following code segment.
double x = (int) (5.5 - 2.5);
double y = (int) 5.5 - 2.5;
System.out.println(x - y);
What is printed as a result of executing the code segment?
A -1.0
B -0.5
C 0.0
D 0.5
E 1.0
I chose E. I did the casting wrong. The correct answer is D. (int) (5.5 -2.5) = 3 (int) 5.5 - 2.5 = 5 - 2.5 = 2.5 x - y = 3 - 2.5 = 0.5