How do you determine the number of elements in an array?

How do you determine the number of elements in an array?

A. buses.length

B. buses.length()

C. buses.size

D. buses.size()

Answer:

A. buses.length

Explanation:

Arrays use the length variable to determine the number of elements, making Option A correct. 

For an ArrayList, Option D would have been the answer.

Example: Java program to illustrate how to get the length of the array: 
// Java program to illustrate
// how to get the length of the array

public class Test {
	public static void main(String[] args)
	{

		// Here array is the
		// array name of int type
		int[] array = new int[4];

		System.out.println("The size of "
						+ "the array is "
						+ array.length);
	}
}

Output:

The size of the array is 4

Comments