0
0
Javaprogramming~15 mins

One-dimensional arrays in Java - Cheat Sheet & Quick Revision

Choose your learning style8 modes available
overviewRecall & Review
beginner
What is a one-dimensional array in Java?
A one-dimensional array in Java is a collection of elements of the same type stored in a single row. It allows you to store multiple values in a single variable, accessed by an index starting at 0.
touch_appClick to reveal answer
beginner
How do you declare a one-dimensional array of integers named 'numbers' with 5 elements in Java?
You declare it like this:
int[] numbers = new int[5];
This creates an array that can hold 5 integers, all initially set to 0.
touch_appClick to reveal answer
beginner
How do you access the third element in a one-dimensional array named 'data'?
You access it using its index:
data[2]
Remember, array indexes start at 0, so the third element is at index 2.
touch_appClick to reveal answer
intermediate
What happens if you try to access an array index that is out of bounds in Java?
Java throws an ArrayIndexOutOfBoundsException. This means you tried to access an index less than 0 or greater than or equal to the array length.
touch_appClick to reveal answer
beginner
How can you initialize a one-dimensional array with values at the time of declaration?
You can write:
int[] numbers = {10, 20, 30, 40, 50};
This creates an array with 5 elements already set to those values.
touch_appClick to reveal answer
What is the index of the first element in a Java array?
A1
B0
C-1
DDepends on the array size
Which of the following is the correct way to declare a one-dimensional array of doubles named 'prices' with 10 elements?
Adouble prices = new double[10];
Bdouble prices[] = new double;
Cdouble prices[] = new double(10);
Ddouble[] prices = new double[10];
What will happen if you try to access array element at index 5 in an array declared as int[] arr = new int[5];?
AReturns the last element
BReturns 0
CThrows ArrayIndexOutOfBoundsException
DCreates a new element at index 5
How do you find the length of an array named 'items' in Java?
Aitems.length
Bitems.size()
Citems.length()
Dlength(items)
Which of these initializes an array with values 1, 2, 3 in one line?
Aint arr[] = {1, 2, 3};
Bint[] arr = new int[3]{1, 2, 3};
Cint arr = {1, 2, 3};
Dint[] arr = (1, 2, 3);
Explain what a one-dimensional array is and how you declare one in Java.
Describe how to access and modify elements in a one-dimensional array in Java.