Question: 1. Suppose that String str1 = apple, String str2 = banana, and String str3 = cherry. Which of the following evaluates to false? (2 points)

1. Suppose that String str1 = "apple", String str2 = "banana", and String str3 = "cherry." Which of the following evaluates to false? (2 points)

str1.compareTo(str2) < 0 str2.compareTo(str3) > 0 str3.compareTo(str1) < 0

I only II only III only I and II only II and III only

2. Assume that age has been declared as an int variable. Which expression is true whenever age indicates that the person is a teenager? (2 points)

((age <= 20) && (age >= 13)) ((age < 20) || (age >= 13)) ((age <= 19) && (age < 13)) ((age <= 19) || (age >= 13)) ((age <= 19) && (age > 12))

3. !((x > y) && (y <= 0)) is equivalent to which of the following expressions? (2 points)

!(x > y) && !(y <= 0) (x <= y) || (y > 0) (x < y) || (y > 0)

I only II only III only I and III only II and III only

4. Assume that these String variables have been declared:

String str1 = "hello"; String str2 = "hello";

What is the value of the following expression? (2 points)

str1.equals(str2)

true false hello hellohello equal

5. When is the following expression true? (2 points)

!(!a || b) || (!a && b)

If and only if a and b have different values If and only if a and b have the same value If and only if both a and b are true If and only if both a and b are false The expression is never true

6. What is output by the following segment of code? (2 points)

int i = 6; while (i >= 2) { System.out.print (i + " "); if ((i % 2) == 0) { i = i / 2; } else { i++; } }

6 7 4 2 6 3 4 2 6 2 4 1 6 5 4 3 2 6 3 5 2

7. Given the following code:

if (n == 2) { k -= 2; } else if (n == 3) { k -= 3; }

can be rewritten as:

if (< condition >) { < assignment statement >; }

Assume that evaluating < condition > does not change the values stored in n and k. Which of the following could be used as < assignment statement >? (2 points)

k -= n; k -= 1; k -= 2; k += n; k = n - k;

8. What is stored in str after the following code executes? (2 points)

String str = "Computer Science"; int i = 0; while (i < 8) { if (str.indexOf("m") < i) { str = str.substring(0, 2) + str; } i += 2; }

Computer Science CoComputer Science CoCoComputer Science CoCoCoComputer Science Computer ScienceCo

9. 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 j after this code executes? (2 points)

0 2 3 4 5

10. What is the value of b after the following code executes? (2 points)

int a = 2; int b = 0; while (a < 10) { b = 6; while (b > a) { b--; } a += b; }

2 3 4 5 6

11. 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? (2 points)

Whenever target is the first element of the array Whenever target is the last element of the array Whenever target is not present in the array Whenever target is -1 Whenever target = a[target]

12. Suppose the following array is declared:

int[ ] grades = {88, 92, 95, 83};

What is the value of grades[2]? (2 points)

An ArrayIndexOutOfBoundsException occurs

88 92 95 83

13. Suppose the following array is declared:

int[ ] grades = {88, 92, 95, 83};

and the following integer is declared:

int index = 6 % 3;

What is the value of grades[index]? (2 points)

An ArrayIndexOutOfBoundsException occurs

88 92 95 83

14. Suppose the following array is declared:

int[ ] grades = {88, 92, 95, 83};

What are the values in grades after the following code executes? (2 points)

grades[0] = grades[1]; grades[1] = grades[0]; An ArrayIndexOutOfBoundsException occurs

{92, 88, 95, 83} {88, 88, 95, 83} {92, 92, 95, 83} {88, 92, 95, 83}

15. Suppose the following array is declared:

int[ ] grades = {88, 92, 95, 83};

What is the value of grades[grades.length-1]? (2 points)

An ArrayIndexOutOfBoundsException occurs

88 92 82 83

16. Which of these code segments print the same results? (2 points)

int i = 1; while (i <= 10){ System.out.println(i + " "); i++;}

int i; for (i = 0; i <= 10; i++) { System.out.println(i + " ");}

int i; for (i = 1; i <= 10; i++) { System.out.println(i + " ");}

I and II only II and III only I and III only All three print the same results. All three print different results.

17. What is stored in the array a after the following code executes? (2 points)

int[ ] a = {11, 33, 55, 77}; int i = 0; while (i < a.length) { if (a[i] < 50) { a[i] = a[i] % 3; } else { a[i] = a[i - 1] + a[i]; } i++; }

{2, 0, 88, 165} {2, 3, 88, 165} {2, 0, 55, 165} {2, 0, 55, 132} An endless loop occurs

18. 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? (2 points)

Only n >= 1 Only n <= 1 Only n == 1 These segments never print the same result. Any integer n produces the same result.

19. The following code is intended to calculate the sum of the first five positive even integers starting at 2.

int sum = 2; int k; for (k = 4; k <= 12; k += 2) { sum += k; }

What is wrong with this code segment? (2 points)

The segment calculates the sum of the first four positive even integers. The segment calculates the sum of the first six positive even integers. The segment calculates the sum of the first seven positive even integers. The variable sum is incorrectly initialized. The segment would work correctly if sum was initialized to 0. The segment works as intended.

20. Consider the following code:

String[ ] words = {"cat", "dog", "lamb", "giraffe", "elephant"}; int i = 0; int index = -1; int temp; while (i < words.length) { temp = words[i].indexOf("a"); if (temp > index) { index = temp; } i++; }

What is the value of index after this code executes? (2 points)

-1 0 1 5 An ArrayIndexOutOfBoundsException occurs.

21. Assume that you have two arrays of integers that have been declared and initialized. One is named a, and the other is named b. Both arrays have the same number of elements. Consider code that compares the elements of two arrays and prints the value true if the elements in corresponding positions are equal, and prints false otherwise. Which of the following code will accomplish this? (2 points)

boolean temp = true; int i; for (i = 0; i < a.length; i++){ temp = (a[i] == b[i]); } System.out.println(temp);

boolean temp = false; int i; for (i = 0; i < a.length; i++) { if (a[i]==b[i]) { temp = true; } }

System.out.println(temp);

boolean temp; int i; for (i = 0; i < a.length; i++) { temp = (a[i] == b[i]); System.out.println(temp); }

int i = 0; while (i < a.length && a[i] == b[i]) { i++;} System.out.println(i > a.length);

boolean temp = true; int i; for (i = 0; i < a.length; i++) { temp = (a(i) == b(i)); }

22. Which of the following statements is false? (2 points)

For-each loops (or enhanced for loops) can be used to iterate over arrays. For-each loops can only be used to iterate over all array elements. Traditional for loops can be used to iterate over all or some array elements. For-each loops can be used to iterate over all array elements in reverse order. For-each loops can be replaced with either for loops or while loops.

23. What does the decimal number 107 equal in the hexadecimal system? (2 points)

121 5D 3D A9 6B

24. What does the binary number 1001 represent in the decimal system? (2 points)

11 9 17 15 23

25. What does the octal number 140 represent in the decimal system? (2 points)

96 29 57 190 132

26. In the following algorithm, which constant should be assigned to maximum to ensure that the algorithm finds the largest integer? (2 points)

int maximum = << constant >>; for(int current = 0; current < numbers.length; current++) { if(maximum < numbers[current]) maximum = numbers[current]; }

Int.MIN_VALUE Int.MAX_VALUE Integer.MAX_VALUE Integer.MIN_VALUE

27. What does the hexadecimal number B9 represent in the decimal system? (2 points)

111 241 66 185 123

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!