Java - MCQ

Multiple Choice Questions


Share with a Friend

 

Java MCQ - Methods and Arrays - Set 32

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
View Answer
Correct: 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
View Answer
Correct: 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
View Answer
Correct: 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
View Answer
Correct: 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
View Answer
Correct: C



 
 
 
   
   
   
UltraInfo Technologies
ITDeveloper