APCSA midterm review

Ace your homework & exams now with Quizwiz!

Each of the following code segments is intended to print the word Hello. Which of the following code segments works as intended? System.out.print("Hello"); System.out.print(Hello); System.out.print(He);System.out.print(llo); A. I only B. II only C. III only D. I and II E. II and III

A. I only

Consider the following code segment. int a = 1; String result = ""; while (a < 20) { result += a; a += 5; } System.out.println(result); What, if anything, is printed as a result of executing the code segment? A. 21 B. 161116 C. 161161 D. 16111621 E. Nothing is printing because of an infinite loop.

B. 161116

Consider the following code segment. int count = 0; int number = 20; while (number > 0) { number = number / 2; count++; } What will be the value of count after executing the code segment? A. 6 B. 5 C. 4 D. 1 E. 0

B. 5

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.

B. false

Consider the following code segment. String str = "AP-CSA"; for (int i = 0; i < str.length(); i++) { if (str.substring(i, i + 1).equals("A")) { System.out.print(i + " "); } } What is printed as a result of executing the code segment? A. 0 B. 5 C. 0 5 D. 0 6 E. 1 6

C. 0 5

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

Consider the following code segment. double p = 10.6; double n = -0.2; System.out.println((int) (p + 0.5)); System.out.print((int) (n - 0.5)); What is printed as a result of executing the code segment? A. 10 -1 B. 10 0 C. 11 -1 D. 11 0 E. 11 1

D. 11 0

Consider the following code segment, which is intended to store the sum of all multiples of 10 between 10 and 100, inclusive (10 + 20 + ... + 100), in the variable total. int x = 100; int total = 0; while( /* missing code */ ) { total = total + x; x = x - 10; } Which of the following can be used as a replacement for /* missing code */ so that the code segment works as intended? A. x < 100 B. x <= 100 C. x > 10 D. x >= 10 E. x != 10

D. x >= 10

Consider the following code segment. int x = 0; x++; x += 1; x = x + 1; x -= -1; System.out.println(x); What is printed when the code segment has been executed? A. 0 B. 1 C. 2 D. 3 E. 4

E. 4

Consider the following code segment. for (int i = 0; i < 5; i++) // Line 1 { for (int j = 0; j < 5; j++) { int k = i + j; System.out.print(k + " "); } } Which of the following best describes the result of changing i < 5 to i > 5 in line 1? A. The numbers will be printed in the reverse order as they were in the original code segment because the outer loop will occur in reverse order. B. Five additional values will be printed because the outer for loop will iterate one additional time. C. An infinite loop will occur because the termination condition of the loop will never be reached. D. There will be no change to the program output. E. Nothing will be printed because the body of the outer for loop will not execute at all.

E. Nothing will be printed because the body of the outer for loop will not execute at all.

Consider the following code segment, which is intended to display 6.0. double fact1 = 1 / 2; double fact2 = 3 * 4; double product = fact1 * fact2; System.out.println(product); Which of the following best describes the error, if any, in the code segment? A. There are no errors and the code works as intended. B. Either the numerator or the denominator of the fraction 1 / 2 should be cast as double. C. The expression fact1 * fact 2 should be cast as double. D. The expressions 1 / 2 and 3 * 4 should both be cast as double. E. The variables fact1 and fact2 should both be declared as int.

B. Either the numerator or the denominator of the fraction 1 / 2 should be cast as double.

Consider the following method. public String mystery(String word, int num) { String result = ""; for (int k = num; k >= 0; k--) { result += word.substring(0, k); } return result; } What is returned as a result of the call mystery("computer", 3) ? A. ccocom B. comcoc C. ccocomcomp D. compcomcoc E. comcomcomcom

B. comcoc

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. 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 code segment below. int x = 10; int y = 20; System.out.print(y + x / y); What is printed as a result of executing the code segment? A. 1 B. 1.5 C. 3 D. 20 E. 20.5

D. 20

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

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

Consider the following code segment. int j = 1; while (j <= 5) { for (int k = 4; k > 1; k--) { System.out.println("ha"); // line 6 } j++; } How many times will the print statement on line 6 execute? A. 15 B. 16 C. 20 D. 24 E. 25

A. 15

Consider the following code segment. int a = 1; int b = 2; int c = 3; int d = 4; double x = a + b * c % d; What is the value of x when the code segment has been executed? A. 1.0 B. 2.5 C. 3.0 D. 7.0 E. 9.0

C. 3.0

Consider the following code segment. int a = 100; while (a > 1) { System.out.println("$"); a /= 2; } How many $'s are printed as a result of executing the code segment? A. 0 B. 5 C. 6 D. 7 E. 50

C. 6

Consider the following incomplete code segment, which is intended to print the sum of the digits in num. For example, when num is 12345, the code segment should print 15, which represents the sum 1 + 2 + 3 + 4 + 5. int num = 12345; int sum = 0; /* missing loop header */ { sum += num % 10; num /= 10; } System.out.println(sum); Which of the following should replace /* missing loop header */ so that the code segment will work as intended? A. while (num > 0) B. while (num >= 0) C. while (num > 1) D. while (num > 2) E. while (num > sum)

A. while (num > 0)

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

B. cat dog horse cow

Consider the following code segment. double a = 7; int b = (int) (a / 2); double c = (double) b / 2; System.out.print(b); System.out.print(" "); System.out.print(c); What is printed as a result of executing the code segment? A. 3 1 B. 3 1.0 C. 3 1.5 D. 3.5 1.5 E. 3.5 1.75

C. 3 1.5

Consider the following methods, which appear in the same class. public void methodA(int arg) { int num = arg * 10; methodB(num); } public void methodB(int arg) { System.out.print(arg + 10); } Consider the call methodA(4), which appears in a method in the same class. What, if anything, is printed as a result of the call methodA(4) ? A. 14 B. 40 C. 50 D. 140 E. Nothing is printed.

C. 50

Consider the following code segment. int a = 1; while (a <= 2) { int c = 1; while (/* missing condition */) { System.out.print("*"); c++; } a++; } The code segment is intended to print "******". Which of the following can be used to replace /* missing condition */ so that the code segment works as intended? A. c <= 2 B. c < 3 C. c <= 3 D. c > 2 E. c >= 3

C. c <= 3

Consider the following code segment. int j = 10; int k = 8; j += 2; k += j; System.out.print(j); System.out.print(" "); System.out.println(k); What is printed when the code segment is executed? A. 2 2 B. 2 10 C. 10 8 D. 12 12 E. 12 20

E. 12 20

Consider the following code segment. int total = 0; for (int k = 0; k <= 100; k += 2) { total += k; } Which of the following for loops could be used to replace the for loop in the original code segment so that the original and the revised code segments store the same value in total? A. for (int k = 0; k < 100; k += 2) { total += k + 1; } B. for (int k = 1; k < 101; k += 2) { total += k - 1; } C. for (int k = 0; k <= 101; k += 2) { total += k + 1; } D. for (int k = 1; k <= 101; k += 2) { total += k + 1; } E. for (int k = 1; k <= 101; k += 2) { total += k - 1; }

E. for (int k = 1; k <= 101; k += 2) { total += k - 1; }

Consider the following code segment. System.out.print("Ready"); // Line 1 System.out.print("Set"); // Line 2 System.out.print("Go!"); // Line 3 The code segment is intended to produce the following output but may not work as intended. Ready Set Go! Which change, if any, can be made so that the code segment produces the intended output? A. Changing print to println in lines 1 and 2 B. Changing print to println in line 3 C. Interchanging lines 1 and 3 D. Replacing all three lines with the single statement System.out.println("Ready Set Go!"); E. No change is needed; the code segment works correctly as is.

A. Changing print to println in lines 1 and 2

A teacher has created a Student class. The class contains the following. An int variable called grade to represent the student's grade level A String variable called name to represent the student's name A double variable called average to represent the student's grade point average A method called updateAverage that updates the student's average. The object greg will be declared as type Student. Which of the following descriptions is accurate? A. greg is an instance of the Student class. B. greg is an instance of the updateAverage method. C. greg is an instance of three attributes. D. Student is an instance of the greg object. E. updateAverage is an instance of the Student class.

A. greg is an instance of the Student class.

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.

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 Range String to Print 31 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. I only B. II only C. I and II only D. II and III only E. I, II, and III

A. I only

Consider the following code segment. double d = 0.25; int i = 3; double diff = d - i; System.out.print((int)diff - 0.5); What is printed as a result of executing the code segment? A. -2 B. -2.5 C. -3 D. -3.25 E. Nothing is printed because an int cannot be subtracted from a double.

B. -2.5

Consider the following code segment. int x = 4; int y = 6; x -= y; y += x; Which of the following best describes the behavior of the code segment? A. The value of x and the value of y have not been changed. B. Both the value of x and the value of y have been decreased. C. Both the value of x and the value of y have been increased. D. The value of x has been decreased and the value of y has been increased. E. The value of x has been increased and the value of y has been decreased.

B. Both the value of x and the value of y have been decreased.

Consider the following code segment. int x = 10; int y = 20; /* missing code */ System.out.print(top / bottom); Which of the following replacements for /* missing code */ will cause an ArithmeticException to occur? I. int top = x - y;int bottom = y - x; II. int top = 2 * x;int bottom = y - top; III. int top = x + y;int bottom = 2 * top; A. I only B. II only C. III only D. I and II E. II and III

B. II only

A store sells rope only in whole-foot increments. Given three lengths of rope, in feet, the following code segment is intended to display the minimum length of rope, in feet, that must be purchased so that the rope is long enough to be cut into the three lengths. For example, for lengths of 2.8 feet, 3 feet, and 5 feet, the minimum length of rope that must be purchased is 11 feet. For these inputs, the code segment should display 11. As another example, for lengths of 1.1 feet, 3.2 feet, and 2 feet, the minimum length of rope that must be purchased is 7 feet. For these inputs, the code segment should display 7. double len1; double len2; double len3; double total = len1 + len2 + len3; int minLength = (int) (total + 0.5); System.out.print(minLength); Which of the following best describes the behavior of the code segment? A. The code segment works as intended for all nonnegative values of len1, len2, and len3. B. The code segment works as intended but only when the sum of the three lengths is an integer or the decimal part of the sum of the three lengths is greater than or equal to 0.5. C. The code segment does not work as intended for some values of len1, len2, and len3. It could be corrected by casting len1, len2, and len3 to int before adding them together and assigning the sum to minLength. D. The code segment does not work as intended for some values of len1, len2, and len3. It could be corrected by declaring minLength as a double and casting it to an int in the System.out.print statement. E. The code segment does not work as intended for any values of len1, len2, and len3. It returns the sum of 0.5 and the three lengths for all values of len1, len2, and len3.

B. The code segment works as intended but only when the sum of the three lengths is an integer or the decimal part of the sum of the three lengths is greater than or equal to 0.5.

Which statement correctly declares a variable that can store a temperature rounded to the nearest tenth of a degree? A. boolean patientTemp; B. double patientTemp; C. int patientTemp; D. patientTemp = 0; E. patientTemp = 0.0;

B. double patientTemp;

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 method. public void adjust(double max, double min, double total, double n) { total = total - max - min; n = n - 2.0; System.out.println(total / n); } Consider the call adjust(25.0, 5.0, 60.0, 5.0), which appears in a method in the same class. What is printed as a result of the method call? A. 6.0 B. 10.0 C. 12.0 D. 15.0 E. 20.0

B. 10.0

Consider the following class. public class MagicNumber { private int num; public MagicNumber() { num = 10; } public void displayNumber() { System.out.println(num); } public void add_2() { num = num + 2; } } When located in a method in a class other than MagicNumber, which of the following code segments will compile without error? I. MagicNumber.add_2(); MagicNumber.displayNumber(); II. MagicNumber n1 = new MagicNumber(); n1.add_2(); n1.displayNumber(); III. n2.add_2();n2.displayNumber(); A. I only B. II only C. III only D. II and III only E. None of the code segments will compile.

B. II only

Consider the following code segments, which differ only in their loop header. Code Segment I for (int i = 0; i < 10; i++) { System.out.print( "*" ); } Code Segment II for (int i = 1; i <= 10; i++) { System.out.print( "*" ); } Which of the following best explains how the difference in the two loop headers affects the output? A. The output of the code segments is the same because the loops in each code segment terminate when i is 10. B. The output of the code segments is the same because the loops in both code segments iterate 10 times. C. The output of the code segments is different because code segment I iterates from i = 0 to i = 9 and code segment II iterates from i = 1 to i = 10. D. The output of the code segments is different because code segment I iterates from i = 0 to i = 10 and code segment II iterates from i = 1 to i = 11. E. Neither code segment produces output because both loop conditions are initially false.

B. The output of the code segments is the same because the loops in both code segments iterate 10 times.

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. 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

A student has created a Book class. The class contains variables to represent the following. An int variable called pages to represent the number of pages A boolean variable called isHardcover to indicate whether or not the book is hardcover The object story will be declared as type Book. Which of the following descriptions is accurate? A. An instance of the story class is Book. B. An instance of the Book object is story. C. An attribute of the story object is isHardcover. D. An attribute of the pages object is Book. E. An attribute of the Book instance is story.

C. An attribute of the story object is isHardcover.

Consider the following code segment, which is intended to calculate the average of two quiz scores. double avg = 15 + 20; avg /= 2; Which of the following best describes the behavior of the code segment? A. The code segment stores 17 in avg because 17 is the result of the integer division of 35 by 2. B. The code segment stores 18 in avg because 18 is the result of the integer division of 35 by 2. C. The code segment stores 17.5 in avg because 17.5 is the result of the floating point division of 35.0 by 2. D. The code segment does not compile because int values cannot be added to double values. E. The code segment does not compile because a double value cannot be divided by an int value.

C. The code segment stores 17.5 in avg because 17.5 is the result of the floating point division of 35.0 by 2.

Consider the following code segment, which is intended to display 0.5. int num1 = 5; int num2 = 10; double ans = num1 / num2; System.out.print(ans); 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. The code should have cast the expression num1 / num2 to double. C. The code should have cast either num1 or num2 in the expression num1 / num2 to double. D. The code should have declared ans as an int. E. The code should have initialized num1 to 5.0 and num2 to 10.0.

C. The code should have cast either num1 or num2 in the expression num1 / num2 to double.

Consider the following method. public double secret(int x, double y) { return x / 2.0; } Which of the following lines of code, if located in a method in the same class as secret, will compile without error? A. int result = secret(4, 4); B. int result = secret(4, 4.0); C. double result = secret(4, 4.0); D. double result = secret(4.0, 4); E. double result = secret(4.0, 4.0);

C. double result = secret(4, 4.0);

A teacher determines student percentages in a course as the points a student earns divided by the total points available in the grading period. Points are awarded only in whole number increments, but student percentages are to be stored as decimals. The following code segment appears in a program used to compute student percentages. Points that a student earns are stored in pointsEarned, the total points available in the grading period are stored in totalPoints, and the student percentage is stored in percentage. int pointsEarned; /* missing code */ Which of the following is most appropriate to replace /* missing code */ in the program? A. int totalPoints; int percentage; B. double totalPoints; int percentage; C. int totalPoints; double percentage; D. int totalPoints; boolean percentage; E. double totalPoints; boolean percentage;

C. int totalPoints; double percentage;

Consider the following code segment. int k = 35; while (k >= 0) { System.out.println("X"); k -= 5; } How many times will the string "X" be printed as a result of executing the code segment? A. 1 B. 7 C. 8 D. 35 E. More than 35 times, because the code segment will cause an infinite loop.

C. 8

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 Vbox class. public class Vbox { private int width; private int height; private int depth; public Vbox(int w, int h, int d) { width = w; height = h; depth = d; } public Vbox(int len) { width = len; height = len; depth = len; } } Which of the following declarations, appearing in a method in a class other than Vbox, will correctly instantiate a Vbox object? I. Vbox b1 = new Vbox(4); II. Vbox b2 = new Vbox(2, 8, 4); III. Vbox b3 = new Vbox(4.0, 4.0, 4.0); A. I only B. II only C. I and II only D. I and III only E. II and III only

C. I and II only

Consider the method digitSum below, which takes a positive integer parameter as input. public int digitSum(int num) { int total = 0; while (num > 0) { total += num % 10; num /= 10; } return total; } Which of the following code segments could replace the while loop in the method digitSum without changing the value returned by the method? I. for (int h = 0; h < num; h++) { total += num % 10; num /= 10; } II. for (int j = num; j > 0; j--) { total += j % 10; } III. for (int k = num; k > 0; k /= 10) { total += k % 10; } A. I only B. II only C. III only D. I and II E. II and III

C. III only

Consider the following code segment. int n = 6; for (int i = 1; i < n; i = i + 2) // Line 2 { System.out.print(i + " "); } Which of the following best explains how changing i < n to i <= n in line 2 will change the result? A. An additional value will be printed because the for loop will iterate one additional time. B. One fewer value will be printed because the for loop will iterate one fewer time. C. There will be no change to the program output because the loop will iterate the same number of times. D. An infinite loop will occur because the loop condition will never be false. E. The body of the loop will not execute at all because the loop condition will initially be false.

C. There will be no change to the program output because the loop will iterate the same number of times.

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 method. public void printSomething (int num, boolean val) { num--; System.out.print(val); System.out.print(num); } Consider the following code segment, which appears in a method in the same class as printSomething. printSomething(1, true); printSomething(2, true); What is printed as a result of executing the code segment? A. 0true1true B. 1true2true C. true0true1 D. true1true0 E. true1true2

C. true0true1

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

D. Hello!How are you?

Which of the following arithmetic expressions evaluates to 1 ? I. 2 / 5 % 3 II. 2 / (5 % 3) III. 2 / 5 + 1 A. I only B. II only C. I and II only D. II and III only E. I, II, and III

D. II and III only

Consider the following code segment. int x = /* initial value not shown */; int y = /* initial value not shown */; int z = x; z /= y; z += 2; Which of the following best describes the behavior of the code segment? A. It sets z to 2. B. It sets z to x. C. It sets z to (1 / y) + 2. D. It sets z to (x / y) + 2. E. It sets z to (x + 2) / y.

D. It sets z to (x / y) + 2.

Consider the following code segment: /* data type 1 */ x = 0.5; /* data type 2 */ y = true; Which of the following best describes the data types that should be used to replace/* data type 1 */ and /* data type 2 */ so that the code segment compiles without error? A. The variable x should be declared as an int and the variable y should be declared as a boolean. B. The variable x should be declared as an int and the variable y should be declared as an int. C. The variable x should be declared as a double and the variable y should be declared as an int. D. The variable x should be declared as a double and the variable y should be declared as a boolean. E. The variable x should be declared as a boolean and the variable y should be declared as a boolean.

D. The variable x should be declared as a double and the variable y should be declared as a boolean.

Consider the following class declaration. public class Thing { private String color; public Thing() { color = "Blue"; } public Thing(String setColor) { color = setColor; } } Which of the following code segments, when appearing in a class other than Thing, would create a reference of type Thing with a value of null ? A. Thing someThing = new Thing("Green"); B. Thing someThing = new Thing(""); C. Thing someThing = new Thing(); D. Thing someThing; E. Thing("Green");

D. Thing someThing;

Consider the following code segment. System.out.println("W"); System.out.println("X"); System.out.print("Y"); System.out.print("Z"); What is printed as a result of executing the code segment? A. WXYZ B. W XYZ C. WX YZ D. W X YZ E. W X Y Z

D. W X YZ

Consider the following code segment. int x; int y; x = 3; y = /* missing expression */; x = 1 + 2 * y; System.out.print(x); System.out.println(y); Which of the following can be used as a replacement for /* missing expression */ so that the code segment prints 94 ? A. 3 B. x C. x - 1 D. x + 1 E. 1 - 2 * x

D. x + 1

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 method, which returns the lesser of its two parameters. public int min(int first, int second) { /* implementation not shown */ } Assume that each of the following expressions appears in a method in the same class as the method min. Assume also that the int variables p, q, and r have been properly declared and initialized. Which of the following expressions evaluates to the minimum value among p, q, and r? I. min(min(p, q), r) II. min(p, min(q, r)) III. min(min(p, q), p) 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 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

Consider the following code segment. for (int k = 0; k < 4; k++) { /* missing loop header */ { System.out.print(k); } System.out.println(); } The code segment is intended to produce the following output. 0 11 222 3333 Which of the following can be used to replace /* missing loop header */ so that the code segment will work as intended? A. for (int h = 0; h < k; h++) B. for (int h = 1; h < k + 1; h++) C. for (int h = 0; h < 3; h++) D. for (int h = k; h >= 0; h--) E. for (int h = k; h <= 0; h--)

D. for (int h = k; h >= 0; h--)

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

A school administrator has created a Student class. The class contains variables to represent the following. An int variable called studentID to represent the student's ID number A String variable called studentName to represent the student's name The school administrator has also created a Parent class. The class contains variables to represent the following. A String variable called parentName to represent the parent's name A String variable called email to represent the parent's e-mail address The object penelope will be declared as type Student. The object mrsPatel will be declared as type Parent. Which of the following descriptions is accurate? A. An attribute of the penelope object is email. B. An attribute of the penelope object is Parent. C. An attribute of the penelope object is Student. D. An attribute of the mrsPatel object is studentName. E. An attribute of the mrsPatel object is email.

E. An attribute of the mrsPatel object is email.

Consider the following code segment. System.out.println(hello); // Line 1 System.out.print(world); // Line 2 The code segment is intended to produce the following output but does not work as intended. hello world Which of the following changes can be made so that the code segment produces the intended output? A. Inserting System.out.print(); between lines 1 and 2 B. Inserting System.out.println(); between lines 1 and 2 C. Changing println in line 1 to print D. Changing print in line 2 to println E. Enclosing hello in line 1 and world in line 2 in quotation marks

E. Enclosing hello in line 1 and world in line 2 in quotation marks

The volume of a cylinder is equal to the height times the area of the circular base. The area of the circular base is equal to π (pi) times the square of the radius. The code segment below is intended to compute and print the volume of a cylinder with radius r and height h. Assume that the double variables r, h, and pi have been properly declared and initialized. /* missing code */ System.out.print(volume); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? I. double baseArea = pi * r * r; double volume = baseArea * h; II. double volume = pi * r * r; volume = volume * h; III. double volume = pi * r * r * h; A. I only B. III only C. I and III only D. II and III only E. I, II, and III

E. I, II, and III

Consider the following class declaration. public class VetRecord { private String name; private int age; private int weight; private boolean needsVaccine; public VetRecord(String nameP, int ageP, int weightP, boolean needsVaccineP) { name = nameP; age = ageP; weight = weightP; needsVaccine = needsVaccineP; } public VetRecord(String nameP, int ageP, int weightP) { name = nameP; age = ageP; weight = weightP; needsVaccine = true; } } A new constructor is to be added to the VetRecord class. Which of the following is NOT a possible header for the new constructor? A. VetRecord(int ageP, int weightP) B. VetRecord(int ageP, boolean needsVaccine) C. VetRecord(String nameP, int ageP) D. VetRecord(String nameP, boolean needsVaccine) E. VetRecord(String nameP, int weightP, int ageP)

E. VetRecord(String nameP, int weightP, int ageP)

Consider the following method, which takes as input a temperature in degrees Fahrenheit and returns the corresponding temperature in degrees Celsius. public double fahrenheitToCelsius(double f) { double c = (f - 32) * 5 / 9; return c; } Assume that each of the following code segments appears in a method in the same class as fahrenheitToCelsius. Which of the following code segments prints the temperature in degrees Celsius that corresponds to 32 degrees Fahrenheit? A. double f = 32.0; fahrenheitToCelsius(); System.out.println(f); B. double f = 32.0; f = fahrenheitToCelsius(); System.out.println(f); C. double f = 32.0; fahrenheitToCelsius(f); System.out.println(f); D. double f = 32.0; double c = fahrenheitToCelsius(); System.out.println(c); E. double f = 32.0; double c = fahrenheitToCelsius(f); System.out.println(c);

E. double f = 32.0; double c = fahrenheitToCelsius(f); System.out.println(c);

Consider the following class definition. public class AnimalPrinter { public void printDog() { System.out.println("dog"); } public void printCat() { System.out.println("cat"); } /* constructors not shown */ } The method myMethod appears in a class other than AnimalPrinter. The method is intended to produce the following output. dog cat Assume that an AnimalPrinter object myPrinter has been properly declared and initialized inside myMethod. Which of the following code segments, if located in myMethod, will produce the intended output? A. printDog(); printCat(); B. printDog(AnimalPrinter); printCat(AnimalPrinter); C. AnimalPrinter.printDog(); AnimalPrinter.printCat(); D. printDog(myPrinter); printCat(myPrinter); E. myPrinter.printDog(); myPrinter.printCat();

E. myPrinter.printDog(); myPrinter.printCat();

Consider the following class. public class Purchase { private double purchase; private double tax; public Purchase(double purchaseAmt, double taxAmt) { purchase = purchaseAmt; tax = taxAmt; } public void totalAmount() { System.out.print(purchase + tax); } } Assume that a Purchase object p has been properly declared and initialized. Which of the following code segments will successfully print the total purchase amount associated with p? A. Purchase.totalAmount(); B. System.out.print(p); C. totalAmount(p); D. System.out.print(p.totalAmount()); E. p.totalAmount();

E. p.totalAmount();

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. for (int j = 0; j < 4; j++) { for (int k = 0; k < j; k++) { System.out.println("hello"); } } Which of the following best explains how changing the inner for loop header to for (int k = j; k < 4; k++) will affect the output of the code segment? A. The output of the code segment will be unchanged. B. The string "hello" will be printed three fewer times because the inner loop will iterate one fewer time for each iteration of the outer loop. C. The string "hello" will be printed four fewer times because the inner loop will iterate one fewer time for each iteration of the outer loop. D. The string "hello" will be printed three additional times because the inner loop will iterate one additional time for each iteration of the outer loop. E. The string "hello" will be printed four additional times because the inner loop will iterate one additional time for each iteration of the outer loop.

E. The string "hello" will be printed four additional times because the inner loop will iterate one additional time for each iteration of the outer loop.

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


Related study sets

Unit 3: GI/GU/Endocrine Practice

View Set

Lesson 3: Performing Security Assessments

View Set

Manufacturing and Service Process Structures

View Set

Theatre in Contemporary Society Midterm

View Set