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 following program segment in which the statements are jumbled, choose the correct order of statements to swap two variables using the third variable.
void swap(int a, int b) { a = b; → (1) b = t; → (2) int t = 0; → (3) t = a; → (4) }- (1) (2) (3) (4)
- (3) (4) (1) (2)
- (1) (3) (4) (2)
- (2) (1) (4) (3)
There are 128 set of different characters used in a Java program.
The number of bytes occupied by a character array of four rows and three columns are:
- 12
- 24
- 96
- 48
Which of the following is a valid java keyword?
- If
- BOOLEAN
- static
- Switch