Multiple choice

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 {
  Class Q subclass of P {
    void f(int i) {
      void f(int i) {
        print(i);
        print(2 * i);
      }
    }
  }
}

Now consider the following program fragment: Px = new Q() Qy = new Q(); Pz = 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

  1. 1 2 1

  2. 2 1 1

  3. 2 1 2

  4. 2 2 2

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