Question on Arrays



Question on Arrays

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

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

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

3. What will this code print?
    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

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

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};

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

Answer: a
Explanation: None.

6. What is the output of this program?
    class array_output
    {
        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

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:
$ javac array_output.java
$ java array_output
0 2 4 6 8

7. What is the output of this program?
    class multidimention_array
    {
        public static void main(String args[])
        {
            int arr[][] = new int[3][];
            arr[0] = new int[1];
            arr[1] = new int[2];
            arr[2] = new int[3];              
                int sum = 0;
                for (int i = 0; i < 3; ++i)
                    for (int j = 0; j < i + 1; ++j)
                    arr[i][j] = j + 1;
                for (int i = 0; i < 3; ++i)
                    for (int j = 0; j < i + 1; ++j)
                    sum + = arr[i][j];
                System.out.print(sum);           
        }
    }
a) 11
b) 10
c) 13
d) 14

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 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
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.
output:
$ javac evaluate.java
$ java evaluate
1

9. 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
Explanation: None.
output:
$ javac array_output.java
$ java array_output
i i i i i i i i i i

10. What is the output of this program?
    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

Answer: b
Explanation: None.
output:
$ javac array_output.java
$ java array_output
9

11. 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) ‘b’ is int variable; ‘d’ is int array
d) ‘d’ is int variable; ‘b’ is int array

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

12. Which of these is an incorrect array declaration?
a) int arr[] = new int[5] ;
b) int [] arr = new int[5] ;
c)
int arr[];
arr = new int[5];
d) int arr[] = int [5] new;

Answer: d
Explanation: Operator new must be succeeded by array type and array size. The order is important and determines the type of variable.

13. What will this code print?
int arr[] = new int [5];
System.out.print(arr);
a) 0
b) value stored in arr[0].
c) 00000
d) Garbage value

Answer: d
Explanation: arr is an array variable, it is pointing to array of integers. Printing arr will print garbage value. It is not same as printing arr[0].

14. What is the output of below snippet?
Object[] names = new String[3];
names[0] = new Integer(0);
a) ArrayIndexOutOfBoundsException
b) ArrayStoreException
c) Compilation Error
d) Code runs successfully

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

15. Generics does not work with?
a) Set
b) List
c) Tree
d) Array

Answer: d
Explanation: Generics gives the flexibility to strongly typecast collections. Generics is applicable to Set, List and Tree. It is not applicable to Array.

16. How to sort an array?
a) Array.sort()
b) Arrays.sort()
c) Collection.sort()
d) System.sort()

Answer: b
Explanation: Arrays class contains various methods for manipulating arrays (such as sorting and searching). Array is not a valid class.

17. How to copy contents of array?
a) System.arrayCopy()
b) Array.copy()
c) Arrays.copy()
d) Collection.copy()

Answer: a
Explanation: Arrays class contains various methods for manipulating arrays (such as sorting and searching). Array is not a valid class.

18. Can you make an array volatile?
a) True
b) False

Answer: a
Explanation: You can only make variable pointing to array volatile. If an array is changed by replacing individual elements then guarantee provided by volatile variable will not be held.

19. Where is array stored in memory?
a) heap space
b) stack space
c) heap space and stack space
d) first generation memory

Answer: a
Explanation: Array is stored in heap space. Whenever an object is created, it’s always stored in the Heap space and stack memory contains the reference to it.

20. An array elements are always stored in ________ memory locations?
a) Sequential
b) Random
c) Sequential and Random
d) Binary search

Answer: a
Explanation: Array elements are stored in contiguous memory. Linked List is stored in random memory locations.
Question on Arrays Question on Arrays Reviewed by Unknown on February 24, 2019 Rating: 5

No comments:

If you have any doubt or query ,comment below:

Programming copyright © 2018-19. Powered by Blogger.