Vidyalelo
Java Programming · Q25

Data Types and Variables

Programming · Java Programming · question 25

Q25

Which of the following is not a valid way to declare a long variable in Java?

A.
long x = 123L
B.
long y = 123
C.
long z = 123.0
Answer
D.
long w = 0x1L

Answer: Option C

Solution

Answer: Option C
Solution:
The correct answer is Option C: long z = 123.0.

In Java, the correct way to declare a long variable is by using Option A, Option B, or Option D. However, Option C is not a valid way to declare a long variable because it attempts to assign a double value (123.0) to a long variable without type casting.

Here's the breakdown:

Option A: long x = 123L - This is a valid way to declare a long variable in Java, where x is assigned the long value 123L (note the "L" suffix to indicate a long literal).

Option B: long y = 123 - This is also a valid way to declare a long variable in Java, where y is assigned the long value 123 (Java allows you to assign integer literals to long variables without requiring a suffix).

Option C: long z = 123.0 - This is not a valid way to declare a long variable because it attempts to assign a double value (123.0) to a long variable without type casting. It would result in a compilation error.

Option D: long w = 0x1L - This is a valid way to declare a long variable in Java using hexadecimal notation. w is assigned the long value 1L (note the "L" suffix to indicate a long literal).

So, among the provided options, Option C is not a valid way to declare a long variable in Java because it attempts to assign a double value to a long variable without type casting.