Menu
Topics Index
...
`

creating char datatype


If you need explanation Read this topic

If you need Answer Take test on this topic

creating char datatype
What will be the output of the following program?
public class DemoOnChar
{
    public static void main(String args[])
    {
        char ch = 98;
        System.out.print(ch);
    }
}

A. 98
B. B
C. b
D. '98'
E. Compilation Error or Runtime Error
Topic: Character Literals In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans c. char ch is assigned with 98 .sop prints b bcz 98 is ASCII value of b

Posted by Maheshwari Natarajan    2015-01-29 14:17:13


Ans is c character takes ascii value ie ascii value of 98 is 'b' it prints b

Posted by Ramya Reddy Addulla    2015-01-29 15:36:26


ANS IS C (b)

--Here the concept of "TYPE CASTING" is used.

--The int takes 4 bytes of memory and char takes
2 bytes of memory.

--Assignment of 4 bytes of memory to 2 bytes of
memory requires explicit casting. Moreover,
int can take negative values and char takes
only positive values. char is implicitly
unsigned by Designers (note: Java does not
support unsigned data types and infact,
unsigned is not a keyword of Java).

----> byte –> short –> int –> long –> float –> double

--So,Here 98 is considered as ASCII value and it is casted to char
corresponding to 98 i.e. small b.

--Therefore, "b" will get printed.

Posted by Mânïshå Mùlchåndânï    2015-01-29 17:10:14


Ans is Option C... here execution starts from main() method first it will create character variable ch and we are initializing with the value 98 here we are giving integer value to character value.....              here integer range is 32 bits and character range is 16 bits so integer value will be implicitly type cast to character variable and the ASCII code for value 98 is 'b' so    'ch' variable will initialize with the value 'b' so it will print ch value as 'b'... so Ans is Option C.....

Posted by Bhagi Bhagyasri    2015-01-30 04:18:05


This dose is now closed and the winners are Maheshwari Natarajan,  for 'First Correct Comment', Bhagi Bhagyasri,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Maheshwari Natarajan. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-01-30 05:24:08


© meritcampus 2019

All Rights Reserved.

Open In App