Java OCA OCP Practice Question 692

Question

The author of this method forgot to include the data type.

Which of the following reference types can fill in the blank to complete this method?

public static void m(____ v) { 
   v.add("metal"); 
   String str = v.get(0); 
   int num = v.length(); 
} 
  • A. ArrayList
  • B. ArrayList<String>
  • C. StringBuilder
  • D. None of the above


D.

Note

The add() and get() methods are available on ArrayList.

ArrayList uses size rather than length to get the number of elements.

Option D is correct.

If length was changed to size, Option B would compile if put in the blank.

Option A still wouldn't compile in the blank because a cast would be needed to store the value in str.




PreviousNext

Related