Java OCA OCP Practice Question 2264

Question

What is true about the following?

import java.util.*; 
public class Main { 
   public static void main(String[] args) { 
      List list = Arrays.asList("Main"); 
      method(list);     // c1 
   } /*from   ww  w.  j a va 2s. c o  m*/
   private static void method(Collection<?> x) {    //c2 
      x.forEach(a -> {});   // c3 
  } 
} 
  • A. The code doesn't compile due to line c1.
  • B. The code doesn't compile due to line c2.
  • C. The code doesn't compile due to line c3.
  • D. The code compiles and runs without output.
  • E. The code compiles but throws an exception at runtime.


D.

Note

This code actually does compile.

Line c1 is fine because the method uses the ? wildcard, which allows any collection.

Line c2 is a standard method declaration.

Line c3 looks odd, but it does work.

The lambda takes one parameter and does nothing with it.

Since there is no output, Option D is correct.




PreviousNext

Related