AP Comp Sci A Unit Review

¡Supera tus tareas y exámenes ahora con Quizwiz!

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;

I and II only

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?

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

Which of the following statements stores the value 3 in x ?

int x = 8 % 5;

1.2 Variables and Data Types

---

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) ?

48

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) ?

103

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?

16

System.out.println(404 / 10 * 10 + 1);

401

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?

Hello System.out.println!!!

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?

0.0

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?

8

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?

AP CS A

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;

I only

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?

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

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?

x = y; y = temp;

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?

*** *** ****

1.3 Expressions and Assignment Statements

---

1.4 Compound Assignment Operators

---

1.5 Casting and Ranges of Variables

---

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?

2.0 2

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?

9

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?

Changing print to println in line 2 only

Which of the following expressions evaluate to 7 ? 9 + 10 % 12 (9 + 10) % 12 9 - 2 % 12

II and III

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?

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.

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?

Line 1

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?

The code segment compiles without error.

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?

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.

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?

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

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?

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

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?

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

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?

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

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. public static int numDivisors (int inputVal) { int count = 0; for (int k = 1; k <= inputVal; k++) { if ( /* condition */ ) { count++; } } return count; } Which of the following can be used to replace / * condition * / so that numDivisors will work as intended?

inputVal % k == 0

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?

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

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?

9

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?

15

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?

double avgScore; int count;


Conjuntos de estudio relacionados

Ecological Succession, Cycles of Matter and Plants review

View Set

ACCT CH. 5 comprehension questions

View Set

World Geo Latin America Ch. 9 (mom)

View Set

Fundemntals of Nursing Chapter 21

View Set

Physical Education IS-Module 6 Exam A

View Set

Project Management Test 1 Chapter 1

View Set