Type Casting in Java or Type Conversion or Type Promotions Tutorial and MCQ
Table of Contents
Study and learn Type Casting in 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.
Type Casting in Java
Java allows developers to work with different types of data.and Java allows conversion of numeric data of type byte, short, char, int, long, float and double from one type to another type.
Automatic Type Conversion refers to both Automatic Type Promotions and Type Casting. Type Promotion is a Widening Conversion while Type Casting is a Narrowing Conversion. A widening conversion is also called Upward Casting and Narrowing Conversion is also called a Downward Casting.
Type Promotions or Type Conversions in java
Implicit type conversion of a variable from one data type to another data type is called Type Promotion. Since it is a Type Promotion, the value of a variable is promoted to a compatible higher data type. This type of conversion is also called a Widening Conversion.
Note 1: Type Promotion is a Lossless Conversion. Data is not truncated.
Note 2: Some developers call this Automatic Type Conversion as Automatic Type Casting or Implicit Type Casting. Explicit Type casting or conversion is simply called Type Casting.
Numeric arithmetic expressions are automatically promoted to a higher data type provided the following two conditions or Rules are satisfied.
- Target data type should be compatible with the source data type.
- Target data type should be bigger in size than the source data type.
Compatible Data Types
All numeric data types are compatible for either Widening Conversion or Narrowing Conversion. The list is below.
- byte
- char
- short
- int
- long
- float
- double
Incompatible Data Types
Numeric to Non-numeric data types are incompatible for either Widening Conversion or Narrowing Conversions. The list is below.
- boolean
- byte, char, short, int, long, float, double
Automatic Type Promotion Scenarios
If one of the operands is of type long, the whole arithmetic expression gives a long value. If one of the operands is of type float, the whole arithmetic expression gives a float value. Lastly, if one of the operands is of type double, the whole arithmetic expression gives a double value.
Here is the list of possible Scenarios of how a data type is promoted automatically.
- byte to int
- char to int
- short to int
- int to long
- byte to long
- char to long
- short to long
- byte to float
- char to float
- short to float
- int to float
- byte to double
- char to double
- short to double
- int to double
- float to double
1 Example
class TypePromotion1 { public static void main(String args[]) { short a = 120; byte b = 50; //120*50 is promoted to int automatically int c = a*b; System.out.println(c); int a = 4; byte d = a * 5; //ERROR //a*5 is converted to int //int to byte conversion is not automatic } }
2 Example
class TypePromotion2 { public static void main(String args[]) { short a = 120; float b = 50.0f; //120*50.0f is promoted to float automatically float c = a*b; System.out.println(c); float d = 65; //65 is converted to float } }
3 Example
class TypePromotion3 { public static void main(String args[]) { short a = 120; int b = 50; double c = 1.0; //120*50*1.0 is promoted to double automatically //short * int * 1.0 == double double e = a*b*c; System.out.println(c); double f = 35*1.0; //35*1.0 => int * double == double System.out.println(f); double g = 40; //40 is converted to double } }
Type Casting in Java
Type Casting is nothing but manually casting or converting an operand or constant or the value of an expression from one data type to another. java Type Casting is explicit in nature and it is a Narrowing Type Conversion.
Note: Type Casting is a Lossy Conversion. Data is TRUNCATED
You should check compatibility before casting the data (constant, value of expression) to another data type. Check the above list of Compatible and Incompatible data types under Type Promotions.
Numeric arithmetic expressions are manually demoted to a lower data type provided the following two conditions or Rules are satisfied.
- The target data type is compatible with the source data type.
- The target data type is smaller in size than the source data type.
Syntax of Type Casting
Syntax for explicit type casting includes Parentheses surrounding a Target Data Type and an immediately following Source Data Type.
(Target Data Type)Source Data Type Example: float a = 34.56; int b = (int)a;
1.Example
Type casting from int to short, int to byte, int to char and char to int are shown below.
class TypeCasting1 { public static void main(String args[]) { int a = 30; //int to short type casting short b = (short)a; //int to byte byte c = (byte)a; //short to byte byte d = (byte)b; //int to char int num = 65; char ch = (char)num; System.out.println(ch); //prints A //char to int char ch2 = 'B'; int num2 = ch2; System.out.println(num2); //prints 66; } }
2. Example – Real World Example for Software Developers
Below example demonstrates how to retain only 2 digits of precision. You can use Format Specifiers to print only 2 digits after precision. But, the variable holds all the digits.
class TypeCasting2 { public static void main(String args[]) { //GOAL : PRINT : 21.42 int a = 150; int b = 7; //150/7 = 21.428572 float c = a/b; printf(c); //Unfortunately Output: 21.0 //TYPE PROMOTION //1.0f promotes int a to float value float d = (1.0f * a)/b; // 150.0/7 d = d* 100; //2142.8572 //TYPE CASTING d = (int)d; //lossy conversion d = 1.0f * d / 100; // 2142.0/100 System.out.println(d); //varible d holds only two precision digits //Output: 21.42 } }
This is how Java language implements Type Casting or Type Conversions which are essential to handle different types of input data
[WpProQuiz 55]
Type Casting in JAVA MCQ (Important)
1) What are the Type Conversions available in Java language?
A) Narrowing Type Conversion
B) Widening Type Conversion
C) A and B
D) None of the above
Answer [=] C
2) What is a higher data type in Java language?
A) A data type which holds more data than other data types
B) A data type whose size is more than other data types
C) A data type which can hold more precision digits than other data types
D) All the above
Answer [=] D
Explanation:
Float is bigger than short
double is bigger than float
3) What is a Widening Type Conversion in Java?
A) Conversion of data from higher data type to lower data type
B) Conversion of data from lower data type to higher data type
C) Conversion of data from any data type to any data type
D) None of the above
Answer [=] B
4) What is a Narrowing Type Conversion in Java?
A) Conversion of data from lower data type to higher data type
B) Conversion data from a higher data type to a lower data type
C) Conversion of data from any data type to any data type
D) None of the above
Answer [=] B
5) What is the result of a Narrowing type conversion?
A) Loss of data
B) Addition of data
C) Corruption of data
D) None of the above
Answer [=] A
Explanation:
int a =(int)1.2f; //a holds 1
6) What is the result of a Widening Type Conversion in Java?
A) Loss of data
B) Gain of data
C) No change
D) None of the above
Answer [=] C
Explanation:
int a=456; float b = a; //No change of data //b holds 456.0;
7) Type promotion in Java usually refers to ____.
A) Narrowing Type Conversion
B) Widening Type Conversion
C) No Type Conversion
D) None of the above
Answer [=] B
Explanation:
All integers are promoted to int or long.
And All characters are promoted to int from char or long from char.
All float numbers are promoted to double.
8) Type Casting in Java usually refers to ____?
A) Narrowing Type Conversion
B) Widening Type Conversion
C) No Type Conversion
D) None of the above
Answer [=] A
9) Explicit Type Conversion in Java refers to ___?
A) Narrowing Type Conversion
B) Widening Type Conversion
C) No Type Conversion
D) None of the above
Answer [=] A
10) Implicit Type Conversion in Java is also called ___?
A) Narrowing Type Conversion
B) Widening Type Conversion
C) No Type Conversion
D) None of the above
Answer [=] B
Explanation:
Implicit type conversion is an Automatic Type Promotion from a lower data type to a higher data type.
11) Which are the compatible Data Types for Type Promotion or Type Casting?
A) byte, char, short
B) char, int, float
C) float, long, double
D) All the above
Answer [=] D
Explanation:
Number to Number conversions are possible with or without a data loss.
12)
What is the output of the following Java Code? int a=9; float b = a/2; System.out.println(b);
A) 4.0
B) 4.5
C) 5.0
D) None of the above
Answer [=] A
Explanation:
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
13)
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);
A) 66
B) A
C) B
D) 65
Answer [=] C
Explanation:
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.
14)
What is the output of the below Java code snippet? float a = 8.2/2; System.out.println(a);
A) 4.1
B) 8.1
C) 4
D) Compiler error
Answer [=] D
Explanation:
Add a suffix f or F float a = 8.2f/2; (or) explicit typecast float a = (float)8.2/2; System.out.println(a);
15)
What is the output of the Java code snippet? byte b = 25; b++; b = b+1; System.out.println(b);
A) 25
B) 26
C) 27
D) Compiler error
Answer [=] D
Explanation:
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
16)
What is the output of the Java code snippet? int a = 260; byte b= (byte)a; System.out.println(b);
A) 0
B) 4
C) 255
D) 260
Answer [=] B
Explanation:
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
17) In a lossy Type Casting or Type Conversion, how is the number truncated to the target data type in Java?
A) That big number is divided by the target data type highest possible number say 2^N.
B) That big number is Modulo Divided by the target data type highest possible number say 2^N and the Remainder is taken.
C) That big number is Modulo Divided by the target data type highest possible number say 2^N and the Quotient is taken.
D) None of the above
Answer [=] B
Explanation:
byte Maximum = 256 = (*2^8) short maximum = 2^16 = 65536 int maximum = 2^32 long maximum = 2^64
18)
What is the output of the Java code snippet? short a = (short)65540; System.out.println(a);
A) 0
B) 4
C) 65536
D) 65540
Answer [=] B
Explanation:
65540 is bigger than short range -32768 to +32767. So, 65540 % 2^32 = 65540%65536 = 4
19) A boolean literal in Java can be type casted to which data type?
A) byte
B) short
C) int
D) None of the above
Answer [=] D
Explanation:
A boolean literal or variable takes only true or false. So, it does not accept numbers for type conversion.
20) Which data type is type promote to if a variable or operand in an expression is of type long?
A) int
B) long
C) float
D) double
Answer [=] B
Explanation:
All other operands are type promote 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.