Java OCA OCP Practice Question 3090

Question

Consider the following program:

class Base {//  w  ww . j  a v  a 2 s  . c om
        public static void foo(Base bObj) {
               System.out.println("In Base.foo()");
               bObj.bar();
        }
        public void bar() {
               System.out.println("In Base.bar()");
        }
}

class Derived extends Base {
        public static void foo(Base bObj) {
               System.out.println("In Derived.foo()");
               bObj.bar();
        }
        public void bar() {
               System.out.println("In Derived.bar()");
        }
}

public class Main {
        public static void main(String []args) {
               Base bObj = new Derived();
               bObj.foo(bObj);
        }
}

What is the output of this program when executed?

a)
In Base.foo()/*ww w . j  a v  a  2 s .co  m*/
In Base.bar()
b)
In Base.foo()
In Derived.bar()
c)
In Derived.foo()
In Base.bar()
d)
In Derived.foo()
In Derived.bar()


b)

Note

A static method is resolved statically.

Inside the static method, a virtual method is invoked, which is resolved dynamically.




PreviousNext

Related