Question: 1.Consider the following two code segments. In both, assume that n is an integer variable that has been declared and initialized. Segment 1 int sum
1.Consider the following two code segments. In both, assume that n is an integer variable that has been declared and initialized. Segment 1 int sum = 1; int i; for (i = 2; i < n; i++) { sum += i; } System.out.println(sum); Segment 2 int sum = 1; int i = 2; while (i <= n) { sum += i; i++; } System.out.println(sum); For which integer values of n do these code segments print the same result?
| A.Only n>=1 |
| B. Only n<=1 |
| C. Only n==1 |
| D. These segments never print the same result. |
| E. Any integer n produces the same result. |
2. Given the following code: int i = 100; int j = 10; while (i > 0) { i = i / j; j = 1 + j % 5; } What is the value of i after this code executes?
| A. 0 |
| B. 1 |
| C. 2 |
| D. 5 |
| E. 10 |
3.The following code is intended to calculate the sum of the first five positive odd integers starting at 1. int sum = 0; int k; for (k = 1; k <= 10; k += 2) { sum += k; } What is wrong with this code segment?
| A.The segment calculates the sum of the first four positive odd integers. |
| B.The segment calculates the sum of the first six positive odd integers. |
| C. The segment calculates the sum of the first seven positive odd integers. |
| D. The variable sum is incorrectly initialized. The segment would work correctly if sum was initialized to 1. |
| E . The segment works as intended. |
4. The following code is designed to set index to the location of the first occurrence of target in the array a, and to set index to -1 if target is not found in a. index = 0; while (a[index] != target) { index++; } if (a[index] != target) { index = -1; } Which of the following describes the condition under which this program segment will fail to perform the task described?
| A.Whenever target is the first element of the array |
| B.Whenever target is the last element of the array |
| C.Whenever target is not present in the array |
| D.Whenever target is -1 |
| E. Whenever target = a[target] |
5. Suppose the following array is declared: int[ ] grades = {88, 92, 95, 83}; What is the value of grades[grades[0] / 22]?
| A. An ArrayIndexOutOfBoundsException occurs |
| B. 4 |
| C. 88 |
| D. 92 |
| E. 95 |
6. !((x > y) || (y <= 0)) is equivalent to which of the following expressions?
- (x > y) || !(y <= 0)
- !(x > y) && !(y <= 0)
- (x <= y) && (y > 0)
| A. I only |
| B. II only |
| C. III only |
| D. I and III only |
| E. II and III only |
7. Given the following code: if (n == 2) { k -= 2; } else if (n == 3) { k -= 3; } can be rewritten as if (< condition >) { < assignment statement >; } where < condition > and < assignment statement > are chosen so that the rewritten code performs the same task as the original code. Assume that both n and k are integer variables. Which of the following could be used as < condition >?
- (n == 2) || (n == 3)
- (n == 2) && (n == 3)
- (n >= 2) && (n <= 3)
| A. I only |
| B. II only |
| C. III only |
| D. I and III only |
| E. II and III only |
8. Which of the following statements is false?
| A. For-each loops (or enhanced for loops) can be used to iterate over arrays. |
| B. For-each loops can only be used to iterate over all array elements. |
| C.Traditional for loops can be used to iterate over all or some array elements. |
| D. For-each loops can be used to iterate over all array elements in reverse order. |
| E.For-each loops can be replaced with either for loops or while loops |
9. When is the following expression true? !(!a || b) || (!a && b)
| A. If and only if a and b have different values |
| B. If and only if a and b have the same value |
| C.If and only if both a and b are true |
| D.If and only if both a and b are false |
| E.The expression is never true |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
