computer science

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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? A- The two code segments print the same value only when grade is below 80. B- The two code segments print the same value only when grade is 90 or above or grade is below 80. C- The two code segments print the same value only when grade is 90 or above. D- Both code segments print the same value for all possible values of grade. E- The two code segments print different values for all possible values of grade.

Answer A Correct. Because code segment I uses else statements, points is only ever incremented once. Because code segment II does not use else statements, it is possible for multiple conditions to be true, causing points to be incremented multiple times. For grades lower than 70, both code segments print 0.0. For grades from 70 to 79, both code segments print 2.0. For grades from 80 to 89, code segment I prints 3.0 and code segment II prints 5.0. For grades 90 or above, code segment I prints 4.0 and code segment II prints 9.0.

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? A- isLeapYear(1900) B- isLeapYear(1984) C- isLeapYear(2000) D- isLeapYear(2001) E- isLeapYear(2010)

Answer A Correct. The value 1900 is a multiple of 4, so the expression (val % 4) == 0 evaluates to true and the statement return true; is executed, thereby exiting the method with a return value of true. This is an error because even though 1900 is a multiple of 100, it is not a multiple of 400, so the method should have returned false. Any value that is a multiple of 100 but not a multiple of 400 will pass the first test and return true even though the method was intended to return false.

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? A- randomNumber = 0.70; B- randomNumber = 0.80; C- randomNumber = 0.85; D- randomNumber = 0.90; E- randomNumber = 1.00;

Answer A Correct. When randomNumber is 0.70, the value of event should be set to 1, but the code segment does not work as intended. In the first if statement, the expression randomNumber <= 0.7 evaluates to true, and event is correctly set to 1. In the if-else statement, the expression randomNumber <= 0.8 evaluates to true, and event is incorrectly set to 2. Therefore, this shows that the code does not work as intended.

Consider the following code segment. int start = 4; int end = 5; boolean keepGoing = true; if (start < end && keepGoing) { if (end > 0) { start += 2; end++; } else { end += 3; } } if (start < end) { if (end == 0) { end += 2; start++; } else { end += 4; } } What is the value of end after the code segment is executed? A- 5 B- 6 C- 9 D- 10 E- 16

Answer B Correct. The condition in the first outer if statement evaluates to true because both start < end and keepGoing evaluate to true. In the first nested if statement, end > 0 evaluates to true, so start and end are both assigned the value 6. In the second outer if statement, start < end evaluates to false because the two variables now contain the same value; therefore, no further increments are done and the final value of end is 6.

Consider the following statement, which assigns a value to b1. boolean b1 = true && (17 % 3 == 1); Which of the following assigns the same value to b2 as the value stored in b1 ? A- boolean b2 = false || (17 % 3 == 2); B- boolean b2 = false && (17 % 3 == 2); C- boolean b2 = true || (17 % 3 == 1); D- boolean b2 = (true || false) && true; E- boolean b2 = (true && false) || true;

Answer B Correct. The expression 17 % 3 == 1 evaluates to false, and true && false evaluates to false, so b1 is assigned the value false. The expression on the right-hand side of the assignment statement for b2 evaluates to false by short circuit evaluation.

Consider the following code segment. if (false && true || false) { if (false || true && false) { System.out.print("First"); } else { System.out.print("Second"); } } if (true || true && false) { System.out.print("Third"); } What is printed as a result of executing the code segment? A- First B- Second C- Third D- FirstThird E- SecondThird

Answer C Correct. The condition in the first if statement evaluates to false, so the nested if statement is not executed. The condition in the final if statement evaluates to true , so the string "Third" is printed.

Consider the following code segment. int x = 7; int y = 4; boolean a = false; boolean b = false; if (x > y) { if (x % y >= 3) { a = true; x -= y; } else { x += y; } } if (x < y) { if (y % x >= 3) { b = true; x -= y; } else { x += y; } } What are the values of a, b, and x after the code segment has been executed? A- a = true, b = true, x = -1 B- a = true, b = false, x = 3 C- a = true, b = false, x = 7 D- a = false, b = true, x = 3 E- a = false, b = false, x = 11

Answer C Correct. The first three conditions encountered in the code segment evaluate to true. In the first if statement, since x > y and x % y >= 3, the body of the nested if clause is executed; a is assigned the value true and x is decremented by y, resulting in 3. In the third if statement, since x < y but y % x is not >= 3, the body of the nested else clause is executed; b is unchanged and x is incremented by y, resulting in 7. The variables a, b, and x contain the values true, false, and 7, respectively.

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? A- choice < 5 B- choice >= 5 and choice <= 10 C- choice > 10 D- choice == 5 or choice == 10 E- There is no value for choice that will cause the two code segments to produce different output.

Answer C Correct. When choice is greater than 10, code segment A will print "blue" and the else statements are not executed. Code segment B will print "blue" but will then execute the next if statement and print "yellow", thereby giving different output for initial values that are greater than 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? A- degrees = 90; B- degrees = 94; C- degrees = 95; D- degrees = 96; E- The code will work as intended for all values of degrees.

Answer C Correct. When degrees is 95, the expression degrees > 95 evaluates to false so that "School is open" is printed. This shows that the code does not work as intended, because it was supposed to show that school was closed when the temperature reached or exceeded 95°F.

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 ? A- x = 8 and y = 25 B- x = 10 and y = 10 C- x = 10 and y = 30 D- x = 15 and y = 30 E- x = 25 and y = 30

Answer C Correct. When x is 10, the expression x <= 10 evaluates to true. When y is 30, the expression y > 25 evaluates to true. Therefore, the expression (x <= 10) == (y > 25) compares true == true, resulting in true. The expression will evaluate to true only when the results of the two subexpressions are the same. Either both of the subexpressions should be true or both of the subexpressions should be false.

Consider the following Boolean expressions. I. A && B II. !A && !B Which of the following best describes the relationship between values produced by expression I and expression II? A- Expression I and expression II evaluate to different values for all values of A and B. B- Expression I and expression II evaluate to the same value for all values of A and B. C- Expression I and expression II evaluate to the same value only when A and B are the same. D- Expression I and expression II evaluate to the same value only when A and B differ. E- Expression I and expression II evaluate to the same value whenever A is true.

Answer D Correct. A truth table can be used to show that expressions I and II evaluate to the same value (false) when A and B have different truth values (true, false or false, true) and that they evaluate to different truth values when A and B have the same truth value (both true or both false).

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? A- 5 B -10 C - 10 == 10 D- true E- false

Answer D Correct. After the second line of the code segment executes, a has the value 10 and b has the value 10 as well, so a == b evaluates to true.

Consider the following code segment. int x = 3; int y = -1; if (x - 2 > y) { x -= y; } if (y + 3 >= x) { y += x; } System.out.print("x = " + x + " y = " + y); What is printed as a result of the execution of the code segment? A- x = -1 y = -1 B- x = 2 y = 1 C- x = 3 y = 2 D- x = 4 y = -1 E- x = 4 y = 3

Answer D Correct. The Boolean expression in the first if statement evaluates to true, so the statement x -= y is executed setting x equal to x - y, or 4. The Boolean expression in the second if statement evaluates to false. Therefore, the statement y += x is not executed. The values of x and y are 4 and -1, respectively, when the print statement is executed.

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.println("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"); } A- I only B- II only C- III only D- I and II only E- I, II, and III

Answer D Correct. The original code segment prints "YES" for odd integers and "NO" for even integers. Code segment I produces the same output as the original code segment because the result of evaluating the expression x % 2 has only two possible values: 0 if x is even and 1 if x is odd. Code segment II produces the same output as the original code segment because the second else clause that prints "NONE" is never reached. Code segment III does not produce the same output as the original code segment. It creates a local boolean variable and assigns it the result of x % 2 == 0, but when using it in the if statement, the wrong branch is taken so that "YES" is printed for even integers and "NO" is printed for odd integers.

Consider the following code segment. double regularPrice = 100; boolean onClearance = true; boolean hasCoupon = false; double finalPrice = regularPrice; if(onClearance) { finalPrice -= finalPrice * 0.25; } if(hasCoupon) { finalPrice -= 5.0; } System.out.println(finalPrice); What is printed as a result of executing the code segment? A- 20.0 B- 25.0 C- 70.0 D- 75.0 E- 95.0

Answer D Correct. The variable finalPrice is initialized to regularPrice. Since the boolean variable onClearance is true, the code in the body of the first if statement is executed, setting finalPrice to 75.0. Since the boolean variable hasCoupon is false, the code in the body of the second if statement is not executed. The value 75.0 is printed.

Consider the following code segment. if (a < b || c != d) { System.out.println("dog"); } else { System.out.println("cat"); } Assume that the int variables a, b, c, and d have been properly declared and initialized. Which of the following code segments produces the same output as the given code segment for all values of a, b, c, and d ? A- if (a < b && c != d) { System.out.println("dog"); } else { System.out.println("cat"); } B- if (a < b && c != d) { System.out.println("cat"); } else { System.out.println("dog"); } C- if (a > b && c == d) { System.out.println("cat"); } else { System.out.println("dog"); } D- if (a >= b || c == d) { System.out.println("cat"); } else { System.out.println("dog"); } E- if (a >= b && c == d) { System.out.println("cat"); } else { System.out.println("dog"); }

Answer E Correct. Since the original expression prints "dog" when a < b || c != d evaluates to true, an equivalent code segment would print "cat" when the negative of the expression, or !(a < b || c != d), evaluates to true. Applying De Morgan's laws produces the equivalent Boolean expression a >= b && c == d for the case when "cat" should be printed.

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

Answer E Correct. Since the value of the Boolean expression (5 < 8) is true and the value of the Boolean expression (5 == 8) is false, the value of the expression (5 < 8) == (5 == 8) is equivalent to the value of the expression true == false, which is false.

Consider the following code segment. int x = 7; if (x < 7) { x = 2 * x; } if (x % 3 == 1) { x = x + 2; } System.out.print(3 * x); What is printed as a result of executing the code segment? A- 7 B- 9 C- 14 D- 21 E- 27

Answer E Correct. The initial value of x is 7. The condition (x < 7) is false, so the body of the first if statement is not executed. The condition x % 3 == 1 is true, so the body of the second if statement is executed. This increases the value of x from 7 to 9. Lastly, the value of 3 * x, which is 27, is printed.


Ensembles d'études connexes

Anatomy of Plants - Assessment I-III

View Set

CH 6: The Life Insurance Underwriting and Policy Issue

View Set

Series 6: Regulations (FINRA Rules)

View Set

HW 2 - 13.4 Le Chatelier's Principle Concentration and Pressure Changes

View Set

Unit 1-1 Spanish Subject Pronoun Replacement

View Set

Fundamental of nursing Chapter 24

View Set

Exam 5 (FINAL EXAM) Microeconomics

View Set

Chapter 28 - Infection Prevention & Control

View Set