Examveda

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.

D. All of the above

Answer: Option C

Solution (By Examveda Team)

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

This Question Belongs to Java Program >> Data Types And Variables

Join The Discussion

Comments (9)

  1. Hari Krishna
    Hari Krishna:
    2 days ago

    Here option c is correct

  2. Deependra Singh
    Deependra Singh:
    1 year ago

    I believe the size of the destination should be larger than the source type e.g. int to long, byte to short or int or long,,,,, float to double,,,,in all these examples, the destination (long, on LHS) is larger than the source (int, on RHS).
    Therefore, the answer should be OPTION C where the size of destination type is larger than source type.

  3. Madagalam UmaMaheswari
    Madagalam UmaMaheswari:
    2 years ago

    Ok let me explain the concept behind this is implicit type casting .
    where java/jvm easily convert RHS TO LHS side type when the range/memory is huge on LHS and RHS is small range/memory then LHS ,so when we assign like this example :- long a = 32667777888;(remember in java long automatically treats as int unless and until you add L in the suffix to literal(to value);so on RHS side it is practically we are willing to store an int literal(value)---(this is source type) and assigning it to long on LHS where two or different types but compatible(same type of data that is decimal value ) as LHS has long storage capacity is huge---(destination type) so it can store int value easily without any complation error.but when we try to assign RHS Value more than int range it gives you compilation error -- (size is exceed like that ) then the scenario of adding "L" at suffix to RHS value comes into picture beacuse java treats long value as int within its range.so to end it the destination type is LHS and source type is RHS ,RHS is always have huge storage capacity then LHS (then only java will auttomatically convert type implictly)and that should be compatible means have same work in same type data (decimal values).same goes with float and double and char.

  4. Mujammil Ali
    Mujammil Ali:
    7 years ago

    solution me

  5. Nikhil Rj
    Nikhil Rj:
    7 years ago

    Sorry 2nd example was wrong
    float f=123l;
    compiled successfully and executed successfully... Where as size of long is larger than float..

  6. Nikhil Rj
    Nikhil Rj:
    7 years ago

    I need to give you another example..
    long l=1.7f;
    Compiled successfully and execute successfully..
    Size of long is larger than float...

  7. Nikhil Rj
    Nikhil Rj:
    7 years ago

    Wrong question
    char c=10;
    Compiled successfully and run successfully where as 10 is by default int type.
    10 is here auto casted to char..

  8. Harshad Khot
    Harshad Khot:
    8 years ago

    byte have 8 bits i'e 1 byte and short have 2 byte. if you convert value byte to short then it convert it automatically bcz byte is lesser then short but convert short to byte then it no converting bcz short is greeter then byte .

  9. Karthik Raja
    Karthik Raja:
    8 years ago

    plz explain this question......

Related Questions on Data Types and Variables