nerdexam
Oracle

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.

Using Operators and Decision Constructs

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)
  • A
    94% (34)
  • B
    3% (1)
  • C
    3% (1)

Why each option

This question assesses knowledge of Java's arithmetic modulo operator behavior with both floating-point and integer data types.

A1.0, 1Correct

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".

B1.0f, 1

`1.0f` is the internal float representation, but `System.out.print` typically displays it as `1.0`.

C7.33, 7

`7.33` and `7` would be the results of division operations, not the modulo (remainder) operation.

DCompilation fails

The provided code fragment is syntactically correct and will compile without any errors.

EAn exception is thrown at runtime

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

#modulo operator#float#int#arithmetic operators

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice