English

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

Question

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);

Sum
Advertisements

Solution

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
  Is there an error in this question or solution?
Chapter 1: Revision of Class 9 Syllabus - Exercises [Page 46]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 1 Revision of Class 9 Syllabus
Exercises | Q 5. (iv) | Page 46
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×