Menu
Topics Index
...
`

Room And Fan Output2505


If you need explanation Read this topic

If you need Answer Take test on this topic

Room And Fan Output2505
What will be the output of the following program?
public class RoomAndFan {
    public static void main(String[] args) {
        Room r = new Room();
        Fan f = new Fan();
        r.name = "Hall";
        r.flooring = "Marble";
        r.width = r.height = 12;
        f.speed = 1500;
        r.fan.color = "Brown";
        r.fan.numberOfWings = 3;
        System.out.print(r.name + r.flooring + r.width + r.height);
        System.out.print(r.fan.speed + r.fan.color + r.fan.numberOfWings);
    }
}
class Room {
    String name;
    String flooring;
    int width, height;
    Fan fan = new Fan();
}
class Fan {
    double speed;
    String color;
    int numberOfWings;
}

A. HallMarble12120.0Brown3
B. HallMarble241500.0Brown3
C. HallMarble12121500.0Brown3
D. Compilation Error or Runtime Error
Topic: Calling A Class From Another Class In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Option is A
Here two objects of Fan class and One Object of Room Class have been created.
Among the two objects of Fan Class,
'f' is a reference to fan object created in the main() and 'fan' is a reference to fan object created during the creation of Room Object referred by 'r'.
So the room has name , flooring, width , height and Fan class object referred by 'fan' as it's instance variables.

Status of the Objects
Room Object referred by r
r.name="Hall"
r.flooring="Marble"
r.width=12
r.height=12
r.fan.speed=0.0
r.fan.color="Brown"
r.fan.numberOfWings=3

Fan Object referred by f
f.speed=1500.0
f.color=""
f.numberOfWings=0

Posted by Kittu Manoj    2014-04-28 08:54:38


Ans is c we made a object r of room class in roomandfan class and is printed by print statement and room class extends fan class so all the variables can be used

Posted by Bharath Yelchuri    2014-04-28 08:57:09


Ans is A..  r.width+r.height vl become string so first printf gives HallMarble1212 and we didnt assign the value for the fan.speed variable so it take default value 0.0..

Posted by Om Prakash Om    2014-04-28 08:58:23


Congratulations Kittu Manoj. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-04-29 04:09:00


it also prints speed value so ans is c

Posted by Bharath Yelchuri    2014-04-29 09:36:57


yes Bharath Yelchuri here we have r.fan.color = "Brown" and r.fan.numberOfWings = 3 see 9,10th lines but not r.fan.speed so it holds its default value i.e 0.0(bcoz it is double).so it is prints 0.0 not 1500 so ans is A only

Posted by Raviteja Daggupati    2014-04-29 09:58:05


okk thnks .... ;)

Posted by Bharath Yelchuri    2014-04-29 09:59:42


© meritcampus 2019

All Rights Reserved.

Open In App