Object Oriented Programming - I (3140705) MCQs

MCQs of Methods and Arrays

Showing 31 to 40 out of 44 Questions
31.
An array elements are always stored in ________ memory locations.
(a) Sequential
(b) Random
(c) Sequential and Random
(d) Binary search
Answer:

Option (a)

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

Option (a)

33.
In Java arrays are
(a) objects
(b) object references
(c) primitive data type
(d) None of the above
Answer:

Option (a)

34.
Which one of the following is a valid statement?
(a) char[] c = new char();
(b) char[] c = new char[5];
(c) char[] c = new char(4);
(d) char[] c = new char[];
Answer:

Option (b)

35.
What is the result of compiling and running the following code?
public class Test{
        public static void main(String[] args){
                int[] a = new int[0];
                System.out.print(a.length);
        }
}
(a) 0
(b) Compilation error, arrays cannot be initialized to zero size.
(c) Compilation error, it is a.length() not a.length
(d) None of the above
Answer:

Option (a)

36.
What will be the output?
public class Test{
        public static void main(String[] args){
                int[] x = new int[3];
                System.out.println("x[0] is " + x[0]);
        }
}
(a) The program has a compile error because the size of the array wasn't specified when declaring the array.
(b) The program has a runtime error because the array elements are not initialized.
(c) The program runs fine and displays x[0] is 0.
(d) The program has a runtime error because the array element x[0] is not defined.
Answer:

Option (c)

37.
What is the output of the following code?
public class Test{
        public static void main(String args[]){
                double[] myList = {1, 5, 5, 5, 5, 1};
                double max = myList[0];
                int indexOfMax = 0;
                for(int i = 1; i < myList.length; i++){
                        if(myList[i] > max){
                                max = myList[i];
                                indexOfMax = i;
                        }
                }
                System.out.println(indexOfMax);
        }
}
(a) 0
(b) 1
(c) 2
(d) 3
Answer:

Option (b)

38.
Determine output:
public class Test{
        public static void main(String[] args){
                int[] x = {1, 2, 3, 4};
                int[] y = x;

                x = new int[2];

                for(int i = 0; i < x.length; i++)
                        System.out.print(y[i] + " ");
        }
}
(a) 1 2 3 4
(b) 0 0 0 0
(c) 1 2
(d) 0 0
Answer:

Option (c)

39.
Analyze the following code and choose the correct answer.
int[] arr = new int[5];
arr = new int[6];
(a) The code has compile errors because the variable arr cannot be changed once it is assigned.
(b) The code has runtime errors because the variable arr cannot be changed once it is assigned.
(c) The code can compile and run fine. The second line assigns a new array to arr.
(d) The code has compile errors because we cannot assign a different size array to arr.
Answer:

Option (c)

40.
What will be the output?
public class Test{
        public static void main(String[] args){
                int[] a = new int[4];
                a[1] = 1;
                a = new int[2];
                System.out.println("a[1] is " + a[1]);
        }
}
(a) The program has a compile error because new int[2]
(b) The program has a runtime error because a[1]
(c) a[1] is 0
(d) a[1] is 1
Answer:

Option (c)

Showing 31 to 40 out of 44 Questions