AP CS- Unit 3

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

In the code segment below, the int variable temp represents a temperature in degrees Fahrenheit. The code segment is intended to print a string based on the value of temp. The following table shows the string that should be printed for different temperature ranges. Temperature RangeString to Print31 and below"cold"32-50"cool"51-70"moderate"​71 and above"warm" String weather; if (temp <= 31) { weather = "cold"; } else { weather = "cool"; } if (temp >= 51) { weather = "moderate"; } else { weather = "warm"; } System.out.print(weather); Which of the following test cases can be used to show that the code does NOT work as intended? temp = 30 temp = 51 temp = 60 A. 1 only B. II only C. I and II only D. II and III only E. I, II, and III

A. 1 only

Consider the following expression. (3 + 4 == 5) != (3 + 4 >= 5) What value, if any, does the expression evaluate to? A. true B. false C. 5 D. 7 E. No value; relational operators cannot be used on arithmetic expressions.

A. true

The following method is intended to return true if and only if the parameter val is a multiple of 4 but is not a multiple of 100 unless it is also a multiple of 400. The method does not always work correctly. public boolean isLeapYear(int val) { if ((val % 4) == 0) { return true; } else { return (val % 400) == 0; } } Which of the following method calls will return an incorrect response? Answer A: isLeapYear(1900) Answer B: isLeapYear(1984) Answer C: isLeapYear(2000) Answer D: isLeapYear(2001) Answer E: isLeapYear(2010)

Answer A: isLeapYear(1900)

Consider the following Boolean expression in which the int variables x and y have been properly declared and initialized. (x <= 10) == (y > 25) Which of the following values for x and y will result in the expression evaluating to true ? x = 8 and y = 25 Answer A: x = 8 and y = 25 Answer B: x = 10 and y = 10 Answer C: x = 10 and y = 30 Answer D: x = 15 and y = 30 Answer E: x = 25 and y = 30

Answer C: x = 10 and y = 30

Consider the following code segment in which the int variable x has been properly declared and initialized. if (x % 2 == 1) { System.out.println("YES"); } else { System.out.println("NO"); } Assuming that x is initialized to the same positive integer value as the original, which of the following code segments will produce the same output as the original code segment? I. if (x % 2 == 1) { System.out.print("YES"); } if (x % 2 == 0) { System.out.println("NO"); } II. if (x % 2 == 1) { System.out.println("YES"); } else if (x % 2 == 0) { System.out.println("NO"); } else { System.out.println("NONE"); } III. boolean test = x % 2 == 0; if (test) { System.out.println("YES"); } else { System.out.println("NO"); } Answer A: I only Answer B: II only Answer C: III only Answer D: I and II only Answer E: I, II, and III

Answer D: I and II only

In the following expression, j, k, and m are properly declared and initialized int variables. !((j == k) && (k > m)) Which of the following is equivalent to the expression above? A. (j != k) || (k < m) B. (j != k) || (k <= m) C. (j == k) || (k < m) D. (j != k) && (k <= m) E. (j == k) && (k < m)

B. (j != k) || (k <= m)

In the following expression, sweet, salty, and sour are properly declared and initialized boolean variables. sweet && (salty || sour) Which of the following expressions is equivalent to the expression above? A. (sweet && salty) || sour B. (sweet && salty) || (sweet && sour) C. (sweet && salty) && (sweet && sour) D. (sweet || salty) && sour E. (sweet || salty) && (sweet || sour)

B. (sweet && salty) || (sweet && sour)

Consider the following code segment. boolean a = true; boolean b = false; System.out.print((a == !b) != false); What is printed as a result of executing this code segment? A. false B. true C. 0 D. 1 E. Nothing is printed because the expression (a == !b) != false is an invalid parameter to the System.out.print method.

B. true

Consider the following code segment. int m = 8; int n = 3; if (m + n > 10) { System.out.print(m + n); } if (m - n > 0) { System.out.print(m - n); } What, if anything, is printed as a result of executing the code segment? A. Nothing is printed. B. 5 C. 11 D. 115 E. 511

D. 115

Consider the following variable declarations and initializations. int a = 2; int b = 6; int c = 3; Which of the following expressions evaluates to false ? A. a < b == c < b B. a > b == b < c C. a < b != b < c D. a < b != c < b E. a > b != b > c

D. a < b != c < b

Consider the following code segment. int a = 1; int b = 0; int c = -1; if ((b + 1) == a) { b++; c += b; } if (c == a) { a--; b = 4; } What are the values of a, b, and c after this code segment has been executed? A. a = 0, b = 4, and c = 0 B. a = 0, b = 4, and c = 1 C. a = 1, b = 0, and c = -1 D. a = 1, b = 1, and c = 0 E. a = 1, b = 1, and c = 1

D. a = 1, b = 1, and c = 0

Assume that the int variables a, b, c, and low have been properly declared and initialized. The code segment below is intended to print the sum of the greatest two of the three values but does not work in some cases. if (a > b && b > c) { low = c; } if (a > b && c > b) { low = b; } else { low = a; } System.out.println(a + b + c - low); For which of the following values of a, b, and c does the code segment NOT print the correct value? A. a = 1, b = 1, c = 2 B. a = 1, b = 2, c = 1 C. a = 1, b = 2, c = 3 D. a = 2, b = 2, c = 2 E. a = 3, b = 2, c = 1

E. a = 3, b = 2, c = 1

Consider the following two code segments. Assume that variables x and y have been declared as int variables and have been assigned integer values. I. int result = 0; if (x > y) { result = x - y; System.out.print(result); } else if (x < y) { result = y - x; System.out.print(result); } else { System.out.print(result); } II. if (x < y) { System.out.print(y - x); } else { System.out.print(x - y); } Which of the following correctly compares the outputs of the two code segments? A. Code segment I and code segment II produce the same output for all values of x and y. B. Code segment I and code segment II produce the same output only when x is equal to y. C. Code segment I and code segment II produce the same output only when x is not equal to y. D. Code segment I and code segment II produce the same output only when x is less than y. E. Code segment I and code segment II do not produce the same output for any values of x and y.

A. Code segment I and code segment II produce the same output for all values of x and y.

Consider the following code segments, which are each intended to convert grades from a 100-point scale to a 4.0-point scale and print the result. A grade of 90 or above should yield a 4.0, a grade of 80 to 89 should yield a 3.0, a grade of 70 to 79 should yield a 2.0, and any grade lower than 70 should yield a 0.0. Assume that grade is an int variable that has been properly declared and initialized. Code Segment I double points = 0.0; if (grade > 89) { points += 4.0; } else if (grade > 79) { points += 3.0; } else if (grade > 69) { points += 2.0; } else { points += 0.0; } System.out.println(points); Code Segment II double points = 0.0; if (grade > 89) { points += 4.0; } if (grade > 79) { grade += 3.0; } if (grade > 69) { points += 2.0; } if (grade < 70) { points += 0.0; } System.out.println(points); Which of the following statements correctly compares the values printed by the two methods? The two code segments print the same value only when grade is below 80. Answer A: The two code segments print the same value only when grade is below 80 . Answer B: The two code segments print the same value only when grade is 90 or above or grade is below 80 . Answer C: The two code segments print the same value only when grade is 90 or above. Answer D: Both code segments print the same value for all possible values of grade . Answer E: The two code segments print different values for all possible values of grade.

Answer A: The two code segments print the same value only when grade is below 80 .

Consider the following code segment, which is intended to simulate a random process. The code is intended to set the value of the variable event to exactly one of the values 1, 2, or 3, depending on the probability of an event occurring. The value of event should be set to 1 if the probability is 70 percent or less. The value of event should be set to 2 if the probability is greater than 70 percent but no more than 80 percent. The value of event should be set to 3 if the probability is greater than 80 percent. The variable randomNumber is used to simulate the probability of the event occurring. int event = 0; if (randomNumber <= 0.70) { event = 1; } if (randomNumber <= 0.80) { event = 2; } else { event = 3; } The code does not work as intended. Assume that the variable randomNumber has been properly declared and initialized. Which of the following initializations for randomNumber will demonstrate that the code segment will not work as intended? randomNumber = 0.70; Answer A: randomNumber = 0.70; Answer B: randomNumber = 0.80; Answer C: randomNumber = 0.85; Answer D: randomNumber = 0.90; Answer E: randomNumber = 1.00;

Answer A: randomNumber = 0.70;

Consider the following two code segments where the int variable choice has been properly declared and initialized. Code Segment A if (choice > 10) { System.out.println("blue"); } else if (choice < 5) { System.out.println("red"); } else { System.out.println("yellow"); } Code Segment B if (choice > 10) { System.out.println("blue"); } if (choice < 5) { System.out.println("red"); } else { System.out.println("yellow"); } Assume that both code segments initialize choice to the same integer value. Which of the following best describes the conditions on the initial value of the variable choice that will cause the two code segments to produce different output? choice < 5 Answer A: choice < 5 Answer B: choice >= 5 and choice <= 10 Answer C: choice > 10 Answer D: choice == 5 or choice == 10 Answer E: There is no value for choice that will cause the two code segments to produce different output.

Answer C: choice > 10

A school that does not have air conditioning has published a policy to close school when the outside temperature reaches or exceeds 95°F. The following code segment is intended to print a message indicating whether or not the school is open, based on the temperature. Assume that the variable degrees has been properly declared and initialized with the outside temperature. if (degrees > 95) { System.out.println("School will be closed due to extreme heat"); } else { System.out.println("School is open"); } Which of the following initializations for degrees, if any, will demonstrate that the code segment may not work as intended? degrees = 90; Answer A: degrees = 90; Answer B: degrees = 94; Answer C: degrees = 95; Answer D: degrees = 96; Answer E: The code will work as intended for all values of degrees.

Answer C: degrees = 95;

Consider the following code segment. int a = 10; int b = 5 * 2; System.out.print(a == b); What is printed as a result of executing the code segment? Answer A: 5 Answer B: 10 Answer C: 10 == 10 Answer D: true Answer E: false

Answer D: true

Consider the following statement. boolean x = (5 < 8) == (5 == 8); What is the value of x after the statement has been executed? Answer A: 3 Answer B: 5 Answer C: 8 Answer D: true Answer E: false

Answer E: false

Consider the following code segment. String myString = new String("my string"); String yourString = new String(); yourString = "my string"; boolean dotEquals = myString.equals(yourString); boolean equalsEquals = (myString == yourString); System.out.print(dotEquals + " " + equalsEquals); What is printed as a result of executing the code segment? A. true true B. true false C. false true D. false false E. my string my string

B. true false

Consider the following code segment. String str1 = new String("Happy"); String str2 = new String("Happy"); System.out.print(str1.equals(str2) + " "); System.out.print(str2.equals(str1) + " "); System.out.print(str1 == str2); What is printed as a result of executing the code segment? A. true true true B. true true false C. false true false D. false false true E. false false false

B. true true false

In the following expression, sunny and windy are properly declared and initialized boolean variables. !sunny && !windy Which of the following is equivalent to the expression above? A. sunny || windy B. !sunny || !windy C. !(sunny || windy) D. !(sunny && windy) E. !(sunny && !windy)

C. !(sunny || windy)

Consider the following code segment. int quant = 20; int unitPrice = 4; int ship = 8; int total; if (quant > 10) { unitPrice = 3; } if (quant > 20) { ship = 0; } total = quant * unitPrice + ship; What is the value of total after this code segment has been executed? A. 20 B. 60 C. 68 D. 80 E. 88

C. 68

The following code segment prints one or more characters based on the values of boolean variables b1 and b2. Assume that b1 and b2 have been properly declared and initialized. if (!b1 || b2) { System.out.print("A"); } else { System.out.print("B"); } if (!(b1 || b2)) { System.out.print("C"); } else { System.out.print("D"); } if (b1 && !b1) { System.out.print("E"); } If b1 and b2 are both initialized to true, what is printed when the code segment has finished executing? A. ABCD B. ABD C. AD D. BD E. BDE

C. AD

Consider the following code segment. String first = new String("duck"); String second = new String("duck"); String third = new String("goose"); if (first == second) { System.out.print("A"); } else if (second == third) { System.out.print("B"); } else if (first.equals(second)) { System.out.print("C"); } else if (second.equals(third)) { System.out.print("D"); } else { System.out.print("E"); } What is printed as a result of executing the code segment? A. A B. B C. C D. D E. E

C. C

Consider the following code segment. boolean a = true; boolean b = true; System.out.print((b || (!a || b)) + " "); System.out.print(((!b || !a) && a) + " "); System.out.println(!(a && b) && b); What output is produced when this code segment is executed? A. true true true B. true false true C. true false false D. false true false E. false false false

C. true false false

Consider the following code segment, which uses properly declared and initialized int variables x and y and the String variable result. String result = ""; if (x < 5) { if (y > 0) { result += "a"; } else { result += "b"; } } else if (x > 10) { if (y < 0) { result += "c"; } else if (y < 10) { result += "d"; } result += "e"; } result += "f"; What is the value of result after the code segment is executed if x has the value 15 and y has the value 5 ? A. ad B. adf C. d D. def E. ef

D. def

Consider the following code segment, which is intended to set the Boolean variable inRange to true if the integer value num is greater than min value and less than max value. Otherwise inRange is set to false. Assume that inRange, num, min, and max have been properly declared and initialized. boolean isBigger; boolean isSmaller; boolean inRange; if (num < max) { isSmaller = true; } else { isSmaller = false; } if (num > min) { isBigger = true; } else { isBigger = false; } if (isBigger == isSmaller) { inRange = true; } else { inRange = false; } Which of the following values of num, min, and max can be used to show that the code does NOT work as intended? A. num = 20, min = 30, max = 50 B. num = 30, min = 20, max = 40 C. num = 40, min = 10, max = 40 D. num = 50, min = 50, max = 50 E. num = 60, min = 40, max = 50

D. num = 50, min = 50, max = 50

Consider the following two code segments, which are both intended to determine the longest of the three strings "pea", "pear", and "pearl" that occur in String str. For example, if str has the value "the pear in the bowl", the code segments should both print "pear" and if str has the value "the pea and the pearl", the code segments should both print "pearl". Assume that str contains at least one instance of "pea". I. if (str.indexOf("pea") >= 0) { System.out.println("pea"); } else if (str.indexOf("pear") >= 0) { System.out.println("pear"); } else if (str.indexOf("pearl") >= 0) { System.out.println("pearl"); } II. if (str.indexOf("pearl") >= 0) { System.out.println("pearl"); } else if (str.indexOf("pear") >= 0) { System.out.println("pear"); } else if (str.indexOf("pea") >= 0) { System.out.println("pea"); } Which of the following best describes the output produced by code segment I and code segment II? A. Both code segment I and code segment II produce correct output for all values of str. B. Neither code segment I nor code segment II produce correct output for all values of str. C. Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pear" but not "pearl". D. Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pearl". E. Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pea" but not "pear".

E. Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pea" but not "pear".

Consider the following code segment in which the int variables a and b have been properly declared and initialized. if (a < b) { a++; } else if (b < a) { b++; } else { a++; b++; } Which of the following code segments is equivalent to the code segment above? A. if (a <= b + 1) { a++; } else if (b <= a - 1) { b++; } else { a++; b++; } B. if (a + 1 <= b) { a++; } else if (b - 1 <= a) { b++; } else { a++; b++; } C. if (a - b < 0) { a++; } else if (b - a > 0) { b++; } else { a++; b++; } D. if (a != b) { a++; } else if (b != a) { b++; } else { a++; b++; } E. if (a == b) { a++; b++; } else if (a < b) { a++; } else { b++; }

E. if (a == b) { a++; b++; } else if (a < b) { a++; } else { b++; }


Set pelajaran terkait

FD- Culturally Competent Nursing Care + Culture

View Set

Acute and Chronic Wound Management

View Set

International Business Chapter 5

View Set

Chapter 7: Mental Images and Propositions

View Set