Java OCA OCP Practice Question 2625

Question

Given:

import java.util.List;
import java.util.Vector;

public class Main {
   public static void main(String[] args) {
      List<Integer> i = new Vector<Integer>();
      i.add(3);//from   ww  w  . j a  va  2s.c  o  m
      i.add(2);
      i.add(5);
      int ref = 1;
      m(ref);
      System.out.println(i.get(ref));
   }

   static int m(int x) {
      return ++x;
   }
}

What is the result?

  • A. 2
  • B. 3
  • C. 5
  • D. Compilation fails.
  • E. An exception is thrown at runtime.


H:Note

A is correct.

The variable "x" is a copy of ref, and when "x" gets incremented, ref does not.

Also, the list index is zero-based and instances of Vector aren't automatically sorted.




PreviousNext

Related