Computer Applications
Which of the following is a valid Java declaration?
Values & Data Types Java
3 Likes
Answer
int num = 'B';
Reason — Let's analyze each of the options:
1. float x = 12.5; — Incorrect
- A floating-point literal (like
12.5) is by defaultdoublein Java. - To store it in a
floatvariable, explicitly specifyforF:float x = 12.5f; // Corrected version
2. char ch = "A"; — Incorrect
- A
charmust be enclosed in single quotes (''), not double quotes (""). - Correct version:
char ch = 'A';
3. boolean b = 0; — Incorrect
- Java does not allow assigning integers (
0or1) toboolean. - Only
trueorfalseis allowed. - Correct version:
boolean b = false;
4. int num = 'B'; — Correct
- A
charin Java can be assigned to anintbecause characters are internally stored as Unicode values (ASCII-compatible). 'B'corresponds to Unicode 66, sonumwill store 66.
Answered By
2 Likes
Related Questions
Consider the array given below:
char ch[] = {'A','E','I','O', 'U'};Write the output of the following statements:
System.out.println(ch[0]*2);:- 65
- 130
- 'A'
- 0
Which of the following is a valid java keyword?
- If
- BOOLEAN
- static
- Switch
The default value of a boolean variable is:
- False
- 0
- false
- True
There are 128 set of different characters used in a Java program.