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


2. Array data access using _____.

A. Operator
B. Variable
C. index
D. Pointer

View Answer


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


4. Java Array can allocate __________.

A. Dynamic Memory
B. Static Memory
C. Both A and B
D. None of the above

View Answer


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


6. Index in array start with ______.

A. -1
B. 0
C. 1
D. infinite

View Answer


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


8. We can calculate the length of an array using ________.

A. sizeof(array)
B. array.len
C. array.length
D. array.sizeof()

View Answer


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


10. In java, array elements are stored in ________ memory locations.

A. Random
B. Sequential
C. Sequential & Random
D. Binary search

View Answer


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


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


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


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


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


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


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


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


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


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


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


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


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


24. How to sort an array?

A. Array.sort()
B. Arrays.sort()
C. Collection.sort()
D. System.sort()

View Answer


25. How to copy contents of array?

A. System.arrayCopy()
B. Array.copy()
C. Arrays.copy()
D. Collection.copy()

View Answer





Also check :

Discussion



* You must be logged in to add comment.