Java OCA OCP Practice Question 1370

Question

How many lines does the following code output?

import java.util.*; 
public class Main { 
   public static void main(String[] args) { 
      List<String> l = Arrays.asList("A", "B"); 
      for (String e1 : l) 
         for (String e2 : l) 
            System.out.print(e1 + " " + e2); 
            System.out.println(); 
   } // w ww.  jav a2  s . co  m
} 
  • A. One
  • B. Four
  • C. Five
  • D. The code does not compile.
  • E. The code compiles but throws an exception at runtime.


A.

Note

Looping through the same list multiple times is allowed.

There are not braces around the loops.

This means that only the print statement is inside the loop.

It executes four times.

The println() only executes once at the end, making Option A the answer.




PreviousNext

Related