Computer Applications
What will be the output of the following code?
char x = 'A';
int m;
m = (x == 'a')? 'A' : 'a';
System.out.println("m = " + m);
Java
Values & Data Types Java
ICSE 2010
82 Likes
Answer
m = 97
Working
The condition x == 'a' is false as X has the value of 'A'. So, the ternary operator returns 'a' that is assigned to m. But m is an int variable not a char variable. So, through implicit conversion Java converts 'a' to its numeric ASCII value 97 and that gets assigned to m.
Answered By
27 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.
int x = 98; char ch = (char)x; what is the value in ch?
- b
- A
- B
- 97