Quiz One

Ace your homework & exams now with Quizwiz!

Consider the following code segment. int w = 1; int x = w / 2; double y = 3; int z = (int) (x + y); Which of the following best describes the results of compiling the code segment? A The code segment compiles without error. B The code segment does not compile, because the int variable x cannot be assigned the result of the operation w / 2. C The code segment does not compile, because the integer value 3 cannot be assigned to the double variable y. D The code segment does not compile, because the operands of the addition operator cannot be of different types int and double. E The code segment does not compile because the result of the addition operation is of type double and cannot be cast to an int.

A The code segment compiles without error.

Consider the following code segment. System.out.print("Hello System.out.println"); System.out.print("!!!"); What is printed as a result of executing the code segment? A Hello!!! B Hello System.out.println!!! C Hello !!! D Hello System.out.println !!! E Nothing is printed because the text "System.out.println" cannot appear inside a print statement.

B Hello System.out.println!!!

In the code segment below, assume that the int variables a and b have been properly declared and initialized. int c = a; int d = b; c += 3; d--; double num = c; num /= d; Which of the following best describes the behavior of the code segment? A The code segment stores the value of (a + 3) / b in the variable num. B The code segment stores the value of (a + 3) / (b - 1) in the variable num. C The code segment stores the value of (a + 3) / (b - 2) in the variable num. D The code segment stores the value of (a + 3) / (1 - b) in the variable num. E The code segment causes a runtime error in the last line of code because num is type double and d is type int.

B The code segment stores the value of (a + 3) / (b - 1) in the variable num.

A code segment (not shown) is intended to determine the number of players whose average score in a game exceeds 0.5. A player's average score is stored in avgScore, and the number of players who meet the criterion is stored in the variable count. Which of the following pairs of declarations is most appropriate for the code segment described? A double avgScore; boolean count; B double avgScore; double count; C double avgScore; int count; D int avgScore; boolean count; E int avgScore; int count;

C double avgScore; int count;

The code segment below is intended to calculate the circumference c of a circle with the diameter d of 1.5. The circumference of a circle is equal to its diameter times pi. /* missing declarations */ c = pi * d; Which of the following variable declarations are most appropriate to replace /* missing declarations */ in this code segment? A int pi = 3.14159; int d = 1.5; final int c; B final int pi = 3.14159; int d = 1.5; int c; C final double pi = 3.14159; double d = 1.5; double c; D double pi = 3.14159; double d = 1.5; final double c = 0.0; E final double pi = 3.14159; final double d = 1.5; final double c = 0.0;

C final double pi = 3.14159; double d = 1.5; double c;

Consider the following code segment. int a = 5; int b = 4; int c = 2; a *= 3; b += a; b /= c; System.out.print(b); What is printed when the code segment is executed? A 2 B 4 C 9 D 9.5 E 19

C 9

Consider the following code segment. System.out.print("AP"); System.out.println(); System.out.println("CS"); System.out.print("A"); What is printed as a result of executing the code segment? A APCSA B APCS A C AP CSA D AP CS A E AP CS A

D AP CS A

Which of the following expressions evaluate to 7 ? 9 + 10 % 12 (9 + 10) % 12 9 - 2 % 12 A I only B II only C I and III D II and III E I, II, and III

D II and III

Consider the following code segment. System.out.print(I do not fear computers. ); // Line 1 System.out.println(I fear the lack of them.); // Line 2 System.out.println(--Isaac Asimov); // Line 3 The code segment is intended to produce the following output but may not work as intended. I do not fear computers. I fear the lack of them. --Isaac Asimov Which change, if any, can be made so that the code segment produces the intended output? A In line 1, print should be changed to println. B In lines 2 and 3, println should be changed to print. C The statement System.out.println() should be inserted between lines 2 and 3. D In lines 1, 2, and 3, the text that appears in parentheses should be enclosed in quotation marks. E No change is needed; the code segment works correctly as is.

D In lines 1, 2, and 3, the text that appears in parentheses should be enclosed in quotation marks.

Consider the following code segment. int a = 5; int b = 8; int c = 3; System.out.println(a + b / c * 2); What is printed as a result of executing this code? A 2 B 6 C 8 D 9 E 14

D 9

Consider the following code segment. int x = 5; x += 6 * 2; x -= 3 / 2; What value is stored in x after the code segment executes? A -1.5 B 1 C 9 D 15.5 E 16

E 16

Consider the following code segment. double x = 4.5; int y = (int) x * 2; System.out.print(y); What is printed as a result of executing the code segment? A 8 B 8.0 C 9 D 9.0 E 10

A 8

Consider the following code segment. System.out.print("One"); // Line 1 System.out.print("Two"); // Line 2 System.out.print("Three"); // Line 3 System.out.print("Four"); // Line 4 The code segment is intended to produce the following output, but does not work as intended. OneTwo ThreeFour Which of the following changes can be made so that the code segment produces the intended output? A Changing print to println in line 1 only B Changing print to println in line 2 only C Changing print to println in line 3 only D Changing print to println in lines 2 and 3 only E Changing print to println in lines 1, 2, 3, and 4

B Changing print to println in line 2 only

The following code segment is intended to interchange the values of the int variables x and y. Assume that x and y have been properly declared and initialized. int temp = x; /* missing code */ Which of the following can be used to replace /* missing code */ so that the code segment works as intended? A x = y; x = temp; B x = y; y = temp; C y = x; x = temp; D y = x; temp = y; E y = x; temp = x;

B x = y; y = temp;

Consider the following code segment. double x = (int) (5.5 - 2.5); double y = (int) 5.5 - 2.5; System.out.println(x - y); What is printed as a result of executing the code segment? A -1.0 B -0.5 C 0.0 D 0.5 E 1.0

D 0.5

Consider the following code segment, which is intended to print the digits of the two-digit int number num in reverse order. For example, if num has the value 75, the code segment should print 57. Assume that num has been properly declared and initialized. /* missing code */ System.out.print(onesDigit); System.out.print(tensDigit); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? A int onesDigit = num % 10; int tensDigit = num / 10; B int onesDigit = num / 10; int tensDigit = num % 10; C int onesDigit = 10 / num; int tensDigit = 10 % num; D int onesDigit = num % 100; int tensDigit = num / 100; E int onesDigit = num / 100; int tensDigit = num % 100;

A int onesDigit = num % 10; int tensDigit = num / 10;

Consider the following method. public int getTheResult(int n) { int product = 1; for (int number = 1; number < n; number++) { if (number % 2 == 0) product *= number; } return product; } What value is returned as a result of the call getTheResult(8) ? A 48 B 105 C 384 D 5040 E 40320

A 48

In the code segment below, assume that the int variable n has been properly declared and initialized. The code segment is intended to print a value that is 1 more than twice the value of n. /* missing code */ System.out.print(result); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? int result = 2 * n;result = result + 1; int result = n + 1;result = result * 2; int result = (n + 1) * 2; A I only B II only C III only D I and III E II and III

A I only

Consider the following code segment. System.out.print(*); // Line 1 System.out.print("*"); // Line 2 System.out.println(); // Line 3 System.out.println("*"); // Line 4 The code segment is intended to produce the following output, but may not work as intended. ** * Which line of code, if any, causes an error? A Line 1 B Line 2 C Line 3 D Line 4 E The code segment works as intended.

A Line 1

Consider the following incomplete method, which is intended to return the number of integers that evenly divide the integer inputVal. Assume that inputVal is greater than 0. Which of the following can be used to replace / * condition * / so that numDivisors will work as intended? A inputVal % k == 0 B k % inputVal == 0 C inputVal % k != 0 D inputVal / k == 0 E k / inputVal > 0

A inputVal % k == 0

Consider the following code segment. int x = 5; int y = 6; /* missing code */ z = (x + y) / 2; Which of the following can be used to replace /* missing code */ so that the code segment will compile? int z = 0; int z; boolean z = false; A I only B II only C I and II only D II and III only E I, II, and III

C I and II only

Consider the following code segment. System.out.print("*"); System.out.println("**"); System.out.println("***"); System.out.print("****"); What is printed as a result of executing the code segment? A * ** *** **** B * ** ******* C * ***** **** D *** *** **** E *** *******

D *** *** ****

Consider the following method. public int mystery(int num) { int x = num; while (x > 0) { if (x / 10 % 2 == 0) return x; x = x / 10; } return x; } What value is returned as a result of the call mystery(1034) ? A 4 B 10 C 34 D 103 E 1034

D 103

Which of the following statements stores the value 3 in x ? A int x = 4 / 7; B int x = 7 / 3; C int x = 7 / 4; D int x = 5 % 8; E int x = 8 % 5;

E int x = 8 % 5;

Consider the following code segment. int a = 4; int b = 5; a++; b++; int c = a + b; a -= 1; System.out.println(a + c); What is printed when the code segment is executed? A 9 B 10 C 14 D 15 E 25

D 15

Consider the following code segment, where k and count are properly declared and initialized int variables. k++; k++; count++; k--; count++; k--; Which of the following best describes the behavior of the code segment? A Te code segment leaves both k and count unchanged. B The code segment increases both k and count by 2. C The code segment increases k by 4 and count by 2. D The code segment leaves k unchanged and increases count by 2. E The code segment increases k by 2 and leaves count unchanged.

Dthe code segment leaves k unchanged and increases count by 2.

Consider the following code segment. double num = 9 / 4; System.out.print(num); System.out.print(" "); System.out.print((int) num); What is printed as a result of executing the code segment? A 2 2 B 2.0 2 C 2.0 2.0 D 2.25 2 E 2.25 2.0

B 2.0 2

Directions: Select the choice that best fits each statement. The following question(s) refer to the following incomplete class declaration. Which of the following can be used to replace / * missing code * / so that advance will correctly update the time? A minutes = minutes % 60; B minutes = minutes + hours % 60; C hours = hours + minutes / 60;minutes = minutes % 60; D hours = hours + minutes % 60; minutes = minutes / 60; E hours = hours + minutes / 60; Related Content & Skills

C hours = hours + minutes / 60; minutes = minutes % 60;

The following code segment is intended to round val to the nearest integer and print the result. double val = -0.7; int roundedVal = (int) (val + 0.5); System.out.println(roundedVal); Which of the following best describes the behavior of the code segment? A The code segment works as intended. B The code segment does not work as intended because val and roundedVal should be declared as the same data type. C The code segment does not work as intended because the expression (val + 0.5) should be cast to a double instead of an int. D The code segment does not work as intended because val should be cast to an int before 0.5 is added to it. E The code segment does not work as intended because the expression (int) (val + 0.5) rounds to the nearest integer only when val is positive.

E The code segment does not work as intended because the expression (int) (val + 0.5) rounds to the nearest integer only when val is positive.

What is printed as a result of executing the following statement? S.o.P (404/10*10+1) A 4 B 5 C 41 D 401 E 405

401

Consider the following code segment. num += num; num *= num; Assume that num has been previously declared and initialized to contain an integer value. Which of the following best describes the behavior of the code segment? A The value of num is two times its original value. B The value of num is the square its original value. C The value of num is two times the square of its original value. D The value of num is the square of twice its original value. E It cannot be determined without knowing the initial value of num.

D The value of num is the square of twice its original value.

Consider the following code segment, which is intended to find the average of two positive integers, x and y. int x; int y; int sum = x + y; double average = (double) (sum / 2); Which of the following best describes the error, if any, in the code segment? A There is no error, and the code works as intended. B In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be less than the expected result for even values of sum. C In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be greater than the expected result for even values of sum. D In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be less than the expected result for odd values of sum. E In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be greater than the expected result for odd values of sum.

D In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be less than the expected result for odd values of sum.


Related study sets

Pathophysiology - Genetics and Disease

View Set

Chapter 16: Control Systems and Quality Management

View Set

বাংলাদেশের অবস্থান ও সীমানা

View Set

Legal/Illegal Interview Questions

View Set

NR 275 ATI In-Class Practice Questions

View Set

"Retort" and from The People, Yes

View Set

Chapter 11 - Section 1 The Byzantine Empire Midterm Review

View Set

PreAP English 10 A study set without OMAM content

View Set