Java OCA OCP Practice Question 865

Question

How many lines does the following code output?

public class Main {
   public static void main(String[] args) {
      String[] days = new String[] { "1", "2", 
            "3", "4", "5", "6", 
            "7" };
      for (int i = 1; i < days.length; i++)
         System.out.println(days[i]);

   }//from www .j  a v a  2s .co m
}
  • A. Six
  • B. Seven
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


A.

Note

In Java, arrays are indexed starting with 0.

While it is unusual for the loop to start with 1, this does not cause an error.

It does cause the code to output six lines instead of seven since the loop doesn't cover the first array element.

Option A is correct.




PreviousNext

Related