Java Programming - Arrays Questions And Answers

Related Topics:

Java Programming : Java Arrays

This is Java Programming Section with Multiple Choice Questions on Java Arrays. Single dimensional and Multi dimensional Arrays Questions with Explanations for Java Freshers.
1. Which of these function is used to allocate memory to array variable in Java?
[A] malloc
[B] alloc
[C] new
[D] calloc

Answer: C. new

Explanation:

Operator new allocates block of memory specified by the size of array, and gives the reference of memory allocated to the array variable.
2. 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

Answer: A. Row

Explanation:

Row is necessary to declare the array.
3. What will be printed using following code block?
int[] a = {0,1,2,3,4,5,6,7};
System.out.println(a.length);
[A] 6
[B] 7
[C] 8
[D] 9

Answer: C. 8

Explanation:

The length of an array is equal to the number of elements in the array.In the above array, there are 8 elements.
4. What will happen when following Java code block is compiled and run?
public class A {

 public static void main(String argv[]){ 

 int ary[]=new int[]{1,2,3};

 System.out.println(ary[1]); 

    } 

}
[A] 1
[B] Compilation Error: Incorrect Syntax
[C] 2
[D] Compilation Error: size of array must be defined

Answer: C. 2

Explanation:

Here a new array is created, the elements 1,2,3 are inserted in it and the reference is stored in the "ary" variable. ary[1] refers to the second element in the array as array index starts from 0.
5.
What will happen if following code block is compiled/run?
int[] iArray = new int[10];

iArray.length = 15;

System.out.println(iArray.length);
[A] Prints 10
[B] Prints 15
[C] Compilation Error
[D] Run Time Error

Answer: C. Compilation Error

Explanation:

Explanation:Once an array is created, it is not possible to change the length of the array.
6. 6.Will the following program compile successfully ?

class MyArray 

{

 public static void main(String[] args) 

    {

int arr1[]=new int[2]{1,2};

 int arr2[]=new int[]{1,2,3};    

    }

}
[A] Yes
[B] No

Answer: B. No

Explanation:

If you are initialising an array you can not define the size of the array at the same time.
7.
Given a one dimensional array arr, what is the correct way of getting the number of elements in arr.

Select the one correct answer.
[A] arr.length
[B] arr.length - 1
[C] arr.size
[D] arr.size - 1

Answer: A. arr.length

8. 8.What is the output of the following program?

class MyMethod 

{

 static int func( int i)

    {

 return  ++i;

    }

   public static void main(String[] args) 

    {

        int i=0;

        int j=func(i++) + i + func(++i);

        System.out.println(i);

        System.out.println(j);

    }

}
[A] 5 2
[B] 2 5
[C] 2 6
[D] 2 4

Answer: B. 2 5

Explanation:

j=func(i++)+ i + func(++i) = func(0)+ 1 + func(2)= 1 + 1 + 3 = 5

i was incremented twice hence i becomes 2.

O/p:2 5
9. What is the output of this program?

  class evaluate {

        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.println(arr[n] / 2);

            }
}
[A] 3
[B] 0
[C] 6
[D] 1

Answer: D. 1

Explanation:

Array arr contains 10 elements. n contains 6 thus in next line n is given value 2 printing

arr[2]/2 i:e 2/2 = 1.
10. What is the output of this program?
class array_output {
        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

Answer: D. i i i i i i i i i i


Comments: (Your feedback is valuable to us)

how will 2 months ago Reply

how will you convert benzene into glyoxal?


Gidraf Kungu 1 year ago Reply

great


yogesh rawat 1 year ago Reply

great


alpesh sakhanpara 1 year ago Reply

U sa


;