Java Programming Multiple Choice Questions - Array
This section focuses on the "Array" in Java programming. These Multiple Choice Questions (MCQ) should be practiced to improve the Java programming skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements and other competitive examinations.
1. Java array is a collection of ________.
A. similar type of elements
B. different type of element
C. heterogeneous data
D. Both A and C
View Answer
Ans : A
Explanation: An array is a collection of similar type of elements which has contiguous memory location.
2. Array data access using _____.
A. Operator
B. Variable
C. index
D. Pointer
View Answer
Ans : C
Explanation: Array data access using index.
3. At time of array initialization which is necessary to specify?
A. Row
B. Column
C. Row and Column
D. None of the above
View Answer
Ans : A
Explanation: Row is necessary to specify at time of array initialization.
4. Java Array can allocate __________.
A. Dynamic Memory
B. Static Memory
C. Both A and B
D. None of the above
View Answer
Ans : B
Explanation: Arrays in java are static lists that can store a certain kind of variables. Therefore these arrays need to be initialized at the compile time.
5. Which of the following is an incorrect array declaration?
A. int [] arr = new int[5].
B. int arr[] = new int[5].
C. int arr[] = new int[5].
D. int arr[] = int [5] new
View Answer
Ans : D
Explanation: int arr[] = int [5] is an incorrect array declaration because Operator new must be succeeded by array type and array size.
6. Index in array start with ______.
A. -1
B. 0
C. 1
D. infinite
View Answer
Ans : B
Explanation: Index in array start with 0.
7. Which of the following is used to declare,construct, and initlaize an array?
A. int arr [] [] = {1, 2, 3, 4};
B. int [] arr = (1, 2, 3);
C. int [] arr = {};
D. int arr [] = {1, 2, 3};
View Answer
Ans : D
Explanation: int arr [] = {1, 2, 3}; is used to declare,construct, and initlaize an array becuase
Option A is wrong because it initializes an int array with String literals.
Option B is wrong because it uses something other than curly braces for the initialization.
Option C is wrong because it provides initial values for only one dimension, although the declared array is a two-dimensional array.
8. We can calculate the length of an array using ________.
A. sizeof(array)
B. array.len
C. array.length
D. array.sizeof()
View Answer
Ans : C
Explanation: We can calculate the length of an array using array.length.
9. Which of the following is advantage of java array?
A. Code Optimization
B. Random access
C. Size No-Limit
D. Both A and B
View Answer
Ans : A
Explanation: Code Optimization and Random access the following is advantage of java array.
10. In java, array elements are stored in ________ memory locations.
A. Random
B. Sequential
C. Sequential & Random
D. Binary search
View Answer
Ans : B
Explanation: Array elements are stored in contiguous memory. Linked List is stored in random memory locations.
11. What will be the output of the program?
class Main
{
public static void main(String args[]) {
int arr[] = {10, 20, 30, 40, 50};
for(int i=0; i < arr.length; i++)
{
System.out.print(" " + arr[i]);
}
}
}
A. 10 20 30 40 50
B. Compiler Error
C. 10 20 30 40
D. None of the above
View Answer
Ans : A
Explanation: It is a simple program where an array is first created then traversed. The important thing to note is, unlike C++, arrays are first class objects in Java. For example, in the following program, size of array is accessed using length which is a member of arr[] object.
12. What will be the output of the program?
int arr[] = new int [5];
System.out.print(arr);
A. 0
B. value stored in arr[0].
C. 0
D. Class name@ hashcode in hexadecimal form
View Answer
Ans : D
Explanation: If we trying to print any reference variable internally, toString() will be called which is implemented to return the String
13. What will be the output of the program?
class Main
{
public static void main(String args[])
{
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i)
{
array_variable[i] = i;
System.out.print(array_variable[i] + " ");
i++;
}
}
}
A. 0 2 4 6 8
B. 1 3 5 7 9
C. 0 1 2 3 4 5 6 7 8 9
D. 1 2 3 4 5 6 7 8 9 10
View Answer
Ans : A
Explanation: When an array is declared using new operator then all of its elements are initialized to 0 automatically. for loop body is executed 5 times as whenever controls comes in the loop i value is incremented twice, first by i++ in body of loop then by ++i in increment condition of for loop.
14. What will be output for the following code?
class Main
{
public static void main(String args[])
{
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 6;
n = arr[arr[n] / 2];
System.out.print(n);
}
}
A. 3
B. 0
C. 6
D. 1
View Answer
Ans : A
Explanation: Array arr contains 10 elements. n contains 6 thus in next line n is given value 2 printing arr[arr[6]/2] i:e arr[3] = 3.
15. Predict the output of following Java Program?
class Main
{
public static void main(String args[])
{
char array_variable [] = new char[10];
for (int i = 0; i < 10; ++i)
{
array_variable[i] = 'i';
System.out.print(array_variable[i] + " ");
}
}
}
A. 1 2 3 4 5 6 7 8 9 10
B. 0 1 2 3 4 5 6 7 8 9 10
C. i j k l m n o p q r
D. i i i i i i i i i i
View Answer
Ans : D
Explanation: No explantion.
16. What will be output for the following code?
class Test {
public static void main(String args[]) {
int arr[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
A. 0 0
B. garbage value
garbage value
C. Compiler Error
D. Exception
View Answer
Ans : C
Explanation: In Java, it is not allowed to put the size of the array in the declaration because an array declaration specifies only the element type and the variable name. The size is specified when you allocate space for the array. .
17. What will be output for the following code?
class Test {
public static void main(String args[]) {
int arr[] = new int[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
A. 0 0
B. garbage value
garbage value
C. Compiler Error
D. Exception
View Answer
Ans : A
Explanation: Java arrays are first class objects and all members of objects are initialized with default values like o, null.
18. What will be output for the following code?
class array_output
{
public static void main(String args[])
{
int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}};
int sum = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3 ; ++j)
sum = sum + array_variable[i][j];
System.out.print(sum / 5);
}
}
A. 8
B. 9
C. 10
D. 11
View Answer
Ans : B
Explanation: No explanation.
19. What will be the output of the program?
class Main
{
public static void main (String[] args)
{
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (arr1 == arr2)
System.out.println("Same");
else
System.out.println("Not same");
}
}
A. Same
B. Not Same
C. Compiler error
D. None of the above
View Answer
Ans : B
Explanation: No explanation.
20. What is the output of this program?
class Main
{
public static void main (String[] args)
{
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (arr1.equals(arr2))
System.out.println("Same");
else
System.out.println("Not same");
}
}
A. Same
B. Not Same
C. Compiler error
D. None of the above
View Answer
Ans : B
Explanation: arr1.equals(arr2) is same as (arr1 == arr2)
21. Which of these is an incorrect Statement?
A. It is necessary to use new operator to initialize an array
B. Array can be initialized using comma separated expressions surrounded by curly braces
C. Array can be initialized when they are declared
D. None of the mentioned
View Answer
Ans : A
Explanation: Array can be initialized using both new and comma separated expressions surrounded by curly braces example : int arr[5] = new int[5]; and int arr[] = { 0, 1, 2, 3, 4};
22. What is the type of variable "b" and "d" in the below snippet?
int a[], b;
int []c, d;
A. "b" and "d" are int
B. "b" and "d" are arrays of type int
C. "d" is int variable; and "b" is int array
D. "b" is int variable; and "d" is int array
View Answer
Ans : D
Explanation: If [] is declared after variable it is applicable only to one variable. If [] is declared before variable it is applicable to all the variables.
23. Which of these is necessary to What is the output of below snippet?specify at time of array initialization?
Object[] names = new String[3];
names[0] = new Integer(0);
A. ArrayIndexOutOfBoundsException
B. ArrayStoreException
C. Compilation Error
D. Code runs successfully
View Answer
Ans : B
Explanation: ArrayIndexOutOfBoundsException comes when code tries to access an invalid index for a given array. ArrayStoreException comes when you have stored an element of type other than the type of array..
24. How to sort an array?
A. Array.sort()
B. Arrays.sort()
C. Collection.sort()
D. System.sort()
View Answer
Ans : B
Explanation: Arrays class contains various methods for manipulating arrays (such as sorting and searching). Array is not a valid class.
25. How to copy contents of array?
A. System.arrayCopy()
B. Array.copy()
C. Arrays.copy()
D. Collection.copy()
View Answer
Ans : A
Explanation: Arrays class contains various methods for manipulating arrays (such as sorting and searching). Array is not a valid class.
Also check :
Discussion