Unit 3 Test

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

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

D true

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

E false

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

C x = 10 and y = 30

At a certain high school students receive letter grades based on the following scale. Which of the following code segments will assign the correct string to gradefor a given integer score ? A II only B III only C I and II only D I and III only E I, II, and III

D I and III only

Assume that the boolean variables a, b, c, and d have been declared and initialized. Consider the following expression. !( !( a && b ) || ( c || !d )) Which of the following is equivalent to the expression? A ( a && b ) && ( !c && d ) B ( a || b ) && ( !c && d ) C ( a && b ) || ( c || !d ) D ( !a || !b ) && ( !c && d ) E !( a && b ) && ( c || !d )

A ( a && b ) && ( !c && d )

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)

A isLeapYear(1900)

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

C Third

Consider the following method. public int pick(boolean test, int x, int y) { if (test) return x; else return y; } What value is returned by the following method call? pick(false, pick(true, 0, 1), pick(true, 6, 7)) A 0 B 1 C 3 D 6 E 7

D 6

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

D 75.0

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

E 27

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

C a = true, b = false, x = 7

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.

C choice > 10

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.

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

Assume that object references one, two, and three have been declared and instantiated to be of the same type. Assume also that one == two evaluates to true and that two.equals(three) evaluates to false. Consider the following code segment. if (one.equals(two)) { System.out.println("one dot equals two"); } if (one.equals(three)) { System.out.println("one dot equals three"); } if (two == three) { System.out.println("two equals equals three"); } What, if anything, is printed as a result of executing the code segment? A one dot equals two B one dot equals two one dot equals three C one dot equals three two equals equals three D one dot equals two one dot equals three two equals equals three E Nothing is printed.

A one dot equals two

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;

A randomNumber = 0.70;

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

B 6

Consider the following code segment. String str1 = new String("Advanced Placement"); String str2 = new String("Advanced Placement"); if (str1.equals(str2) && str1 == str2) { System.out.println("A"); } else if (str1.equals(str2) && str1 != str2) { System.out.println("B"); } else if (!str1.equals(str2) && str1 == str2) { System.out.println("C"); } else if (!str1.equals(str2) && str1 != str2) { System.out.println("D"); } What, if anything, is printed when the code segment is executed? A A B B C C D D E Nothing is printed.

B B

Assume that the boolean variables a and b have been declared and initialized. Consider the following expression. (a && (b || !a)) == a && b Which of the following best describes the conditions under which the expression will evaluate to true? A Only when a is true B Only when b is true C Only when both a and b are true D The expression will never evaluate to true. E The expression will always evaluate to true.

B Only when b is true

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;

B boolean b2 = false && (17 % 3 == 2);

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.

C degrees = 95;

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.

D Expression I and expression II evaluate to the same value only when A and B differ.

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

D I and II only

Consider the following code segment. String alpha = new String("APCS"); String beta = new String("APCS"); String delta = alpha; System.out.println(alpha.equals(beta)); System.out.println(alpha == beta); System.out.println(alpha == delta); What is printed as a result of executing the code segment? A false false false B false false true C true false false D true false true E true true true

D true false 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

D x = 4 y = -1

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"); }

E if (a >= b && c == d) { System.out.println("cat"); } else { S


Conjuntos de estudio relacionados

Unit 1 - The Real Estate Business

View Set

Ch. 4 - Mosby's 7th - Head and Neck Anatomy and Physiology

View Set

Lecture Chapter 9 Select the Correct Answer Exercise 9.04

View Set

Cybersecurity - Week 4 - Certificates

View Set

Chapter 14: Depressive Disorders

View Set

Forensics and Security Ch. 11,14,15,16

View Set