Java OCA OCP Practice Question 2863

Question

Given:

1. import java.util.*;  
2. class Shape {  
3.   String getFreq() { return "12.3"; }  
4.   static String getF() { return "12.3"; }  
5. }  // w  w w  .j  av  a 2s .  com
6. class Main extends Shape {  
7.   String getFreq() { return "3.21"; }  
8.   static String getF() { return "3.21"; }  
9.   public static void main(String[] args) {  
10.     List<Shape> radios = new ArrayList<Shape>();  
11.     radios.add(new Shape());  
12.     radios.add(new Main());  
13.     for(Shape r: radios)  
14.       System.out.print(r.getFreq() + " " + r.getF() + "   ");  
15. } } 

What is the result?

  • A. 3.21 3.21 3.21 3.21
  • B. 3.21 12.3 3.21 12.3
  • C. 12.3 3.21 3.21 3.21
  • D. 12.3 12.3 3.21 3.21
  • E. 12.3 12.3 3.21 12.3
  • F. 12.3 12.3 12.3 12.3
  • G. Compilation fails.
  • H. An exception is thrown at runtime.


E is correct.

Note

The important point is that the getF() methods are static.

static methods CANNOT be overridden, but they can be redefined, as is the case in this code.




PreviousNext

Related