Java OCA OCP Practice Question 2367

Question

Given:

int myChar = 97; 
int yourChar = 98; 
System.out.print((char)myChar + (char)yourChar); 

int age = 20; 
System.out.print(" "); 
System.out.print((float)age); 

What is the output?

  • a 195 20.0
  • b 195 20
  • c ab 20.0
  • d ab 20
  • e Compilation error
  • f Runtime exception


a

Note

When a char primitive data type is used as an operand to arithmetic operators, its corresponding ASCII value is used in the arithmetic operation.

Though (char)myChar explicitly casts int variable myChar to char type,

its value 97 is used in the arithmetic operation.

When literal value 20 is explicitly cast to a float type, it outputs its value as a decimal number, that is, 20.0.




PreviousNext

Related