Arrays QuizJ8 Home « Objects & Classes Quiz

Java Objects & Classes Quiz 1

The quiz below tests your knowledge of the material learnt in Objects & Classes - Lesson 1 - Arrays.

Question 1 : What is the second step of array creation?
- Array allocation is the second step of creating an array.
Question 2 : We can look at an element of an array after array declaration?
- We can only look at an element within an array after allocation. After declaration no size has been allocated.
Question 3 : What will be output from this code snippet?

String strArray[] = {"one", "aa", "c", "rt", "je"};
- The output will be <code>aa</code>. Arrays are zero-index based
Question 4 : What will be output from this code snippet?

String strArray3[] = new String[3]{"one", "rt", "je"};
- The code doesn't compile. When using a single statement to create an array, no size should be specified.
Question 5 : Which of the following statements will compile?

int intArray1[][]; // A
int[][] intArray2; // B
int[] intArray3[]; // C
- All the statements will compile as the square brackets can follow the type, the array name or be split between the two for multi dimensional arrays.
Question 6 : What will be output from this code snippet?

String[] strArray1;; // Array declaration
strArray1 = new String[1]; // Array allocation
- The output will be <code>null</code> as space has been allocated so array is initialized to default.
Question 7 : We can have a maximum of two dimensions for arrays?
- There is actually no limit to the amount of dimensions an array can have although in practical terms arrays are rarely more than three dimensions.
Question 8 : What will be result when this code snippet is run?

int intArray1[]; // 1
intArray1 = int[2]; // 2
intArray1[0] = 22; // 3
intArray1[1] = 44; // 4
System.out.println(intArray1[0]); // 5
- Doesn't compile. The <code>new</code> keyword is mandatory for multiple statement array creation.<br>The code would compile and output <code>22</code> if line 2 changed to <code>intArray1 = new int[2];</code>.
Quiz Progress Bar Please select an answer

What's Next?

The next quiz on Java Objects & Classes is array exceptions and examples.