Java MCQ – Array

This section of our 1000+ Java MCQs focuses on “Array”

1. Which of these operators is used to allocate memory to array variable in Java?
a) malloc
b) alloc
c) new
d) new malloc
View Answer

Answer: c
Explanation: Operator new allocates a block of memory specified by the size of an array, and gives the reference of memory allocated to the array variable.

2. Which of these 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

Answer: d
Explanation: Operator new must be succeeded by array type and array size.

3. What will be the output of the following Java code?

advertisement
advertisement
    int arr[] = new int [5];
    System.out.print(arr);

a) 0
b) value stored in arr[0]
c) 00000
d) Class name@ hashcode in hexadecimal form
View Answer

Answer: d
Explanation: If we trying to print any reference variable internally, toString() will be called which is implemented to return the String in following form:
classname@hashcode in hexadecimal form

4. 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

Answer: 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};
Note: Join free Sanfoundry classes at Telegram or Youtube

5. Which of these is necessary to specify at time of array initialization?
a) Row
b) Column
c) Both Row and Column
d) None of the mentioned
View Answer

Answer: a
Explanation: None.
advertisement

6. What will be the output of the following Java code?

  1.     class array_output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             int array_variable [] = new int[10];
  6. 	    for (int i = 0; i < 10; ++i) 
  7.             {
  8.                 array_variable[i] = i;
  9.                 System.out.print(array_variable[i] + " ");
  10.                 i++;
  11.             }
  12.         } 
  13.     }

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

Answer: 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.
output:

advertisement
$ javac array_output.java
$ java array_output
0 2 4 6 8

7. What will be the output of the following Java code?

  1.     class multidimention_array 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             int arr[][] = new int[3][];
  6.             arr[0] = new int[1];
  7.             arr[1] = new int[2];
  8.             arr[2] = new int[3];               
  9. 	    int sum = 0;
  10. 	    for (int i = 0; i < 3; ++i) 
  11. 	        for (int j = 0; j < i + 1; ++j)
  12.                     arr[i][j] = j + 1;
  13. 	    for (int i = 0; i < 3; ++i) 
  14. 	        for (int j = 0; j < i + 1; ++j)
  15.                     sum + = arr[i][j];
  16. 	    System.out.print(sum); 	
  17.         } 
  18.     }

a) 11
b) 10
c) 13
d) 14
View Answer

Answer: b
Explanation: arr[][] is a 2D array, array has been allotted memory in parts. 1st row contains 1 element, 2nd row contains 2 elements and 3rd row contains 3 elements. each element of array is given i + j value in loop. sum contains addition of all the elements of the array.
output:

$ javac multidimention_array.java
$ java multidimention_array
10

8. What will be the output of the following Java code?

  1.     class evaluate 
  2.     {
  3.         public static void main(String args[]) 
  4.             {
  5. 	        int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
  6. 	        int n = 6;
  7.                 n = arr[arr[n] / 2];
  8. 	        System.out.println(arr[n] / 2);
  9.             } 
  10.     }

a) 3
b) 0
c) 6
d) 1
View Answer

Answer: d
Explanation: Array arr contains 10 elements. n contains 6 thus in next line n is given value 3 printing arr[3]/2 i:e 3/2 = 1 because of int Value, by int values there is no rest. If this values would be float the result would be 1.5.
output:

$ javac evaluate.java
$ java evaluate
1

9. What will be the output of the following Java code?

  1.     class array_output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             char array_variable [] = new char[10];
  6. 	    for (int i = 0; i < 10; ++i) 
  7.             {
  8.                 array_variable[i] = 'i';
  9.                 System.out.print(array_variable[i] + "");
  10.             }
  11.         } 
  12.     }

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

Answer: d
Explanation: None.
output:

$ javac array_output.java
$ java array_output
i i i i i i i i i i

10. What will be the output of the following Java code?

  1.     class array_output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}};
  6.             int sum = 0;
  7.             for (int i = 0; i < 3; ++i)
  8.                 for (int j = 0; j <  3 ; ++j)
  9.                     sum = sum + array_variable[i][j];
  10.             System.out.print(sum / 5);
  11.         } 
  12.     }

a) 8
b) 9
c) 10
d) 11
View Answer

Answer: b
Explanation: None.
output:

$ javac array_output.java
$ java array_output
9

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.