?
What will be output of the following program code?
What will be output of the following program code?
public class Test{
public static void main(String[] a){
short x = 10;
x = x*5;
System.out.print(x);
}
}
public class Test{
public static void main(String[] a){
short x = 10;
x = x*5;
System.out.print(x);
}
}
Answer & Solution
Correct Answer:
Option
C
lossy conversion from int to short
x = x*5;
^
1 error
x = x*5;
^
1 error
Join the Discussion
Login to post a comment or share your explanation.
Loginpublic static void main(String args[]){
short x = 10;
x = (short) (x*5);
System.out.print("x="+x);
}
}
how it is happening ....
int isn't used anywhere how x is converted to short...x is already short.
x=50
but in the program x is short data type and 5 is int datatype
it is not possible to store int value in short so
ans Compile time error
x=x*(short)5;
then we get 50 answer
here 5 is the integer , then it is not possible to get the result in short
float x = 10.0f;
x = x * 1.4;
you will get the same error as the 1.4 is interpreted as double do it with
x = x * 1.4f and the problem solves.
As a whole, you can multiply a smaller type with a larger one