Study and learn Java MCQ questions and answers on Type Casting or Type Promotions. To convert from one data type to the other either Implicit or Explicit Type Conversions are required. Attend job interviews easily with these MCQs.
Go through Java Theory Notes on Type Promotions before attempting this test.
Float is bigger than short
double is bigger than float
int a =(int)1.2f; //a holds 1
int a=456; float b = a; //No change of data //b holds 456.0;
All integers are promoted to int or long.
All characters are promoted to int from char or long from char.
All float numbers are promoted to double.
Implicit type conversion is an Automatic Type Promotion from a lower data type to a higher data type.
Number to Number conversions are possible with or without a data loss.
What is the output of the following Java Code? int a=9; float b = a/2; System.out.println(b);
You need to type cast at least one number in that expression to float or double to do real number division.
float b = 9*1f/2; //4.5
What is the output of the below Java code snippet? char ch = 'A';//ASCII 65 int a = ch + 1; ch = (char)a; System.out.println(ch);
ch is promoted to int and 1 is added. int value 66 is again type casted to char type. So out will be the next character of A i.e B.
What is the output of the below Java code snippet? float a = 8.2/2; System.out.println(a);
Add a suffix f or F float a = 8.2f/2; (or) explicit typecast float a = (float)8.2/2; System.out.println(a);
What is the output of the Java code snippet? byte b = 25; b++; b = b+1; System.out.println(b);
Explicit type casting is required. Expression b+1 gives int value byte b = 25; b++; b = (byte)(b+1); System.out.println(b); //OUTPUT = 27
What is the output of the Java code snippet? int a = 260; byte b= (byte)a; System.out.println(b);
If a number is too big for a data type, it applies Modulo Division by the highest number possible of that data type. Byte range is -128 to +127. 260 > 127. So, modulo division is applied.
260%256 = 4
byte Maximum = 256 = (*2^8) short maximum = 2^16 = 65536 int maximum = 2^32 long maximum = 2^64
What is the output of the Java code snippet? short a = (short)65540; System.out.println(a);
65540 is bigger than short range -32768 to +32767. So, 65540 % 2^32 = 65540%65536 = 4
A boolean literal or variable takes only true or false. So, it does not accept numbers for type conversion.
All other operands are type promoted to the highest data type available in that expression. If the highest data type is double in an expression, the final result will also be a double value.