Computer Applications

Which of the following is a valid initialisation statement?

  1. int x = "GOOD";
  2. int y = 45.0/2;
  3. int z = (int)'x';
  4. int m = false ;

Values & Data Types Java

3 Likes

Answer

int z = (int)'x';

Reason

  1. int x = "GOOD";"GOOD" is a String literal, not an integer. We cannot assign a String to a variable of type int.
  2. int y = 45.0/2;45.0 is a double literal, so the result of the division is a double. Assigning a double value to an int without explicit typecasting causes a type mismatch error.
  3. int z = (int)'x'; — This is a valid initialization because the character 'x' is explicitly typecast to an integer, storing its Unicode value in the variable z.
  4. int m = false ;false is a boolean literal. We cannot assign a boolean value to a variable of type int.

Answered By

3 Likes


Related Questions