Multiple choice general knowledge science & technology

Which of the following lines will compile without warning or error:

  1. float f = 1.3;

  2. byte b = 257;

  3. int i = 10;

  4. boolean b = null;

  5. char c = "a";

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

Only 'int i = 10' compiles without error. 'float f = 1.3' fails because 1.3 is a double literal requiring casting or 'f' suffix. 'byte b = 257' fails because 257 exceeds byte's range (-128 to 127). 'boolean b = null' fails because booleans can't be null. 'char c = "a"' fails because "a" is a String, not a char.

AI explanation

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

Option A) float f = 1.3; - This option will compile without error because the value 1.3 is a valid floating-point literal, and it can be assigned to a variable of type float.

Option B) byte b = 257; - This option will not compile because the value 257 is outside the range of the byte data type. The byte data type can only hold values from -128 to 127.

Option C) int i = 10; - This option will compile without error because the value 10 is a valid integer literal, and it can be assigned to a variable of type int.

Option D) boolean b = null; - This option will not compile because the boolean data type can only hold the values true or false. It cannot hold the value null.

Option E) char c = "a"; - This option will not compile because the value "a" is a string literal enclosed in double quotes, and it cannot be assigned to a variable of type char. To assign a character literal, you should enclose it in single quotes ('a').

The correct answer is: C) int i = 10. This option will compile without warning or error because the value 10 is a valid integer literal, and it can be assigned to a variable of type int.