Java OCA OCP Practice Question 1815

Question

What would be the result of compiling and running the following program?.


class MyBaseClass {
  int max(int x, int y) { if (x>y) return x; else return y; }
}

class MyBaseClass2 extends MyBaseClass{
  int max(int x, int y) { return super.max(y, x) - 10; }
}

class MyBaseClass3 extends MyBaseClass2 {
  int max(int x, int y) { return super.max(x+10, y+10); }
}
// Filename: Main.java
public class Main {
  public static void main(String[] args) {
    MyBaseClass3 c = new MyBaseClass3();
    System.out.println(c.max(13, 29));
  }//from  w  w  w.java2  s . c  om
}

Select the one correct answer.

  • (a) The code will fail to compile because the max() method in MyBaseClass2 passes the arguments in the call super.max(y, x) in the wrong order.
  • (b) The code will fail to compile because a call to a max() method is ambiguous.
  • (c) The code will compile and print 13, when run.
  • (d) The code will compile and print 23, when run.
  • (e) The code will compile and print 29, when run.
  • (f) The code will compile and print 39, when run.


(e)

Note

The code will compile without errors.

None of the calls to a max() method are ambiguous.

When the program is run, the main() method will call the max() method in MyBaseClass3 with the parameters 13 and 29.

This method will call the max() method in MyBaseClass2 with the parameters 23 and 39.

The max() method in MyBaseClass2 will in turn call the max() method in MyBaseClass with the parameters 39 and 23.

The max() method in MyBaseClass will return 39 to the max() method in MyBaseClass2.

The max() method in MyBaseClass2 will return 29 to the max() method in MyBaseClass3.

The max() method in MyBaseClass3 will return 29 to the main() method.




PreviousNext

Related