Arrays in Java and Multidimensional Arrays Tutorial and Interview MCQ
Table of Contents
Study and learn Interview MCQ Questions and Answers on Arrays in Java and Multidimensional Arrays. Attend job interviews easily with these Multiple Choice Questions.
Arrays in Java and Multidimensional Arrays Tutorial
An array is a collection of elements of same data type Pri-mitive or Object type. Each element has a position number called an Index. The size of an array is simply the number of elements of that array. Let us learn Java arrays and multidimensional arrays in detail.
Note: Java arrays are implemented as Objects. So, a Java array variable has some predefined methods and fields. One such useful read-only FIELD is the length, which holds array size.
One Dimensional Arrays in java
An array can be thought of like a Train with a number of Coaches and each coach has a number. If we know the coach number, we can directly go to that coach easily. Coach number is comparable to an array element INDEX. A Single train represents one dimension 1D or 1D array.
Multiple trains represent Multidimensional arrays where each train also has an index starting with Zero along with a separate index for train coaches or array elements. Index of an array always starts with ZERO and ends with SIZE-1.
0) Array Declaration
Array declaration is nothing but declaring an array variable with a Type. Type can be pri-mitive or non-pri-mitive. Array declaration does not allocate memory for an array variable. There is no need to mention the Size of an array during declaration. Difference between a normal variable and an array variable during declaration or accessing is the addition of Square Brackets and a Subscript indicating the Size, Index or element Position.
Syntax:
Type[] variable; (or) Type variable[]; eg. int ary[]; String[] strAry; int[] ary1, ary2, ary3; int ary1[], ary2[], ary3[];
1) Array Inline Initialization or Shorthand Initialization
Array Inline Initialization does not ask for mentioning its size. At the time of the Declaration itself, you can initialize all array elements using a Shorthand method with two braces and a list of values separated by Commas. You should not add size within square brackets next to the array variable. It is a static way of initializing as the array size cannot be specified at Runtime afterwards.
The problem with this approach is that you can not declare an array without size if you are not initializing it on the same line or statement and later use this Shorthand Initialization approach. You get an error like “Array constants can only be used in initializers“.
Syntax:
TYPE variable[] = {value1, value2, ..., valueN}; eg. int marks[] = {45, 56, 76, 55};
Example: Array initialization with integers and String
int nums[] = {1,24,54,35,87}; for(int i=0; i<5; i++) { System.out.print(i + ","); } String names[] = {"CRICKET", "HOCKEY", "SOCCER"}; for(int i=0; i<names.length; i++) { System.out.println(names[i]); }
2) Lazy Array Initialization / Declare and Initialize Array Later Approach
Lazy array initialization is also called Dynamic Array Initialization. You should not specify the size during the declaration. You can specify the size of an array at Runtime. On the first line in the below example, we have not specified the size of an array.
Syntax:
TYPE variable[]; variable = new TYPE[ SIZE ]; eg. int ary1[]; ary1 = new int[5]; ary1[0]=45; ary1[4]=55; String str[]; str = new String[10]; str[2] = "CABBAGE"; str[3] = "TOMATO";
variables that are declared inside methods are called Local variables. You can declare a local variable of any data type or Object type, but you can not use it without initialization. If you try to use an uninitialized variable, you get an error saying “The local variable may not have been initialized“. Variables that are created using the keyword “new” are an exception to this rule.
Example:
public void show() { int p; //NO Error System.out.println(p); //Error for using it }
Rules for Lazy array Initialization:
- Array size must be specified along with new keyword.
- Array Type must be specified
- A valid identifier for array variable should be given
- Keyword “new” is used to specify the size of the array
To create a new Object or an array in Java, we use a “new” keyword. This “new” keyword allocates memory for the array or an Object. Using the keyword new causes the objects and array elements to get some default values. For pri-mitive data types, default values are dependent on the data type. For Objects, the default value is null. Integers are initialized with a default value of ZERO. Boolean values are initialized with “false” by default.
Data Type | Default Value |
---|---|
byte | 0 |
short | 0 |
char | null or ‘\0’ |
int | 0 |
long | 0 |
float | 0.0 |
double | 0.0 |
Object | null |
String | null( String is an Object) |
Multidimensional Arrays in java
A Multidimensional array in Java is simply an Array of Arrays. It can be comparable to a Matrix with a Row number and a Column number. The row index or number starts at 0. The column index or number starts at 0. Depth or Dimension of an array is represented by the letter ‘D’. So, 3D arrays represent a 3-dimensional array.
An N-dimensional array consists of an array of (N-1)D arrays. For example, a 3D array consists of an array of 2D arrays.
Array Declaration
A multidimensional array declared just like declaring a Single Dimensional array as shown above. You should specify the leftmost dimension or subscript.
Syntax:
int ary[4][][]; //Left most subscript is compulsory int myary[2][];
Array Initialization
Multidimensional arrays Java initialized in two ways.
- Inline Initialization
- Lazy Initialization
Inline Array Initialization
To declare and initialize a Multidimensional array, there is no need to mention the size or any dimension. All subscripts left empty within Square Brackets.
Example:
int twoAry[][] = {{1,2}, {5,4,3}}; for(int i=0; i<twoAry.length; i++) { for(int j=0; j<twoAry[i].length; j++) { System.out.print(twoAry[i][j] + " "); } System.out.println(""); } //OUTPUT //1 2 //5 4 3
Lazy Array Initialization
To create a multidimensional array with a new keyword, you need to specify the leftmost dimension or subscript.
Example
int twoAry[][]; twoAry = new int[2][]; //or //twoAry = new int[2][3]; twoAry[0] = new int[5]; twoAry[1] = new int[4]; int c=0; for(int i=0; i<twoAry.length; i++) { System.out.println("Row-" + (i) + "-size=" + twoAry[i].length); } //OUTPUT Row-0-size=5 Row-1-size=4
This is how Arrays in Java work.
[WpProQuiz 155]
Arrays in Java Interview MCQ
1) An Array in Java is a collection of elements of ___ data type.
A) Same
B) Different
Answer [=] A
2) The Java Virtual Machine (JVM) implements arrays as ___ type.
A) Pri-mitive
B) Object
Answer [=] B
Explanation:
That is the reason why Java Array has predefined methods.
3) Unlike C-Arrays, the Java-Arrays have ___.
A) Names
B) Values
C) Methods and Fields
D) None
Answer [=] C
4) An array declaration in Java without initialization ___ memory.
A) Does not allocate
B) Allocates memory
Answer [=] A
Explanation:
Only initialization causes memory to be allocated.
5) In Java language, an array index starts with ___.
A) -1
B) 0
C) 1
D) Any integer
Answer [=] B
6) Which are the special symbols used to declare an array in Java?
A) Braces { }
B) Parentheses ()
C) Square Brackets [ ]
D) Angled Brackets < >
Answer [=] C
7) Which are the special symbols used to initialize an array at the time of the declaration itself?
A) Parentheses ( )
B) Square Brackets [ ]
C) Braces { }
D) Angled Brackets < >
Answer [=] C
Explanation:
int[] nums = {1,3,6};
8) It is possible to skip initializing some elements of the array during Shorthand Initialization. (TRUE / FALSE)
A) FALSE
B) TRUE
Answer [=] A
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)
A) TRUE
B) FALSE
Answer [=] A
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); }
A) 0
B) null
C) Compiler error
D) Runtime Exception – Null Pointer Exception
Answer [=] D
11) What is the output of the below Java program?
int[] marks = {35,65,95}; System.out.print(marks.length + "," + marks[1]);
A) 2,65
B) 3,95
C) 3,65
D) Compiler error
Answer [=] C
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?
int[] balls = {}; System.out.print(balls.length);
A) 0
B) -1
C) 1
D) Compiler error
Answer [=] A
13) Which is the correct way of knowing Array Size in Java?
A)
//int[] ary; ary.length()
B)
//int[] ary; ary.length
C)
//int[] ary; ary->length()
D)
//int[] ary; ary->length
Answer [=] B
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]);
A) RED
B) YELLOW
C) WHITE
D) Compiler error
Answer [=] D
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]); } }
A) RAM
B) HDD
C) MOUSE
D) Compiler error
Answer [=] C
16) What is the output of the below Java program?
int ages[3] = {25, 27, 30}; System.out.println(ages[1]);
A) 25
B) 27
C) 30
D) Compile error
Answer [=] D
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)
A) FALSE
B) TRUE
Answer [=] B
Explanation:
WRONG: int ary[2]={34,45}; RIGHT: int ary[] ={34,45};
18) If an index of an element is N, what is its actual position in the array?
A) N-1
B) N
C) N+1
D) N+2
Answer [=] C
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)
A) FALSE
B) TRUE
Answer [=] A
Explanation:
An array can be of any data type, pri-mitive or Object type.
20) The name of an array variable or identifier can start with ___.
A) A letter
B) Underscore ( _ )
C) Dollar Symbol ($)
D) All
Answer [=] D
21) Shorthand array initialization in Java needs the keyword “new” to allocate memory to the array and elements. State TRUE or FALSE.
A) FALSE
B) TRUE
Answer [=] A
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.
A) FALSE
B) TRUE
Answer [=] B
Explanation:
int[] ary; ary = new int[5];
23) What is the default value of an element of Object type array?
A) 0
B) null
C) -1
D) Garbage value
Answer [=] B
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?
A) -1
B) 1
C) 0
D) Garbage value
Answer [=] C
25) What is the default value of float or double data type elements of an array in Java?
A) 0
B) 0.0
C) 1
D) 1.0
Answer [=] B
26) What is the default value of a char data type elements of an array in Java?
A) ‘A’
B) ‘\0’
C) null
D) ‘\0’ or null
Answer [=] D
Explanation:
null is nothing but ‘\0’.
27) What is the default value of boolean data type elements of an array in Java?
A) true
B) false
Answer [=] B
28) Allocating memory with the keyword “new” causes the array elements to carry default values. State TRUE or FALSE.
A) FALSE
B) TRUE
Answer [=] B
29) What is the output of the below Java program?
int balls[], rounds=3; balls = new int[rounds]; for(int i=0; i<balls.length; i++) balls[i] = (i+1)*2; for(int j=0; j<balls.length; j++) System.out.print(balls[j] + ",");
A) 0,2,4,
B) 1,2,3,
C) 2,4,6,
D) Compiler error
Answer [=] C
30) What is the output of the below Java program with arrays?
String[] ary = {"KITE", "AIR"}; String str = "PLANE"; ary[1] = str; str = "FLY"; System.out.println(ary[1]);
A) AIR
B) PLANE
C) FLY
D) Compiler error
Answer [=] B
31) An array of arrays in Java is called ___ array.
A) Bidirectional
B) Combo
C) Multidimensional
D) Multi-value
Answer [=] C
32) A multidimensional array contains elements of the same data-type in all rows and columns. State TRUE or FALSE.
A) FALSE
B) TRUE
Answer [=] C
33) An array of dimension N contains __ number of subscripts or brackets?
A) N-1
B) N
C) N+1
D) 10*N
Answer [=] B
34) An array with two dimensions is called a two-dimensional array in Java. State TRUE or FALSE.
A) TRUE
B) FALSE
Answer [=] A
35) Row number and Column number in a Multidimensional array start with ___.
A) -1
B) 0
C) 1
D) 2
Answer [=] B
36) A 4-dimensional array is an array of ___ dimensional arrays.
A) 4
B) 3
C) 2
D) 1
Answer [=] B
Explanation:
An N-dimensional array is an array of (N-1) dimensional arrays.
37) Choose the correct way of initializing a multidimensional array below.
A)
int[][] code = {{1,2},{3,4,5}};
B)
int[2][] code = {{1,2},{3,4,5}};
C)
int[][] code={1,2, 3,4,5};
D) All
Answer [=] A
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?
int[][] goats; goats = new int[3][]; goats[0] = {1,2}; System.out.println(goats[0][1]);
A) 0
B) 1
C) 2
D) Compiler error
Answer [=] D
Explanation:
It is lazy initialization. So, you can not use braces { } to initialize. Use the keyword new. goats[0] = new int[2]; goats[0][0] = 1; goats[0][1]=2; System.out.println(goats[0][1]); //Output: 2
39) State TRUE or FALSE. In a multidimensional array, the number of Columns in each Row can be different.
1 2 3 4 5 6
A) FALSE
B) TRUE
Answer [=] B
40) While mentioning the array size in a multidimensional array using the new keyword, the left-most script is mandatory. State TRUE or FALSE.
int ary[][]; ary = new int[5][];//first dimension is compulsory.
A) FALSE
B) TRUE
Answer [=] B