Quiz 1 Review
How do you instantiate an array in Java? a) int arr[] = new int(3); b) int arr[]; c) int arr[] = new int[3]; d) int arr() = new int(3);
c) int arr[] = new int[3]; Explanation: Note that option b is declaration whereas option c is to instantiate an array.
Which of the following is a correct way to declare a multidimensional array in Java? a) int[][] arr; b) int arr[][]; c) int[] arr[]; d) All of the mentioned
d) All of the mentioned Explanation: All the options are syntactically correct.
What is the output of the following piece of code? public class array { public static void main(String args[]) { int[]arr = {1,2,3,4,5}; System.out.println(arr[2]); System.out.println(arr[4]); } } a) 3 and 5 b) 5 and 3 c) 2 and 4 d) 4 and 2
a) 3 and 5 Explanation: Array indexing starts from 0.
Which of this best describes an array? a) A data structure that shows a hierarchical behavior b) Container of objects of similar types c) Container of objects of mixed types d) All of the mentioned
b) Container of objects of similar types Explanation: Array contains elements only of the same type.
What is the output of the following piece of code? public class array { public static void main(String args[]) { int []arr = {1,2,3,4,5}; System.out.println(arr[5]); } } a) 4 b) 5 c) ArrayIndexOutOfBoundsException d) InvalidInputException
c) ArrayIndexOutOfBoundsException Trying to access an element beyond the limits of an array gives ArrayIndexOutOfBoundsException.