Java OCA OCP Practice Question 2743

Question

Given the following method signatures from ArrayList:

boolean add(E e)  
protected void removeRange(int fromIndexInclusive, int toIndexExclusive)  
int size() 

and given:

import java.util.ArrayList;

public class Main extends ArrayList {
   public static void main(String[] args) {
      Main m = new Main();
      m.add("w");
      m.add("x");
      m.add("y");
      m.add("z");
      m.removeRange(1, 3);// ww  w  .j a  va  2s .  c  om
      System.out.print(m.size() + " ");
      Main m2 = new Main2().go();
      System.out.println(m2.size());
   }
}

class Main2 {
   Main go() {
      Main m2 = new Main();
      m2.add("1");
      m2.add("2");
      m2.add("3");
      m2.removeRange(1, 2);
      return m2;
   }
}

What is the result?

  • A. 1 1
  • B. 1 2
  • C. 2 1
  • D. 2 2
  • E. An exception is thrown at runtime.
  • F. Compilation fails due to a single error.
  • G. Compilation fails due to multiple errors.


F   is correct.

Note

The removeRange() method is protected and so cannot be accessed from the Main2 class.

The rest of the code is legal.




PreviousNext

Related