Question: 21. How many dimensions are in the array? (1 point) int[][] array1 = new int[15][10]; 0 1 2 3 22. Suppose you write a subclass
21.
How many dimensions are in the array? (1 point) int[][] array1 = new int[15][10];
| 0 | |
| 1 | |
| 2 | |
| 3 |
22.
Suppose you write a subclass of Insect named Ant. You add a new method named doSomething to the Ant class. You write a client class that instantiates an Ant object and invokes the doSomething method. Which of the following Ant declarations will permit this? (2 points)
- Insect a = new Ant();
- Ant a = new Ant();
- Ant a = new Insect();
| I only | |
| II only | |
| III only | |
| I and II only | |
| I, II, and III only |
23.
Consider the following code: String[] fruits = {"apple", "pear", "mango", "peach"}; int i = 3; String str = "p"; for(String item : fruits) { i = item.indexOf("p") + 2; str += item.substring(i); } System.out.println(str); Assume that the loop accesses array elements in order. What is output by this code? (2 points)
| learangoach | |
| peearmangoeach | |
| plearangoach | |
| ppplepearpeach | |
| A StringIndexOutOfBoundsException occurs. |
24.
Consider the following code: int[] arr = new int[ < some positive integer > ]; int index; // assume arr is filled with values here // assume the user enters a value for index here if((index >= 0) && (index <= arr.length)) { System.out.println(arr[index]); } When could this code throw an exception? (2 points)
| When index is less than 0. | |
| When index is equal to 0. | |
| When index is greater than the arr.length. | |
| When index is equal to the arr.length. | |
| This code cannot throw an exception because the if statement prevents it. |
25.
Assume that you have an array of integers named arr. Which of these code segments print the same results? (2 points)
- int i = 0; while(i < arr.length) { System.out.println(arr[i]); i++; }
- int i; for(i = 0; i < arr.length; i++) { System.out.println(arr[i]); }
- for(int i : arr) { System.out.println(i); }
| II only | |
| I and II only | |
| II and III only | |
| I and III only | |
| I, II, and III |
26.
Assume that table has been declared and initialized as a two-dimensional integer array with 9 rows and 5 columns. Which segment of code will correctly find the sum of the elements in the third column? (2 points)
| int sum = 0; for(int i = 0; i < table.length; i++) sum += table[2][i]; | |
| int sum = 0; for(int i = 0; i < table[0].length; i++) sum += table[i][2]; | |
| int sum = 0; for(int i = 0; i < table.length; i++) sum += table[i][2]; | |
| int sum = 0; for(int outer = 0; outer < table.length; outer++) for(int inner = 0; inner < table[0].length; inner++) sum += table[outer][2]; | |
| int sum = 0; for(int outer = 0; outer < table[0].length; outer++) for(int inner = 0; inner < table.length; inner++) sum += table[outer][2]; |
27.
Suppose a class named SportsCar is a subclass of a class called Automobile. Automobile has only two methods: accelerate and addGas. Which statement is true? (2 points)
| A SportsCar object has the methods accelerate and addGas only. No other methods are allowed, since no other methods are inherited. | |
| A SportsCar object has at least the methods accelerate and addGas. It might add its own methods. | |
| A SportsCar object has the methods accelerate and addGas only if it is cast to Automobile. | |
| A SportsCar object has the methods accelerate and addGas only if they are static. | |
| A SportsCar object has the methods accelerate and addGas only if they are abstract. |
28.
Consider an integer array, vals, which has been declared with one or more integer values. Which of the code segments updates vals so that each element contains the current value less 2? (2 points)
- for (int v: vals) { v = v 2; }
- for (int m = 0; m < vals.length; m++ ) { vals[m] = vals[m] 2; }
- int m = 0; while (m < vals.length) { vals[m] = vals[m] 2; }
| II only | |
| III only | |
| I and III | |
| II and III | |
| I, II, and III |
29.
Consider the following code block. String str = "Good Evening. How are you?"; String newString = str.substring(0, str.indexOf(".")); Which of the following BEST describes the result of the code above? (2 points)
| "How are you?" is assigned to newString | |
| "Good Evening" is assigned to newString | |
| "Good Evening." is assigned to newString | |
| "12" is assigned to newString | |
| There is a syntax error in the code |
30.
Consider the following code segment. What would print when the code executes? (2 points) int num1 = 12; int num2 = 2; while (num1 < num2) { System.out.print(num1 % num2 + " "); num2++; }
| 0 0 0 2 0 5 0 3 2 1 | |
| 0 0 0 2 0 5 4 3 2 | |
| 0 0 2 0 5 4 3 2 1 | |
| 0 0 0 2 0 5 4 3 2 1 | |
| The loop never executes |
31.
int x = __; int y = __; if (y / x > 0) System.out.print("first "); System.out.print("second"); For which values of x and y below will this program execute "first second"? (2 points)
- x = 5; y = 10;
- x = 6; y = 7;
- x = 1; y = 3;
| I only | |
| II only | |
| I and II only | |
| II and III only | |
| I, II and III |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
