This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Data Structures-Arrays”.
1. 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
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.
2. 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;
View Answer
Explanation: Operator new must be succeeded by array type and array size. The order is important and determines the type of variable.
3. What will be the output of the following Java code snippet?
int sf_arr[] = new int [5];
System.out.print(sf_arr);
a) 00000
b) Value at sf_arr[0]
c) Error
d) Class name@ hashcode in hexadecimal form
View Answer
Explanation: In this Java code, we are trying to print the array sf_arr, but sf_arr is an array of integers, and when we directly print it using System.out.print, we will get a reference to the array object rather than the elements of the array, its format will be class name followed by an “@” sign and the object’s hash code in hexadecimal form.
4. 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
View Answer
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.
5. Generics does not work with?
a) Set
b) List
c) Tree
d) Array
View Answer
Explanation: Generics gives the flexibility to strongly typecast collections. Generics is applicable to Set, List and Tree. It is not applicable to Array.
6. How to sort an array?
a) Array.sort()
b) Arrays.sort()
c) Collection.sort()
d) System.sort()
View Answer
Explanation: The Arrays class contains various methods for manipulating arrays, such as sorting and searching. The sort() method is not available in the Array or System classes. Also, Collections.sort() does not work on arrays of primitive data types; it only works on collections like List. To sort arrays of primitive types, the Arrays.sort() method should be used instead.
7. How to copy contents of array?
a) System.arrayCopy()
b) Array.copy()
c) Arrays.copy()
d) Collection.copy()
View Answer
Explanation: Arrays class contains various methods for manipulating arrays. copy() is not a valid method for Array, Arrays, and Collections.copy() does not work on arrays of primitive data type.
8. Can you make an array volatile?
a) True
b) False
View Answer
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.
9. Where is an array stored in memory?
a) heap space
b) stack space
c) heap space and stack space
d) first generation memory
View Answer
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.
10. An array elements are always stored in ________ memory locations.
a) Sequential
b) Random
c) Sequential and Random
d) Binary search
View Answer
Explanation: Array elements are stored in contiguous memory. Linked List is stored in random memory locations.
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Programming Books
- Apply for Java Internship
- Check Java Books
- Apply for Computer Science Internship
- Practice Programming MCQs