Advertisements
Advertisements
प्रश्न
What will be the output of the given Java code after the calculation [note the data types]?
double c = 300, d = 150; int y = 0, g = 0;
y = c/d;
g = d/c;
System.out.println ("Y=" +y+ "G=" +g);
बेरीज
Advertisements
उत्तर
c and d are of type double.
Therefore:
y = c/d;= `300.0 / 150.0` = 2.0g = d/c;= `150.0/300.0` = 0.5
However, y and g are of type int. In Java, a double value cannot be assigned directly to an int variable.
The program will not produce any output because it contains a compile-time error. A double value cannot be assigned directly to an int variable without an explicit type cast.
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 1: Revision of Class 9 Syllabus - Exercises [पृष्ठ ४६]
