ExamVeda
Login
Home
Data Types and Variables
?

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

Answer & Solution
Correct Answer: Option D
The correct answer is Option D: char w = 0x0041.

In Java, Options A, B, and C are valid ways to declare a char variable, but Option D is not.

Here's the breakdown:

Option A: char x = 'A' - This is a valid way to declare a char variable in Java and initialize it with the character 'A'.

Option B: char y = 'n' - This is a valid way to declare a char variable in Java and initialize it with the character 'n'.

Option C: char z = 65 - This is a valid way to declare a char variable in Java and initialize it with the decimal value 65, which corresponds to the character 'A' in the Unicode character set.

Option D: char w = 0x0041 - This is not a valid way to declare a char variable in Java. It attempts to assign a hexadecimal value (0x0041) to a char variable, which is not allowed without type casting.

So, the correct options for declaring a char variable in Java are Options A, B, and C, while Option D is not valid.
Examveda
Question posted by Examveda
Community

Join the Discussion

No comments yet Be the first to discuss this question.