Multiple choice technology programming languages

Identify the invalid assignments.

  1. float f = \u0038;

  2. long L2 = 2L;

  3. float f = 1.2;

  4. char c = '/u004E';

  5. byte b = 100;

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Option C is invalid: 'float f = 1.2;' fails to compile because floating-point literals like 1.2 are double by default. You need either 'float f = 1.2f;' or 'float f = (float)1.2;'. The other options are valid or have different issues (option D has a typo but the question asks for the invalid assignment).

AI explanation

To answer this question, let's go through each option to understand why it is valid or invalid:

Option A) float f = \u0038; - This option is valid. The value \u0038 is the Unicode representation of the character '8', and it can be assigned to a float variable.

Option B) long L2 = 2L; - This option is valid. The 'L' suffix indicates that the literal value 2 is of type long, and it can be assigned to a long variable.

Option C) float f = 1.2; - This option is invalid. The literal value 1.2 is a double, and it cannot be directly assigned to a float variable without an explicit typecast.

Option D) char c = '/u004E'; - This option is invalid. The character literal '/u004E' is incorrect. The correct representation of the Unicode character 'N' should be '\u004E'.

Option E) byte b = 100; - This option is valid. The literal value 100 is within the range of a byte, and it can be assigned to a byte variable.

The correct answer is C. This option is invalid because the literal value 1.2 is a double and cannot be directly assigned to a float variable without an explicit typecast.