0
what will be the output of the following code?
char x='A'; int m; m=(x=='aa'?'A':'a'); system.out.println("m="+m);
6 Respuestas
+ 1
Anhjje
Good thinking !
Though, about the problems, I'm not that sure.
For the first one, it may indeed cause an error, but for the second one, every char has an int value associated with it (based on the ASCII table) and thus it may not cause any problem at all as a char can easily be converted into an int. To be sure, I'd cast explicitly the char into an int
-> m = (int)(the ternary operator -> ? : )
0
Pseudocode:
Line 1 initialize variable of char datatype, call it x and store character A inside
Line 2 store a new variable of type integer without initialization
Line 3 store the result of ternary operation in variable m which holds an int value
If x is 'aa', hold character 'A' inside m , otherwise hold 'aa'
Last 4 prints out string m= and the result stored in m
Problem 1:
Char only stores one character, so 'aa' will result in an error
Problem 2:
Storing char in int variable
0
ThewyShift you are right! The result would be 97
Thanks
0
i do not understand the answer please explain the answer though the answer is correct 97
0
Each character or symbol you type (a, b, ?, 5, x) has an associated ASCII number in the system
a ASCII value is 97
A ASCII value is 65
D ASCII value is 68
Etc
0
Aman Kumar
The answer is linked to the way we actually list all the characters that exists
For that, tables were created.
One common table that is used by default by programming language to list char values is the ASCII table. It lists all English letters, as well as ponctuation and spacing (space, new line, ...)
As more and more symboles are created and more languages are being supported (Chinese for example), bigger tables were created like UTF-8 and now UTF-16. While ASCII was designed for English people only, those new tables aim to be more complete and universal.
There's also differences as more usage depending on the table you refer to (8 bits for ASCII and UTF-8 and 16 bits for UTF-16)