Computer Applications

Consider the following code snippet:

float x = 8.25F;
int	y;
y = (int)x;

What are the values of x and y?

  1. x = 8.25, y = 8
  2. x = 8.0, y = 8.0
  3. x = 8, y = 8.25
  4. x = 8, y = 8

Values & Data Types Java

13 Likes

Answer

x = 8.25, y = 8

Reason — float data type can store numbers with decimal and int data type stores signed/unsigned integer values. So, x = 8.25. Then, the user is declaring an int variable y and type casting the float value x to an int and assigning it to y.

When we cast x to an int, the fractional part is truncated and the int value (8) is stored in y. Thus, y = 8.

Answered By

6 Likes


Related Questions