Menu
Topics Index
...
`

What am I doing


If you need explanation Read this topic

If you need Answer Take test on this topic

What am I doing
What will be the output of the following program?
public class IAm {
    public static void main(String arg[]) {
        float input = 24.8888888888888889f;
        School ref = null;
        if (input < 10) {
            ref = new School();
        } else if (input > 30) {
            ref = new College();
        } else if (input == 24.88889f) {
            ref = new Office();
        }
        ref.display();
    }
}
class School {
    void display() {
        System.out.println("I am studying 10th class.");
    }
}
class College extends School {
    void display() {
        System.out.println("I am studying B.Tech.");
    }
}

class Office extends School {
    void display() {
        System.out.println("I am working in S/W company.");
    }
}

A. I am studying 10th class.
B. I am studying B.Tech.
C. I am working in S/W company.
D. Compilation Error
E. Runtime Error
Topic: Run Time Polymorphism 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.

C is right

because  ref = new Office(); method will be called and class office wilAns is C...here we create an float variable with value 24.888f..here every float value is double by default so that we are trying to convert it into float by using the f...

here we create reference for the School class

next we are checking in if conditions...last if if condition is true than Office object is created and assigned to School reference
in Java PARENT CLASSS REFERENCE CAN REFER CHILD CLASS OBJECT

Here School is parent class for the Bpthe College and Office...

next we are calling the display() method with the object of Office....while calling compiler wil check whether the method is present in reference type(School) here it is presented so no error...

while executing it wil 1st check in Object type...object type is Office so that it wil execute the method in Office class and it wil print "I am working in S/W company"l be executed in which" I am working in S/W company."   statement will be executed

Posted by Shubham Bansal    2014-11-03 12:29:52


c

Posted by Shubham Bansal    2014-11-03 12:30:09


ans e.all the if case get failed. so outside if condition... ref.display () throws exception. it throws nullpointException bcz this func pointing to nothing

Posted by Maheshwari Natarajan    2014-11-03 12:36:53


Ans is C...here we create an float variable with value 24.888f..here every float value is double by default so that we are trying to convert it into float by using the f...

here we create reference for the School class

next we are checking in if conditions...last if if condition is true than Office object is created and assigned to School reference
in Java PARENT CLASSS REFERENCE CAN REFER CHILD CLASS OBJECT

Here School is parent class for the Bpthe College and Office...

next we are calling the display() method with the object of Office....while calling compiler wil check whether the method is present in reference type(School) here it is presented so no error...

while executing it wil 1st check in Object type...object type is Office so that it wil execute the method in Office class and it wil print "I am working in S/W company"

Posted by Uday Kumar    2014-11-03 15:59:41


This dose is now closed and the winners are Shubham Bansal,  for 'First Correct Comment', Shubham Bansal,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Gaurav Bansal. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-04 04:08:21


Our apologies the correct winner Uday Kumar. We did not realize that Shubham Bansal has copied the comments of Uday Kumar. We are banning Shubham Bansal, since he is not sticking to the rules and trying to cheat.

Posted by Merit Campus    2014-11-04 04:21:10


© meritcampus 2019

All Rights Reserved.

Open In App