Menu
Topics Index
...
`

What am I doing 2273


If you need explanation Read this topic

If you need Answer Take test on this topic

What am I doing 2273
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.88889) {
            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

Posted by Kommeta Narsing    2014-11-04 03:54:18


ans e .
ref.display() throws nullpointException.
bcz if and else if conditions failed.
so that called function pointing to nothing.

Posted by Maheshwari Natarajan    2014-11-04 04:19:24


Answer C

Here we have created three classes here one is School , college and office- college extends from school, office
also extends from school.
In the main method, we have created a variable called input. Then depending upon the value of input,
we have either created an object of school or college or office and assigned it to the reference
of school called ref..
At LINE school, we are only calling the display method using ref.

Posted by Nichitha Veludandi    2014-11-04 04:38:35


Answer is E it will throw a run time error since ref will not be initialsed because input==24.88889 is not true (because floating point value is considred as double by default and its precision is higher and comparing a double and float value actually takes precision into account and it becomes false).calling method using null reference will throw a null pointer exception

Posted by Shashanka Mogaliraju    2014-11-04 05:16:09


Ans is E...here NullPointerException will thrown..this Exception will occur when we try access the element of particular class without creating the Object...accessing with the reference than this exception will raise

.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...all if conditions are failed..in Last If Condition we are we are checking input with 24.88889 which is treated as Double by default...so that here also condition gets failed....so that here reference of the School(ref) didnt get assigned with any class Object..after this we are calling the display()...without creating object we are accessing so that NullPointerException wil Thrown...

Posted by Uday Kumar    2014-11-04 13:47:30


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Maheshwari Natarajan. 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-05 01:18:28


© meritcampus 2019

All Rights Reserved.

Open In App