250+ TOP MCQs on Java.util – Array Class and Answers

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?
a) Array
b) LinkedList
c) HashSet
d) AbstractSet

Answer: a
Clarification: None.

2. Which of this method is used to make all elements of an equal to specified value?
a) add()
b) fill()
c) all()
d) set()

Answer: b
Clarification: fill() method assigns a value to all the elements in an array, in other words, it fills the array with specified value.

3. 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
Clarification: None.

4. Which of these methods can be used to search an element in a list?
a) find()
b) sort()
c) get()
d) binaryserach()

Answer: d
Clarification: binaryserach() method uses binary search to find a specified value. This method must be applied to sorted arrays.

5. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class Arraylist 
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             ArrayList obj1 = new ArrayList();
  7.             ArrayList obj2 = new ArrayList();
  8.             obj1.add("A");
  9.             obj1.add("B");
  10.             obj2.add("A");
  11.             obj2.add(1, "B");
  12.             System.out.println(obj1.equals(obj2));
  13.         }
  14.     }

a) 0
b) 1
c) true
d) false

Answer: c
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

6. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class Array
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             int array[] = new int [5];
  7.             for (int i = 5; i > 0; i--)
  8.                 array[5 - i] = i;
  9.             Arrays.sort(array);
  10.             for (int i = 0; i < 5; ++i)
  11.             	System.out.print(array[i]);;
  12.         }
  13.     }

a) 12345
b) 54321
c) 1234
d) 5432

Answer: a
Clarification: Arrays.sort(array) method sorts the array into 1,2,3,4,5.
Output:

$ javac Array.java
$ java Array
12345

7. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class Array 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             int array[] = new int [5];
  7.             for (int i = 5; i > 0; i--)
  8.                 array[5 - i] = i;
  9.             Arrays.sort(array);
  10.             System.out.print(Arrays.binarySearch(array, 4));
  11.         }
  12.     }

a) 2
b) 3
c) 4
d) 5

Answer: b
Clarification: None.
Output:

$ javac Array.java
$ java Array
3

  250+ TOP MCQs on Command Line Arguments – 1 and Answers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top