Multiple choice

What is the output of the following Java program?

interface A{

} class B implements A {

} class C extends B {

} public class test { public static void main(String[] args) { A c=new C(); System.out.println(c instanceof B); System.out.println(c instanceof A); } }

  1. true true

  2. true false

  3. false true

  4. false false

  5. Error in the program

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

A c=new C(); //parent class which is a interface reference variable refers to object of child class Hence object 'c' created for child class using parent class reference variable. System.out.println(c instanceof B); // B is the parent class of C hence c is the instanceof B also. Hence prints true. System.out.println(c instanceof A); // A is implemented by child class B which is inherited again by child class C. Hence object 'c' can also implement interface A. Hence prints true. Output is: true true