GATE | GATE-CS-2003 | Question 76

Consider the following class definitions in a hypothetical Object Oriented language that supports inheritance and uses dynamic binding. The language should not be assumed to be either Java or C++, though the syntax is similar.

Class P
    void f(int i)

Class Q subclass of P
    void f(int i)

Now consider the following program fragment:

P x = new Q();
Q y = new Q();
P z = new Q();
x.f(1); ((P)y).f(1); z.f(1); 

Here ((P)y) denotes a typecast of y to P. The output produced by executing the above program fragment will be
(A) 1 2 1
(B) 2 1 1
(C) 2 1 2
(D) 2 2 2

Answer: (A)

Explanation: As we are not using virtual function in the classes so, the early binding will happen here i.e., compiler will bind the object with a function by seeing the type of the object/pointer, not the address which they will be storing during run time.

Quiz of this Question

My Personal Notes arrow_drop_up

Improved By : chaudharysatyam3976

Article Tags :


Please write to us at to report any issue with the above content.