Java Type Casting / Promotion MCQ Questions and Answers on 1

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.



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.

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) If a variable or operand in an expression is type long, then all the operands are type promoted to which data type?
A) int
B) long
C) float
D) double
Answer [=]
B
Explanation:

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.