Java OCA OCP Practice Question 1136

Question

What is the result of the following?

String[] s = new String[] { "A", "B", "C" }; 
String[] times = new String[] { "Day", "Night" }; 
for (int i = 0, j = 0; i < s.length 
        && j < times.length; i++, j++) 
{ 
        System.out.print(s[i] + " " + times[j] + "-"); 
} 
  • A. A Day-
  • B. A Day-B Night-
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


B.

Note

This code is correct.

It initializes two variables and uses both variables in the condition check and the update statements.

Since it checks the size of both arrays correctly, it prints the first two sets of elements, and Option B is correct.




PreviousNext

Related