Question: 32. The code segment below should print 10 8 6 4 2 0. 1 int x = 10; 2 while (x >= 0) { System.out.println(x
32.
The code segment below should print "10 8 6 4 2 0". 1 int x = 10; 2 while (x >= 0) { System.out.println(x + " "); 3 x-=2; } Does it work as intended? If NOT, what code segment below would correct it? (2 points)
| Line 3: x += 2; | |
| Line 1: int x = 12; | |
| Line 2: while (x > 1) | |
| Line 2: while (x < 1) | |
| The code runs as expected |
33.
Consider the code segment below. int x = 3; int y = 5; int z = 12; if (x > 0 && y <= 10) { System.out.println(y x * z % x + y); } What would print as a result? (2 points)
| 0 | |
| 1 | |
| 5 | |
| 10 | |
| 11 |
34.
Which of the following expressions is equivalent to !(a && b)? (2 points)
| (a || b) | |
| (!a) && (!b) | |
| (a != b) | |
| (!a) II (!b) | |
| (a && b) |
35.
Consider the method below. What would the result of test(3) be? (2 points) public static int test(int n) { if (n == 0) return 3; else return 2 * test(n - 1); }
| 1 | |
| 3 | |
| 6 | |
| 12 | |
| 24 |
36.
Consider the code block below. for (int x = 0; x < 3; x++ ) { for (int y = 1; y < x; y++ ) { System.out.print(x + " "); } } What is the result when the code executes? (2 points)
| 2 2 | |
| 2 3 | |
| 3 | |
| 2 | |
| Nothing is printed |
37.
Consider the code block below. String str = " hi "; for (int x = 0; x < 3; x++ ) { for (int y = 1; y < 1; y++ ) { System.out.print(x + str); } } What is the result when the code executes? (2 points)
| 2hi 2hi | |
| 2hi 3hi | |
| 2 hi | |
| 2 hi 3 hi | |
| Nothing is printed |
38.
Consider the Animal, Bird, and Cardinal classes shown below. Which of the following object declarations will compile without error? (2 points) public class Animal { /* constructors and methods not shown */ } public class Bird extends Animal { /* constructors and methods not shown */ } public class Cardinal extends Fish { /* constructors and methods not shown */ }
- Cardinal red = new Bird();
- Animal tweety = new Bird();
- Bird chirpy = new Cardinal();
| I only | |
| II only | |
| III only | |
| I, II and III | |
| II and III only |
39.
Consider the code segment below. String[][] letters = {{"A", "B", "C", "D"}, {"E", "F", "G", "H"}, {"I", "J", "K", "L"}}; for(int x = 1; xletters[0].length; x++){ for(int y = 2; yletters.length; y++){ System.out.print(letters[y][x] + " "); } } What is the result when the code executes? (2 points)
| B F J C G K D H L | |
| E I F J G K H L | |
| F J G K H L | |
| J G K H L | |
| J K L |
40.
Assume arr2 is declared as a two-dimensional array of integers. Which of the following segments of code successfully calculates the sum of all elements arr2? (1 point)
- int sum = 0; for(int j = 0; j < arr2.length; j++) { for(int k = 0; k < arr2[j].length; k++) { sum += arr2[j][k]; } }
- int sum = 0; for(int j = 0; j < arr2.length; j++) { for(int k = 0; k < arr2[j].length; k++) { sum += arr2[k][j]; } }
- int sum = 0; for(int[] m : arr2) { for(int n : m) { sum += n; } }
| I only | |
| II only | |
| I and II only | |
| II and III only | |
| I and III only |
41.
The code below is intended to replace all of the items in the array by half of their value. int [ ] arr = {10, 20, 30, 40}; for(int value: arr) { value/=2; } for(int value: arr) { System.out.print(value + " "); } Which BEST explains why the code does NOT work as intended? (2 points)
| The syntax of the for:each loop is incorrect | |
| The array is declared incorrectly | |
| The value of the temporary variable cannot be changed in a for:each loop | |
| The value/=2 equation is not formatted correctly | |
| The print statement has a syntax error in it |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
