0
0
Javaprogramming~15 mins

Array length property in Java - Cheat Sheet & Quick Revision

Choose your learning style8 modes available
overviewRecall & Review
beginner
What does the length property of an array represent in Java?
The length property gives the total number of elements that the array can hold. It tells you how big the array is.
touch_appClick to reveal answer
beginner
How do you access the length of an array named numbers in Java?
You use numbers.length to get the number of elements in the array.
touch_appClick to reveal answer
beginner
True or False: In Java, length is a method for arrays.
False. In Java, length is a property (field) of arrays, not a method. So you do not use parentheses.
touch_appClick to reveal answer
intermediate
What happens if you try to access an index equal to or greater than array.length?
You get an ArrayIndexOutOfBoundsException error because array indexes start at 0 and go up to length - 1.
touch_appClick to reveal answer
intermediate
Can the length of an array be changed after the array is created in Java?
No. The length of an array is fixed once the array is created. To change size, you need to create a new array.
touch_appClick to reveal answer
How do you get the number of elements in a Java array named arr?
Alength(arr)
Barr.length()
Carr.size()
Darr.length
What is the index of the last element in an array with length 10?
A9
B10
C0
D11
If you try to access arr[arr.length], what happens?
AThrows ArrayIndexOutOfBoundsException
BReturns null
CReturns the last element
DReturns the first element
Can you change the size of an existing array in Java?
AYes, by setting a new length
BYes, using <code>resize()</code> method
CNo, arrays have fixed length
DYes, by assigning a new value to <code>length</code>
Which of these is correct to get the length of a Java array?
Aarray.length()
Barray.length
Carray.getLength()
Darray.size
Explain how to find the number of elements in a Java array and why the length property is important.
Describe what happens if you try to access an array index outside the valid range using the length property.