Java Arrays and Multidimensional Arrays Interview MCQ Questions and Answers

Text Example
1) An Array in Java is a collection of elements of ___ data type.
B) Different
2) The Java Virtual Machine (JVM) implements arrays as ___ type.
A) Primitive
Explanation:
That is the reason why Java Array has predefined methods.
3) Unlike C-Arrays, the Java-Arrays have ___.
C) Methods and Fields
4) An array declaration in Java without initialization ___ memory.
A) Does not allocate
B) Allocates memory
Explanation:
Only initialization causes memory to be allocated.
5) In Java language, an array index starts with ___.
D) Any integer
6) Which are the special symbols used to declare an array in Java?
B) Parentheses ()
C) Square Brackets [ ]
D) Angled Brackets < >
7) Which are the special symbols used to initialize an array at the time of the declaration itself?
A) Parentheses ( )
B) Square Brackets [ ]
D) Angled Brackets < >
Explanation:
int[] nums = {1,3,6};
8) It is possible to skip initializing some elements of the array during Shorthand Initialization. (TRUE / FALSE)
Explanation:
No, you can not skip any elements. All elements need to be initialized in one go or at the same time.
9) In Java, an array can be declared without initialization without mentioning the size. (TRUE / FALSE)
Explanation:
It is a Lazy initialization of an array.
10) What is the output of the below Java code snippet with arrays?
static int[] nums;
public static void main(String args[])
System.out.println(nums.length);
C) Compiler error
D) Runtime Exception — Null Pointer Exception
11) What is the output of the below Java program?
int[] marks = {35,65,95};
System.out.print(marks.length + “,” + marks[1]);
D) Compiler error
Explanation:
Array index starts with Zero (0). So marks[1] represents the second element.
12) What is the output of the below Java code snippet?
System.out.print(balls.length);
D) Compiler error
13) Which is the correct way of knowing Array Size in Java?
Explanation:
“length” is a field, not a method. So, parentheses are not required.
14) What is the output of the below Java program with arrays?
String[] colors = {“RED”;”YELLOW”;”WHITE”};
System.out.print(colors[2]);
D) Compiler error
Explanation:
Array elements must be separated with Comma(,)s.
15) What is the output of the below Java program with arrays?
public class Polo {
public static void main(String args[])
String[] computer = {“RAM”,”HDD”,”MOUSE”};
String[] parts = {computer[0],computer[2]};
System.out.print(parts[1]);
D) Compiler error
16) What is the output of the below Java program?
int ages[3] = {25, 27, 30};
System.out.println(ages[1]);
D) Compile error
Explanation:
We should not mention the array size at the time of Shorthand Initialization.
Error: Unresolved compilation problem:
Syntax error on token “3”, delete this token
So make it like
int ages[] = {25, 27, 30};
17) We should not specify the array size if declaration and initialization are done at the same time. (TRUE / FALSE)
Explanation:
int ary[2]={34,45};
int ary[] ={34,45};
18) If an index of an element is N, what is its actual position in the array?
Explanation:
Array index starts with 0. So, an index of 6 means 7th position. An index of N means, N+1 position.
19) An array in Java can be declared only of some predefined types. (TRUE/FALSE)
Explanation:
An array can be of any data type, primitive or Object type.
20) The name of an array variable or identifier can start with ___.
B) Underscore ( _ )
C) Dollar Symbol ($)
21) Shorthand array initialization in Java needs the keyword “new” to allocate memory to the array and elements. State TRUE or FALSE.
Explanation:
Only lazy initialization of array needs the keyword “new” to allocate memory.
22) Lazy initialization of array requires the keyword “new” to allocate memory to the array and its elements. State TRUE or FALSE.
Explanation:
ary = new int[5];
23) What is the default value of an element of Object type array?
D) Garbage value
Explanation:
Objects can be String, ArrayList, HashMap, HashSet etc.
24) What is the default value of byte, short, int or long data type elements of an array in Java?
D) Garbage value
25) What is the default value of float or double data type elements of an array in Java?
26) What is the default value of a char data type elements of an array in Java?
Explanation:
null is nothing but ‘\0’.
27) What is the default value of boolean data type elements of an array in Java?
28) Allocating memory with the keyword “new” causes the array elements to carry default values. State TRUE or FALSE.
29) What is the output of the below Java program?
int balls[], rounds=3;
balls = new int[rounds];
System.out.print(balls[j] + “,”);
D) Compiler error
30) What is the output of the below Java program with arrays?
String[] ary = {“KITE”, “AIR”};
String str = “PLANE”;
System.out.println(ary[1]);
D) Compiler error
31) An array of arrays in Java is called ___ array.
A) Bidirectional
C) Multidimensional
D) Multi-value
32) A multidimensional array contains elements of the same data-type in all rows and columns. State TRUE or FALSE.
33) An array of dimension N contains __ number of subscripts or brackets?
34) An array with two dimensions is called a two-dimensional array in Java. State TRUE or FALSE.
35) Row number and Column number in a Multidimensional array start with ___.
36) A 4-dimensional array is an array of ___ dimensional arrays.
Explanation:
An N-dimensional array is an array of (N-1) dimensional arrays.
37) Choose the correct way of initializing a multidimensional array below.
int[][] code = {{1,2},{3,4,5}};
int[2][] code = {{1,2},{3,4,5}};
Explanation:
We should not mention the size of any row or column.
38) What is the output of the Java program with the multidimensional array?
goats = new int[3][];
System.out.println(goats[0][1]);
D) Compiler error
Explanation:
It is lazy initialization.
So, you can not use braces { } to initialize.
Use the keyword new.
goats[0] = new int[2];
System.out.println(goats[0][1]);
39) State TRUE or FALSE. In a multidimensional array, the number of Columns in each Row can be different.
40) While mentioning the array size in a multidimensional array using the new keyword, the left-most script is mandatory. State TRUE or FALSE.
ary = new int[5][];//first dimension is compulsory.
Originally published at https://cdac-java-mcq.blogspot.com.