14 Which of the following are legal statements?

  1. a float f=1/3;

  2. b int i=1/3;

  3. c float f=1.01;

  4. d double d=999d;


Correct Option: A,B,D

AI Explanation

To determine which of the statements are legal, we need to consider the syntax rules for variable declaration and assignment in programming languages.

Let's go through each option to understand whether it is a legal statement or not:

Option A) a float f=1/3; - This statement is legal. It declares a variable "f" of type float and assigns it the value 1/3. In many programming languages, the division operation between two integers would result in an integer value, but since "f" is of type float, the result will be a floating-point number.

Option B) b int i=1/3; - This statement is not legal. It attempts to declare a variable "i" of type int and assigns it the value 1/3. Similar to option A, the division operation would result in an integer value, but the variable type is int, which cannot store decimal values.

Option C) c float f=1.01; - This statement is legal. It declares a variable "f" of type float and assigns it the value 1.01. The value assigned is a valid floating-point number.

Option D) d double d=999d; - This statement is legal. It declares a variable "d" of type double and assigns it the value 999d. The "d" suffix indicates that the value is of type double.

Therefore, the correct options are A, B, and D.

Find more quizzes: