104k views
5 votes
Which of the following is a valid way to pass an array as a method parameter in java?

A) String[] stringArray = "Computer Science";

B) int[] myArray = new int[26];

C) double[3] nums = [3.5, 35.1, 32.0];

D) boolean[] words = new words[10];

User Jramm
by
7.5k points

1 Answer

5 votes

Final answer:

Option B) int[] myArray = new int[26]; is the correct and valid way to declare and initialize an array in Java to be passed as a method parameter.

Step-by-step explanation:

The question asks which option is a valid way to pass an array as a method parameter in Java. Among the given options, B) int[] myArray = new int[26]; is the correct way to declare an array in Java. Here's why the other options are incorrect:

  • A) String[] stringArray = "Computer Science"; is not valid because "Computer Science" is a String, not an array of Strings.
  • C) double[3] nums = [3.5, 35.1, 32.0]; is not valid syntax in Java. It should be double[] nums = {3.5, 35.1, 32.0}; to correctly declare an array.
  • D) boolean[] words = new words[10]; is incorrect because 'words' is not a data type. Correct syntax would be boolean[] words = new boolean[10];.

Therefore, the valid way to declare and initialize an array to pass as a method parameter in Java is the option B).

User Mathi
by
6.8k points