1. Declare one-dimensional arrays according to the following descriptions. a. A 24-element float array b. A 500-element int array c. A 50-element double array d. A 10-element char array 2. Write a code fragment to do the following tasks: a. Declare a constant named CLASS_SIZE representing the number of students in a class. b. Declare an array QuizAvg of size CLASS_SIZE whose components will contain quiz score averages (double). 3. What is the output of the following program? The data for the program is given below. import java.util.Scanner; public class Array { public static void main(String [] args) { int [] a,b; int j, m, suma = 0, sumb = 0, sumdiff = 0; Scanner keyboard = new Scanner(System.in); a=new int[100]; b=new int[100]; m = keyboard.nextInt(); for (j = 0 ; j < m ; j++) { a[j]=keyboard.nextInt(); b[j]=keyboard.nextInt(); suma = suma + a[j]; sumb += b[j]; sumdiff = sumdiff + (a[j] - b[j]); } for (j = m - 1; j >= 0; j--) System.out.println(a[j] + " " + b[j] + " " + (a[j]-b[j])); System.out.println(suma + " " + sumb + " " + sumdiff); } } DATA: 5 11 15 19 14 4 2 17 6 1 3 4. Given the declarations int [] sample=new int[8]; int i, k; show the contents of the array sample after the following code segment is executed. Use a question mark to indicate any undefined values in the array. for (k=0; k<8;k++) sample[k] = 10 - k; 5. Using the same declarations given for Exercise 4, show the contents of the array sample after the following code segment is executed. for (i=0; i<8; i++) if (i<= 3) sample[i] = 1; else sample[i] = -1; 6. Using the same declarations given for Exercise 4, show the contents of the array sample after the following code segment is executed. for (k=0; k<8; k++) if (k % 2 == 0) sample[k] = k; else sample[k] = k + 100; 7. Given: int h = 6, p = 2, m = 3; int values = new int[7]; Suppose values contains: -4 0 2 6 -2 -1 14 Show the contents of the array values after: for (; m <=5; m++) values[m] = values[h] + values[p] * values[m]; 8. What is the error in the following program segment? int i; int count = new int[10]; System.out.println("please enter 10 numbers: "); for (i = 1; i <= 10; i++) count[i] = keybd.nextInt(); 9. Write the statements to multiply every element of an array of ints (of size 50) by 2. 10. Write the statements to add up those elements of an array of ints (of size 25) which have an even subscript. 11. Write the statements to add up those elements of an array of ints (of size 25) which have an even value. 12. What will the following program segment print? int nums = new int[10]; int i; for (i = 9 ; i >= 0 ; i--) { nums[i] = 5 * (i + 1); System.out.print(nums[i] + " "); } System.out.println(); for (i = 0 ; i < 10 ; i++) System.out.print(nums[i] + " "); System.out.println(); for (i = 0 ; i < 9 ; i++) nums[i+1] = nums[i]; for (i = 0 ; i < 10 ; i++) System.out.print(nums[i] + " "); System.out.println(); 13. What will the following program segment print? int nums = new int[10]; int i; for (i = 9 ; i >= 0 ; i--) { nums[i] = 5 * (i + 1); System.out.print(nums[i] + " "); } System.out.println(); for (i = 0 ; i < 10 ; i++) System.out.print(nums[i] + " "); System.out.println(); for (i = 8 ; i >= 0 ; i--) nums[i+1] = nums[i]; for (i = 0 ; i < 10 ; i++) System.out.print(nums[i] + " "); System.out.println(); } 14. Given: int temps = new int[50]; Write the statements to print "yes" if any element of the array temps contains the value 100. 15. Given: int temps = new int[50]; Write the statements to set the variable found to true if any element of the array temps contains the value 100. If not, the variable found should be false.
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2013