हिंदी

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
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.0
  • g = 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 [पृष्ठ ४६]

APPEARS IN

रुपा पंडित Computer Applications [English] Class 10 ICSE
अध्याय 1 Revision of Class 9 Syllabus
Exercises | Q 5. (iv) | पृष्ठ ४६
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×