What is the result?
A. w-f
B. f-p w-f
C. w-f b-f
D. f-p w-f b-f
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: B
Hello everyone I have understood the code.I'm writting below my understanding please rectify it if necessary
Here 1st lets check is there any possibility of compilation error.
As f is reference of Fish type so it can hold instance of Walleye or Perch
(i.e.,Fish type reference can be assigned to Walleye or Perch object as they lie below it in class hierarchy) and here
test is done with respect to Perch so compiler doesn't complain
then in second if expression w is reference of Walleye type so it can hold instance of Walley which is a Fish and here its tested with respect to Fish so compiler doesn't complain
In 3rd case b is reference of BlueGill type it can hold BlueGill instance or its subtype and if its a Fish(i.e.,subtype of Fish) then
instaneof check might be true so compiler doesn't complain.
As there is no possibility if compilation error lets move runtime where we will check which if expression gives true and what gives false
Here f holds Walleye object which is a Perch so if it is checked with
instanceof Perch then it gives true so f-p is printed
next w holds Walleye object which is a Fish so if its checked with
instanceof Fish then it gives true so w-f is printed
then b holds BlueGill object which isn't Fish so if its checked with
instanceof Fish then it gives false so b-f isn't printed.
kindly check my understanding and rectify it accordingly
thanks in advance
with regards
Roopam