Java OCA OCP Practice Question 2869

Question

Given:

1. import java.util.*;  
2. public class Main {  
3.   public static void main(String[] args) {  
4.     ArrayList[] ls = new ArrayList[3];  
5.     for(int i = 0; i < 3; i++) {  
6.       ls[i] = new ArrayList();  
7.       ls[i].add("a" + i);  
8.     }  /*from w w w. j a  v  a2  s. c  o  m*/
9.     Object o = ls;  
10.     do3(ls);  
11.     for(int i = 0; i < 3; i++) {     
12.       // insert code here  
13.     }  
14.   }  
15.   static Object do3(ArrayList[] a) {  
16.     for(int i = 0; i < 3; i++)  a[i].add("e");  
17.     return a;  
18. } } 

And the following fragments:

  • I. System.out.print(o[i] + " ");
  • II. System.out.print((ArrayList[])[i] + " ");
  • III. System.out.print( ((Object[])o)[i] + " ");
  • IV. System.out.print(((ArrayList[])o)[i] + " ");

If the fragments are added to line 12, independently, which are true? (Choose all that apply.)

  • A. Fragment I will compile.
  • B. Fragment II will compile.
  • C. Fragment III will compile.
  • D. Fragment IV will compile.
  • E. Compilation fails due to other errors.
  • F. Of those that compile, the output will be [a0] [a1] [a2]
  • G. Of those that compile, the output will be [a0, e] [a1, e] [a2, e]


C, D, and G are correct.

Note

Fragments III and IV are the correct syntax to cast the Object back to an array of objects.

The references "o" and "ls" refer to the same object, so the changes made to "ls" in do3() are accessible via "o".




PreviousNext

Related