Java OCA OCP Practice Question 39

Question

What changes are needed to make the following program compile?

import java.util.*;

public class Main {
   public static void main(String args[]) {
      String s1 = "abc";
      String s2 = "def";
      Vector v = new Vector();
      v.add(s1);//  w ww. ja v a2 s.  c  o  m
      v.add(s2);
      String s3 = v.elementAt(0) + v.elementAt(1);
      System.out.println(s3);
   }
}
  • A. Declare Question as public.
  • B. Cast v.elementAt(0) to a String.
  • C. Cast v.elementAt(1) to an Object.
  • D. Import java.lang.


B.

Note

The elementAt() method of Vector returns an Object reference.




PreviousNext

Related