Object Oriented Programming - I (3140705) MCQs

MCQs of Methods and Arrays

Showing 21 to 30 out of 44 Questions
21.
What will be the output of the following Java code?
    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:

Option (b)

22.
What will be the output of the following Java code?
    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:

Option (d)

23.
What will be the output of the following Java code?
    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:

Option (d)

24.
What will be the output of the following Java 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
Answer:

Option (b)

25.
What is the type of variable ‘b’ and ‘d’ in the following Java 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:

Option (c)

26.
What will be the output of the following Java code snippet?
Object[] names = new String[3];
names[0] = new Integer(0);
(a) ArrayIndexOutOfBoundsException
(b) ArrayStoreException
(c) Compilation Error
(d) Code runs successfully
Answer:

Option (b)

27.
Generics does not work with?
(a) Set
(b) List
(c) Tree
(d) Array
Answer:

Option (d)

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

Option (b)

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

Option (a)

30.
Where is an array stored in memory?
(a) heap space
(b) stack space
(c) heap space and stack space
(d) first generation memory
Answer:

Option (a)

Showing 21 to 30 out of 44 Questions