Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Consider the following method:
- public static void tryMe (int [ ] x, int size)
- {
- for (int i = 0; i < size; i++)
- System.out.println( “ [ “ + i + “ ] = “ + x[i]);
- }
- and the declarations:
- int [ ] list = new int [100];
- int [ ] score = new int[50];
- double [ ] gpa = new double [50];
- Which of the following method calls are valid?
- A. tryMe(list, 100);
- valid
- B. tryMe(list, 75);
- valid
- C. tryMe(score, 100);
- Not valid
- D. tryMe(score, 49);
- Valid
- E. tryMe(gpa, 50);
- Not Valid
- 2.
- A. What does it mean to say an array index is out of bounds?
- The index has no value in an array; the array is not large enough to have a value there.
- B. If an array index is outside the bounds, what happens when you run the program?
- It fails to run and you get a ArrayIndexOutOfBoundsException
- 3. Determine whether the following array declarations are valid. If a declaration is invalid, give a correct declaration.
- A. int [75] list;
- int [] list = new int[75];
- B. int size;
- double [] list = new double [size];
- int size = 87;
- double [] list = new double [size];
- C. int[ ] test = new int[-10];
- int[] test = new int[10];
- D. double[] sales = new double [40];
- valid
- 4. Suppose list is an array of five elements of type int. What is stored in list after the following java code executes?
- for (int i = 0; i < 5; i++)
- {
- list[ i ] = 2 * i + 5;
- if (i % 2 == 0)
- list[ i ] = list[ i ] - 3;
- }
- 2
- 7
- 6
- 11
- 10
- 5. Suppose we have the following two-dimensional array:
- int [ ] [ ] temp = { { 6, 8, 12, 9},
- {17, 5, 10, 6},
- {14, 13, 16, 20} };
- Write Java statements to accomplish the following. Note: For each statement, you are asked to output a single element.
- A. Output the contents of the first row and first column element of temp.
- System.out.println(temp[0][0]);
- B. Output the contents of the first row and last column element of temp.
- System.out.println(temp[0][3]);
- C. Output the contents of the last row and first column element of temp.
- System.out.println(temp[2][0]);
- D. Output the contents of the last row and last column element of temp.
- System.out.println(temp[2][3]);
- 6. The following program has five errors (Which could be compile or logic errors). Note that some errors involve multiple lines. For each statement that contains an error:
- A. Statement the line number containing the error and the reason for the error.
- B. Rewrite the statement so that the statement is correct.
- 1. public class Errors
- 2. {
- 3. static Scanner console = new Scanner(System.in);
- 4.
- 5. public static void main(String[ ] args)
- 6. {
- 7. int[ ] myList = new[10] ; int[ ] myList = new[10] ;
- 8.
- 9. for (int i = 0; i <= 10; i++) for (int i = 0; i < 10; i++)
- 10. myList = console.nextInt(); myList[i] = console.nextInt();
- 11.
- 12. for(int j = 0; j < myList.length(); j++) for(int j = 0; j < myList.length; j++)
- 13. System.out.print(myList[ i ] + “ “ ); System.out.print(myList[ j ] + “ “ );
- 14. System.out.println();
- 15. }
- 16. }
- 7. What is the exact output of the following program?
- public class List
- {
- public static void main(String[ ] args)
- {
- int[ ] alpha = new int[ 5 ];
- alpha[ 0 ] = 5;
- for (int i = 1; i < 5; i++)
- {
- alpha[ i ] = 5 * i + 10;
- alpha[ i -1 ] = alpha [ i ] - 4;
- }
- System.out.print( “List elements: “);
- for (int i = 0; i < 5; i++)
- System.out.print(alpha[ i ] + “ “);
- }
- }
- List elements: 11 16 21 26 30
- 8. Write Java statements that do the following:
- A. Declare an array alpha of 15 elements of type int.
- int[ ] alpha = new int[15];
- B. Output the value of the array alpha at index 9.
- System.out.println(alpha[9]);
- C. Set the value of the array alpha at index 8 to the sum of the elements at indices 5 and 12.
- alpha[8] = alpha[5] + alpha[12];
- D. Set the value of the array alpha at index 3 to three times the value at index 7 and then subtract by 57.
- alpha[3] = 3 * alpha[7] - 57;
- E. Output alpha so that five elements per line are printed.
- int i = 0;
- while (i < 15)
- {
- System.out.print(alpha[i]);
- i++;
- if (i % 5 == 0)
- System.out.println();
- }
- 9. Write Java statements that do the following:
- A. Declare an array alpha of 10 rows and 20 columns of type int.
- int[ ][ ] alpha = new int[10][20];
- B. Initialize each element of the array alpha to 5.
- int i = 0;
- int j = 0;
- while (j < 20)
- {
- while (i < 10)
- {
- alpha[i][j] = 5;
- i++;
- }
- i = 0;
- j++;
- }
- C. Store 1 in all elements in the first row, and store 2 in the remaining rows.
- int i = 0;
- while (i < 20)
- {
- alpha[0][i] = 1;
- i++;
- }
- i = 1;
- int j = 0;
- while (j < 20)
- {
- while (i < 10)
- {
- alpha[i][j] = 2;
- i++;
- }
- i = 1;
- j++;
- }
- D. Store 5 in all elements in the first column, and the value of each remaining column is twice the value of the previous column.
- int i = 0;
- while (i < 10)
- {
- alpha[i][0] = 5;
- i++;
- }
- i = 0;
- int j = 1;
- while (j < 20)
- {
- while (i < 10)
- {
- alpha[i][j] = 2 * alpha[i][j-1];
- i++;
- }
- i = 0;
- j++;
- }
- E. Print the array alpha one row per line with a space between each element.
- int i = 0;
- int j = 0;
- while (i < 10)
- {
- while (j < 20)
- {
- System.out.print(alpha[i][j]);
- System.out.print(" ");
- if (j == 19)
- System.out.println();
- j++;
- }
- j = 0;
- i++;
- }
- 10. Write Java statements to define and initialize the following arrays using a single line of code.
- A. Array of doubles called heights. Define and initialize the array of the following values: 5.2, 6.3, 5.8, 4.9, 5.2, 5.7, 6.7, 7.1, 5.10, 6.0
- double[ ] heights = {5.2, 6.3, 5.8, 4.9, 5.2, 5.7, 6.7, 7.1, 5.10, 6.0};
- B. Array of integers called weights. Define and initialize the array to the following values: 120, 125, 137, 140, 150, 180, 210
- int[ ] weights = {120, 125, 137, 140, 150, 180, 210};
- C. Array of characters called specialSymbols. Define and initialize the array to the following values: ‘%bodyamp;rsquo;, ‘#’, ‘%’, ‘&’, ‘!’, ‘^’
- char[ ] specialSymbols = {‘%bodyamp;rsquo;', ‘#’, ‘%’, ‘&’, ‘!’, ‘^’}
- char[ ] specialSymbols = {'#', '%', '&', '!', '^'};
- D. Array of strings called seasons. Define and initialize the array to the following values: “fall”, “winter”, “spring”, “summer”
- String[ ] seasons = {"fall", "winter", "spring", "summer"};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement