Java OCA OCP Practice Question 2465

Question

Given:

1. import java.util.*;  
2. public class Main implements Command {  
3.   public static void main(String[] args) {  
4.     List<String> x = new ArrayList<String>();  
5.     x.add("3");  x.add("7");  x.add("5");  
6.     List<String> y = new Main().doStuff(x);  
7.     y.add("1");  
8.     System.out.println(x);  /*from   ww  w  . java2s  . com*/
9.   }  
10.   List<String> doStuff(List<String> z) {  
11.     z.add("9");  
12.     return z;  
13.   }  
14. }  
15. interface Command {  
16.   List<String> doStuff(List<String> s);  
17. } 

What is the most likely result?

  • A. [3, 7, 5]
  • B. [3, 7, 5, 9]
  • C. [3, 7, 5, 9, 1]
  • D. Compilation fails.
  • E. An exception is thrown at runtime.


D is correct.

Note

Main.doStuff() must be marked public.

If it is, then C would be correct.




PreviousNext

Related