Question:

Which of the following method of Arrays class is used to fill the whole or partial array with specified value in Java?

Show Hint

The {Arrays.fill()} method quickly initializes or resets all elements of an array to the same value.
  • sort()
  • binarySort()
  • fill()
  • binaryFill()
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is C

Solution and Explanation

Step 1: Understanding the Arrays class in Java.
Java provides a utility class called Arrays in the package java.util. This class contains many static methods used to manipulate arrays easily, such as sorting arrays, searching elements, comparing arrays, and filling arrays with values.
Step 2: Purpose of the fill() method.
The Arrays.fill() method is used to assign a specified value to every element of an array or to a specific range within the array. This method is useful when programmers want to initialize or reset all elements of an array to a particular value.
Example:
int arr[] = new int[5];
Arrays.fill(arr, 10);
After execution, the array becomes:
[10, 10, 10, 10, 10]
It can also fill a specific portion of the array:
Arrays.fill(arr, 1, 3, 5);
This fills the array from index 1 to index 2 with value 5.
Step 3: Evaluation of options.
(A) sort(): Incorrect. This method sorts elements of an array.
(B) binarySort(): Incorrect. This is not a valid method in the Arrays class.
(C) fill(): Correct. It fills an array or part of an array with a specified value.
(D) binaryFill(): Incorrect. This is not a valid Java method.
Step 4: Conclusion.
Thus, the method used to fill the whole or part of an array with a specified value is fill().
Final Answer: fill()
Was this answer helpful?
0
0