1Z0-803 · Question #236
Given the code fragment: float x = 22.00f % 3.00f; int y = 22 % 3; System.out.print(x + ", "+ y); What is the result?
The correct answer is A. 1.0, 1. This question assesses knowledge of Java's arithmetic modulo operator behavior with both floating-point and integer data types.
Question
Given the code fragment:
float x = 22.00f % 3.00f; int y = 22 % 3; System.out.print(x + ", "+ y); What is the result?
Options
- A1.0, 1
- B1.0f, 1
- C7.33, 7
- DCompilation fails
- EAn exception is thrown at runtime
How the community answered
(36 responses)- A94% (34)
- B3% (1)
- C3% (1)
Why each option
This question assesses knowledge of Java's arithmetic modulo operator behavior with both floating-point and integer data types.
The modulo operator `%` applied to `22.00f % 3.00f` calculates the remainder of the float division, which is `1.0f`. For integers, `22 % 3` yields `1`, the remainder of the integer division. Printing these values results in "1.0, 1".
`1.0f` is the internal float representation, but `System.out.print` typically displays it as `1.0`.
`7.33` and `7` would be the results of division operations, not the modulo (remainder) operation.
The provided code fragment is syntactically correct and will compile without any errors.
There are no operations in the code fragment that would lead to a runtime exception, such as division by zero.
Concept tested: Java arithmetic modulo operator behavior
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html
Topics
Community Discussion
No community discussion yet for this question.