Java OCA OCP Practice Question 1140

Question

How many lines does the following code output?

import java.util.*; 
public class Main { 
        public static void main(String[] args) { 
           List<String> v = Arrays.asList("OCA", "OCP"); 
           for (String e1 : v) 
              for (String e2 : v) 
                 System.out.println(e1 + " " + e2); 
        } 
} 
  • A. One
  • B. Four
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


B.

Note

Looping through the same list multiple times is allowed.

The outer loop executes twice.

The inner loop executes twice for each of those iterations of the outer loop.

Therefore, the inner loop executes four times, and Option B is correct.




PreviousNext

Related