1 Explain how arrays are passed to methods as arguments 2Wri
1) Explain how arrays are passed to methods as arguments.
2)Write the declaration for an array of doubles called averages that is initialized with
an initializer list.
3) Write the declaration for a two-dimensional array of integers that can be thought of
as a table with three rows and three columns. Assign the value 3 to the cell that is
in the second row and the third column.
4)Write a loop that cycles through an array of String objects called names and prints
them out, one per line. Your loop should not use a foreach loop.
5) Write a loop that cycles through an array of String objects called names and prints
out the ones that start with a vowel, one per line. Your loop should use a foreach
loop.
6) Write a method that accepts an array of integers as a parameter and returns the
sum of all of the integers in the array.
7) Write a method called doubleSize that accepts an integer array as a parameter and
returns a reference to a new integer array that is twice as long and contains all of
the elements of the first array in the same positions.
8) Write a method called average that accepts an array of floating pointnumbers and
returns their average.
9) Write a method called containsPair that accepts an array of integers as a
parameter and returns true if it contains two integers that are equal.
10) Write a method thattakes in an arbitrary number of String objects, and then prints
out all of them that have over 10 characters.
11) Write a method that takes in at least one integer and returns the largest of all
integer parameters sent in.
12) Write a method that accepts an array of integers as a parameter and returns a
reference to an array that contains the even numbers in the array original array.
The returned array should have a size equal to the number of even numbers in the
original array.
13) Write a short program that accepts an arbitrary number of command line
arguments, and prints out those containing the character \'z\'.
14) Write a line of code that initializes a two-dimensional array of integers using an
initializer list.
15) Write a method that accepts an array of integers and returns the smallest value in
the list.
Solution
1) Explain how arrays are passed to methods as arguments.
Answer : array can be passed by reference
public class ArrayExample {
void a() {
int b[] = { 1, 2, 3, 4, 5, 6, 7 };
c(b);
}
void c(int b[]) {
int e = b.length;
for (int f = 0; f < e; f++) {
System.out.print(b[f] + \" \");// Single Space
}
}
public static void main(String args[]) {
ArrayExample obj = new ArrayExample();
obj.a();
}
}
2)Write the declaration for an array of doubles called averages that is initialized with
an initializer list.
Answer :
double[] averages = { 12.3, 44, 88.4 };
3) Write the declaration for a two-dimensional array of integers that can be thought of
as a table with three rows and three columns. Assign the value 3 to the cell that is
in the second row and the third column.
Answer :
int[][] table=new int[3][3];
table[1][2] = 3;
4)Write a loop that cycles through an array of String objects called names and prints
them out, one per line. Your loop should not use a foreach loop.
Answer :
String[] names = { \"rajesh\", \"pavan\", \"kishore\" };
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}
5) Write a loop that cycles through an array of String objects called names and prints
out the ones that start with a vowel, one per line. Your loop should use a foreach
loop.
Answer :
String[] names = { \"rajesh\", \"pavan\", \"kishore\" };
for (String string : names) {
System.out.println(string);
}
6) Write a method that accepts an array of integers as a parameter and returns the
sum of all of the integers in the array.
Answer :
public int sum(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
7) Write a method called doubleSize that accepts an integer array as a parameter and
returns a reference to a new integer array that is twice as long and contains all of
the elements of the first array in the same positions.
Answer :
public int[] doubleSize(int[] arr) {
int[] newArr = new int[arr.length * 2];
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i];
}
return newArr;
}
8) Write a method called average that accepts an array of floating pointnumbers and
returns their average.
public float average(float[] arr) {
float sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum / arr.length;
}
9) Write a method called containsPair that accepts an array of integers as a
parameter and returns true if it contains two integers that are equal.
Answer :
public boolean containsPair(int[] arr) {
if (arr[0] == arr[1])
return true;
else
return false;
}
10) Write a method thattakes in an arbitrary number of String objects, and then prints
out all of them that have over 10 characters.
Answer :
public void arbitraryStrObj(String... strings) {
for (int i = 0; i < strings.length; i++) {
if (strings[i].length() > 10) {
System.out.println(strings[i]);
}
}
}
calling method: arbitraryStrObj(\"Srinivas palli\", \"Pavan kumar\", \"rajesh\");
NOTE: please share remaining as different


