Vidyalelo
Java Programming · Q38

Data Types and Variables

Programming · Java Programming · question 38

Q38

Automatic type conversion in Java takes place when

A.
Two type are compatible and size of destination type is shorter than source type.
B.
Two type are compatible and size of destination type is equal of source type.
C.
Two type are compatible and size of destination type is larger than source type.
Answer
D.
All of the above

Answer: Option C

Solution

Answer: Option C
Solution:
The correct answer is C: Two types are compatible and the size of the destination type is larger than the source type.

Automatic type conversion, also known as implicit type conversion or widening conversion, happens when Java automatically converts one data type to another.

This occurs when two conditions are met:

1. Compatibility: The two data types must be compatible. Java must know how to convert from the source type to the destination type.

2. Size: The destination type must be larger than the source type so that no data is lost.

Why other options are wrong:

Option A: If destination is shorter than source, data loss may occur. Java does NOT allow this automatically. It needs explicit casting.

Option B: If both types are equal in size, conversion is not needed because the source already fits.

Example:

int myInt = 10;
double myDouble = myInt; // Allowed, widening conversion

double myDouble2 = 10.5;
int myInt2 = (int) myDouble2; // Needs explicit cast because narrowing may lose data