Menu
Topics Index
...
`

instanceof keyword1992


If you need explanation Read this topic

If you need Answer Take test on this topic

instanceof keyword1992
Compare the following programs?
public class Scroll {
    public static void main(String[] args) {
        Scroll t = (Scroll) new Object();
        System.out.println(true);
    }
}
class Scroll2 {
    public static void main(String[] args) {
        String s = null;
        System.out.println(s instanceof String);
    }
}

A. Both Scroll and Scroll2 produce same output. 
B. Scroll and Scroll2 produce different output.
C. Scroll compiles and runs with out any error, but Scroll2 gives compilation error.
D. Scroll2 compiles and runs with out any error, but Scroll gives compilation error.
E. Both Scroll and Scroll2 fail to execute. 
Topic: Assigning Super Class Reference To A Sub Class Reference 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.

Answer : D coz i think at Line:3 we will have an error , RUN_TIME_EXCEPTION as we cannot typeCast it with (Scroll)

Posted by Deepak Kumar Yadav    2014-05-08 03:58:30


B.
We can have multiple main() methods provided they need to be in different classes and need to executed individually
if we execute like " java Scroll "
Output : true
on the other hand if we execute like " java Scroll2 "
Output : false as ( null instanceOf String --> false )

Posted by Abhishek Kathuria    2014-05-08 06:39:10


Congratulations Deepak Kumar Yadav. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-05-09 04:02:19


© meritcampus 2019

All Rights Reserved.

Open In App