Menu
Topics Index
...
`

Method overriding and TypeCasting


If you need explanation Read this topic

If you need Answer Take test on this topic

Method overriding and TypeCasting
What will be the output of the following program?
public class Rough1 {
    public static void main(String[] args) {
            byte b = getStatusValue(255);
            System.out.println(b);
            b = Rough2.getStatusValue(122);
            System.out.println(b);
    }
    static byte getStatusValue(Object obj) throws NullPointerException {
        if (obj != null)
            return 127;
        else
            return -1;
    }
}
class Rough2 extends Rough1 {
    static byte getStatusValue(Object obj) {
        if (obj != null)
            try {
                return (Byte) obj;
            } catch (Exception e) {
                System.out.println("Can't type cast");
            }
        else
            return -1;
        return (byte) 255;
    }
}

A. -1
122
B. 127
Can't type cast
-1
C. 127
122
D. -1
Can't type cast
-1
E. Some other output
F. Compilation Error or Runtime Error
Topic: Java Throw Keyword - Java Throws Keyword

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.

Ans is B..the function getStatusValue returns 127 as the object is not empty and in Rough2 we can't typecast 255 to byte so the output is -1..

Posted by Badam Swathi    2014-07-24 12:53:13


127 cant't type cast -1  here rough2 object value not declared and else statement here return -1 are to be taken

Posted by Sai Nadh    2014-07-24 13:00:23


Ans b.
1st methd cald n objct not a null so method returns 127.
rough2 refernc using dot operatr cald method ,exception occurd so it prints cant  type cast.
returns -1

Posted by Maheshwari Natarajan    2014-07-24 13:01:12


ans is b. 127

Posted by Shaktiraj Herma    2014-07-24 13:02:20


ans is F..getStatusValue thrwing exception bt nt handling in main

Posted by Uday Kumar    2014-07-24 13:04:16


ans c

Posted by Samyuktha Reddy    2014-07-24 13:24:12


B

Posted by Pavan Kalyan    2014-07-25 01:13:35


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

Posted by Merit Campus    2014-07-25 04:10:01


© meritcampus 2019

All Rights Reserved.

Open In App