Sample test in Java.

1. Write a function that accepts an array of non-negative integers and returns the second largest integer in the array. Return -1 if there is no second largest.

2. Write a function that takes an array of integers as an argument and returns a value based on the sums of the even and odd numbers in the array. Let X = the sum of the odd numbers in the array and let Y = the sum of the even numbers. The function should return X – Y

3. Write a function that accepts a character array, a zero-based start position and a length. It should return a character array containing containing length characters starting with the start character of the input array. The function should do error checking on the start position and the length and return null if the either value is not legal.

public class Main {

/** Creates a new instance of Main */

public Main() {

}

/*

1. Write a function that accepts an array of non-negative integers and returns the second largest integer in the array. Return -1 if there is no second largest.

*/

public int getSecondLargestValue(int[] a){

int max1 = -1;

int max2 = -1;

for (int i=0; i

max2 = max1;

max1 = a[i];

} else if (a[i] != max1 && a[i] > max2)

max2 = a[i];

}

return max2;

}

/*

2. Write a function that takes an array of integers as an argument and returns a value based on the sums of the even and odd numbers in the array. Let X = the sum of the odd numbers in the array and let Y = the sum of the even numbers. The function should return X – Y

*/

public int getSumOddEven(int[] vals){

int x = 0;//odd value

int y = 0;//even value

int len = vals.length;

//calculat odd and even value

for(int oe=0; oe

return null;

}

char[] sub = new char[length];

for (int i=start, j=0; j

## Leave a Reply