Java OCA OCP Practice Question 2829

Question

Given:

2. import java.util.*;  
3. class Shape {  
4.   static List<String> s1 = new ArrayList<String>();  
5. }  /*ww w .ja  va 2 s. com*/
6. public class Main extends Shape {  
7.   public static void main(String[] args) {  
8.     List c1 = new ArrayList();  
9.     s1.add("1"); s1.add("2");  
10.     c1.add("3"); c1.add("4");  
11.     getShape(s1, c1);  
12.   }  
13.   static void getShape(List<String> a1, List a2) {  
14.     for(String s1: a1) System.out.print(s1 + " ");  
15.     for(String s2: a2) System.out.print(s2 + " ");  
16. } } 

What is the result? (Choose all that apply.)

  • A. "1 2 3 4"
  • B. "1 2", followed by an exception
  • C. An exception is thrown with no other output.
  • D. Compilation fails due to an error on line 13.
  • E. Compilation fails due to an error on line 14.
  • F. Compilation fails due to an error on line 15.


F   is correct.

Note

When getting elements from a non-generic collection, a cast (from Object) is required.

The rest of the code is legal.




PreviousNext

Related