Java MCQs on Array class under java.util library of Java Programming Language.
1. Which of these standard collection classes implements all the standard functions on list data structure? Answer: a 2. Which of this method is used to make all elements of an equal to specified value? Answer: b 3. Which of these method of Array class is used sort an array or its subset? Answer: c 4. Which of these methods can be used to search an element in a list? Answer: d 5. What will be the output of the following Java program? a) 0 6. What will be the output of the following Java program? a) 12345 7. What will be the output of the following Java program? a) 2
a) Array
b) LinkedList
c) HashSet
d) AbstractSet
Clarification: None.
a) add()
b) fill()
c) all()
d) set()
Clarification: fill() method assigns a value to all the elements in an array, in other words, it fills the array with specified value.
a) binarysort()
b) bubblesort()
c) sort()
d) insert()
Clarification: None.
a) find()
b) sort()
c) get()
d) binaryserach()
Clarification: binaryserach() method uses binary search to find a specified value. This method must be applied to sorted arrays.
import java.util.*;
class Arraylist
{
public static void main(String args[])
{
ArrayList obj1 = new ArrayList();
ArrayList obj2 = new ArrayList();
obj1.add("A");
obj1.add("B");
obj2.add("A");
obj2.add(1, "B");
System.out.println(obj1.equals(obj2));
}
}
b) 1
c) true
d) false
Clarification: obj1 and obj2 are an object of class ArrayList hence it is a dynamic array which can increase and decrease its size. obj.add(“X”) adds to the array element X and obj.add(1,”X”) adds element x at index position 1 in the list, Both the objects obj1 and obj2 contain same elements i:e A & B thus obj1.equals(obj2) method returns true.
Output:
$ javac Arraylist.java
$ java Arraylist
true
import java.util.*;
class Array
{
public static void main(String args[])
{
int array[] = new int [5];
for (int i = 5; i > 0; i--)
array[5 - i] = i;
Arrays.sort(array);
for (int i = 0; i < 5; ++i)
System.out.print(array[i]);;
}
}
b) 54321
c) 1234
d) 5432
Clarification: Arrays.sort(array) method sorts the array into 1,2,3,4,5.
Output:
$ javac Array.java
$ java Array
12345
import java.util.*;
class Array
{
public static void main(String args[])
{
int array[] = new int [5];
for (int i = 5; i > 0; i--)
array[5 - i] = i;
Arrays.sort(array);
System.out.print(Arrays.binarySearch(array, 4));
}
}
b) 3
c) 4
d) 5
Clarification: None.
Output:
$ javac Array.java
$ java Array
3
