120k views
20 votes
4. If d is a double variable with a value of 7.5, which of the following statements results in a (1 point) value of 7 being assigned to x?

int x = (int)d;
int x = d;
int x = int(d);
double x = d;​

User Ivantod
by
4.4k points

1 Answer

11 votes

Answer:

int x = (int)d

Step-by-step explanation:

(int) means the largest integer value smaller than <value>.

which is 7.

Only the first choice has the correct syntax.

The second choice sets an int as a double, which does not work.

The third one shows int as a function, d as a parameter, which is not allowed, as int is a keyword in java

For the last choice, you are setting x to 7.5 not 7.

So int x = (int)d is correct.

User Koeno
by
4.1k points