Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

which of the following assignment statements is illegal? a short s = 10…

Question

which of the following assignment statements is illegal? a short s = 10; b float f = -34; c int t = 23; d int t = 4.5;

Explanation:

Brief Explanations
  • Option A: Assigning an integer literal (10) to a short variable is legal as 10 is within the range of short (usually -32768 to 32767).
  • Option B: Assigning an integer literal (-34) to a float variable is legal because Java (or similar languages) allows implicit widening from int to float.
  • Option C: Assigning an integer literal (23) to an int variable is legal.
  • Option D: Assigning a floating - point literal (4.5) to an int variable is illegal. In languages like Java, you can't implicitly convert a float (or double) to an int without a cast, as it would involve a loss of precision (the decimal part).

Answer:

D. int t = 4.5;