Java Multiple Choice Questions & Answers on Java.util package Array Class for Freshers

https://www.computersprofessor.com/2017/12/java-multiple-choice-questions-answers_22.html
1. Which of these standard collection classes implements all the standard functions on list data structure?
a) Array
b) LinkedList
c) HashSet
d) AbstractSet
Answer: a
2. Which of these classes implements Set interface?
a) ArrayList
b) HashSet
c) LinkedList
d) DynamicList
Answer: b
Explanation: HashSet and TreeSet implements Set interface where as LinkedList and ArrayList implements List interface.
3. Which of these method is used to make all elements of an equal to specified value?
a) add()
b) fill()
c) all()
d) set()
Answer: b
Explanation: fill() method assigns a value to all the elements in an array, in other words it fills the array with specified value.
4. Which of these method of Array class is used sort an array or its subset?
a) binarysort()
b) bubblesort()
c) sort()
d) insert()
Answer: c
5. Which of these methods can be used to search an element in a list?
a) find()
b) sort()
c) get()
d) binaryserach()
Answer: d
Explanation: binaryserach() method uses binary search to find a specified value. This method must be applied to sorted arrays.
6. Which of these method is used to change an element in a LinkedList Object?
a) change()
b) set()
c) redo()
d) add()
a) change()
b) set()
c) redo()
d) add()
Answer: b
Explanation: An element in a LinkedList object can be changed by first using get() to obtain the index or location of that object and the passing that location to method set() along with its new value.
7. What is the output of this program?
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));
}
}
a) 0
b) 1
c) true
d) false
b) 1
c) true
d) false
Answer: c
Explanation: 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.
8. What is the output of this program?
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]);;
}
}
a) 12345
b) 54321
c) 1234
d) 5432
b) 54321
c) 1234
d) 5432
Answer: a
Explanation: Arrays.sort(array) method sorts the array into 1,2,3,4,5.
9. What is the output of this program?
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.fill(array, 1, 4, 8);
for (int i = 0; i < 5 ; i++)
System.out.print(array[i]);
}
}
a) 12885
b) 12845
c) 58881
d) 54881
b) 12845
c) 58881
d) 54881
Answer: c
Explanation: array was containing 5,4,3,2,1 but when method Arrays.fill(array, 1, 4, 8) is called it fills the index location starting with 1 to 4 by value 8 hence array becomes 5,8,8,8,1.
10. What is the output of this program?
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));
}
}
a) 2
b) 3
c) 4
d) 5
b) 3
c) 4
d) 5
Answer: b