AP JAVA
Consider the following code segment. int[] arr = {4, 3, 2, 1, 0}; int total = 0; for (int k = 0; k <= total; k++) { if (arr[k] % 2 == 0) total += arr[k]; else total -= arr[k]; } System.out.print(total); What, if anything, is printed as a result of executing the code segment?
1
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?
3 1.5
Which of the following expressions represents x|k−j|x|k−j|, where x, k, and j are properly declared and initialized int variables? 1. Math.abs(k - j, Math.pow(x)); 2. Math.abs(Math.pow(x), k - j); 3. Math.pow(x, Math.abs(k - j)); 4. Math.pow(Math.abs(k - j), x); 5. Math.pow.abs(x, k - j));
3. Math.pow(x, Math.abs(k - j));
Consider the following variable declarations and initializations. int a = 2; int b = 6; int c = 3; Which of the following expressions evaluates to false ? 1. a < b == c < b 2. a > b == b < c 3. a < b != b < c 4. a < b != c < b 5. a > b != b > c
4. a < b != c < b
Consider the following code segment. String str = "AP"; str += "CS " + 1 + 2; System.out.println(str); What is printed as a result of executing the code segment?
APCS 12
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? 1. There are no errors and the code works as intended. 2. Either the numerator or the denominator of the fraction 1 / 2 should be cast as double. 3. The expression fact1 * fact 2 should be cast as double. 4. The expressions 1 / 2 and 3 * 4 should both be cast as double. 5. The variables fact1 and fact2 should both be declared as int.
2. Either the numerator or the denominator of the fraction 1 / 2 should be cast as double.
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?
20
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? 1. The code segment stores 17 in avg because 17 is the result of the integer division of 35 by 2. 2. The code segment stores 18 in avg because 18 is the result of the integer division of 35 by 2. 3. The code segment stores 17.5 in avg because 17.5 is the result of the floating point division of 35.0 by 2. 4. The code segment does not compile because int values cannot be added to double values. 5. The code segment does not compile because a double value cannot be divided by an int value.
3. 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 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? 1. int result = secret(4, 4); 2. int result = secret(4, 4.0); 3. double result = secret(4, 4.0); 4. double result = secret(4.0, 4); 5. double result = secret(4.0, 4.0);
3. double result = secret(4, 4.0);
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? 1. It sets z to 2. 2. It sets z to x. 3. It sets z to (1 / y) + 2. 4. It sets z to (x / y) + 2. 5. It sets z to (x + 2) / y.
4. It sets z to (x / y) + 2.
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? 1. a = 1, b = 1, c = 2 2. a = 1, b = 2, c = 1 3. a = 1, b = 2, c = 3 4. a = 2, b = 2, c = 2 5. a = 3, b = 2, c = 1
5. a = 3, b = 2, c = 1
Consider the following method. public double calculate(double x) { return x + 1.5; } The following code segment calls the method calculate in the same class. Double d1 = new Double(7.5); System.out.println(calculate(d1)); What, if anything, is printed when the code segment is executed?
9.0
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?
Enclosing hello in line 1 and world in line 2 in quotation marks
The array fruits is declared below. String [] fruits = {"apples", "bananas", "cherries", "dates"}; Which of the following code segments will cause an ArrayIndexOutOfBoundsException ? I. for (int i = 0; i <= fruits.length; i++) { System.out.println(fruits[i]); } II. for (int i = 0; i <= fruits.length - 1; i++) { System.out.println(fruits[i]); } III. for (int i = 1; i <= fruits.length; i++) { System.out.println(fruits[i - 1]); }
I only
Which of the following code segments can be used to set the value of the string str to "Good morning, sunshine!" ? 1. String str = "Good " + "morning," + " sunshine!"; 2. String str = "Good"; str += " morning, sunshine!"; 3. String str = " morning, "; str = "Good" + str + "sunshine!";
I, II, and III
Consider the following method, which is intended to return the number of strings of length greater than or equal to 3 in an array of String objects. public static int checkString(String[] arr) { int count = 0; for (int k = 0; k < arr.length; k++) { if (arr[k].length() >= 3) count++; } return count; } Which of the following code segments compile without error? 1. checkString(new String[]); 2. checkString(new String[0]); 3. String[] str = {"cat", "dog"}; checkString(str);
II and III only
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?
W X YZ
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
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?
cat dog horse cow
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?
int totalPoints; double percentage;
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?
true0true1
Each of the following code segments is intended to print the word Hello. Which of the following code segments works as intended? 1. System.out.print("Hello"); 2. System.out.print(Hello); 3. System.out.print(He); System.out.print(llo);
1 only
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? 1. temp == 30 2. temp == 51 3. temp == 60
1 only
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? 1. Code segment I and code segment II produce the same output for all values of x and y. 2. Code segment I and code segment II produce the same output only when x is equal to y. 3. Code segment I and code segment II produce the same output only when x is not equal to y. 4. Code segment I and code segment II produce the same output only when x is less than y. 5. Code segment I and code segment II do not produce the same output for any values of x and y.
1. Code segment I and code segment II produce the same output for all values of x and y.
Consider the following method, which is intended to return the index of the first negative integer in a given array of integers. public int positionOfFirstNegative(int[] values) { int index = 0; while (values[index] >= 0) { index++; } return index; } What precondition is needed on the values array so that the method will work as intended? 1. The array values must contain at least one negative integer. 2. The array values must contain at least one nonnegative integer. 3. The array values must contain at least one positive integer. 4. No precondition is needed. The method will never work as intended. 5. No precondition is needed. The method will always work as intended.
1. The array values must contain at least one negative integer.
Consider the following code segment. int[] arr = {1, 2, 3, 4, 5}; Which of the following code segments would correctly set the first two elements of array arr to 10 so that the new value of array arr will be {10, 10, 3, 4, 5} ? 1. arr[0] = 10; arr[1] = 10; 2. arr[1] = 10; arr[2] = 10; 3. arr[0, 1] = 10; 4. arr[1, 2] = 10; 5. arr = 10, 10, 3, 4, 5;
1. arr[0] = 10; arr[1] = 10;
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?
10.0
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?
12 20
Consider the following code segment. Integer original = new Integer(8); Integer first = new Integer(original.intValue() * 2); Integer second = new Integer(original.intValue() + 2); System.out.println(first.intValue() + " " + second.intValue()); What is printed when the code segment is executed?
16 10
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? 1. (sweet && salty) || sour 2. (sweet && salty) || (sweet && sour) 3. (sweet && salty) && (sweet && sour) 4. (sweet || salty) && sour 5. (sweet || salty) && (sweet || sour)
2. (sweet && salty) || (sweet && sour)
Consider the following code segment. Integer num = new Integer(15); int n = num.intValue(); Which of the following statements best describes the type and contents of num and n after the code segment executes? 1. num is an Integer that contains the value 15, and n is an int that contains the value 0. 2. num is an Integer that contains the value 15, and n is an int that contains the value 15. 3. num is an Integer that contains the value 15, and n is an Integer that contains the value 15. 4. num is an int that contains the value 15, and n is an Integer that contains the value 0. 5. num is an int that contains the value 15, and n is an Integer that contains the value 15.
2. num is an Integer that contains the value 15, and n is an int that contains the value 15.
Consider the following code segment, which is intended to print the maximum value in an integer array values. Assume that the array has been initialized properly and that it contains at least one element. int maximum = /* missing initial value */; for (int k = 1; k < values.length; k++) { if (values[k] > maximum) maximum = values[k]; } System.out.println(maximum); Which of the following should replace /* missing initial value */ so that the code segment will work as intended? 1. 0 2. values[0] 3. values[1] 4. Integer.MIN_VALUE 5. Integer.MAX_VALUE
2. values[0]
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? 1. There is no error and the code works as intended. 2. The code should have cast the expression num1 / num2 to double. 3. The code should have cast either num1 or num2 in the expression num1 / num2 to double. 4. The code should have declared ans as an int. 5. The code should have initialized num1 to 5.0 and num2 to 10.0.
3. The code should have cast either num1 or num2 in the expression num1 / num2 to double.
Consider the following code segment. int[] numbers = {1, 2, 3, 4, 5, 6}; for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } Which of the following for loops produces the same output as the code segment? 1. for (int x : numbers) { System.out.println(numbers[x]); } 2. for (int x : numbers) { System.out.println(numbers); } 3. for (int x : numbers) { System.out.println(x); } 4. for (numbers : int x) { System.out.println(numbers[x]); } 5. for (numbers : int x) { System.out.println(x); }
3. for (int x : numbers) { System.out.println(x); }
Consider the following code segment. int[] arr = {10, 20, 30, 40, 50}; for(int x = 1; x < arr.length - 1; x++) { arr[x + 1] = arr[x] + arr[x + 1]; } Which of the following represents the contents of arr after the code segment has been executed? 1. {10, 20, 30, 70, 120} 2. {10, 20, 50, 90, 50} 3. {10, 20, 50, 90, 140} 4. {10, 30, 60, 100, 50} 5. {10, 30, 60, 100, 150}
3. {10, 20, 50, 90, 140}
The code segment below is intended to set the boolean variable duplicates to true if the int array arr contains any pair of duplicate elements. Assume that arr has been properly declared and initialized. boolean duplicates = false; for (int x = 0; x < arr.length - 1; x++) { /* missing loop header */ { if (arr[x] == arr[y]) duplicates = true; } } Which of the following can replace /* missing loop header */ so that the code segment works as intended? 1. for (int y = 0; y <= arr.length; y++) 2. for (int y = 0; y < arr.length; y++) 3. for (int y = x; y < arr.length; y++) 4. for (int y = x + 1; y < arr.length; y++) 5. for (int y = x + 1; y <= arr.length; y++)
4. for (int y = x + 1; y < arr.length; y++)
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? 1. Purchase.totalAmount(); 2. System.out.print(p); 3. totalAmount(p); 4. System.out.print(p.totalAmount()); 5. p.totalAmount();
5. p.totalAmount();
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) ?
50
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?
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?
AD
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?
Changing print to println in lines 1 and 2
Consider the following code segment. String s1 = "ABCDEFGHI"; String s2 = s1.substring(6, 7); String s3 = new String("abcdefghi"); String s4 = s3.substring(4, 5); String s5 = s3.substring(2, 3); System.out.print(s2 + s4 + s5); What, if anything, is printed when the code segment is executed?
Gec
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?
Hello!How are you?
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? 1. Vbox b1 = new Vbox(4); 2. Vbox b2 = new Vbox(2, 8, 4); 3. Vbox b3 = new Vbox(4.0, 4.0, 4.0);
I and II only
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? 1. min(min(p, q), r) 2. min(p, min(q, r)) 3. min(min(p, q), p)
I and II only
Consider the following method. public int[] transform(int[] a) { a[0]++; a[2]++; return a; } The following code segment appears in a method in the same class as transform. /* missing code */ arr = transform(arr); After executing the code segment, the array arr should contain {1, 0, 1, 0}. Which of the following can be used to replace /* missing code */ so that the code segment works as intended? 1. int[] arr = {0, 0, 0, 0}; 2. int[] arr = new int[0]; 3. int[] arr = new int[4];
I and III
Consider the following class definition. public class Toy { private int yearFirstSold; public int getYearFirstSold() { return yearFirstSold; } /* There may be instance variables, constructors, and other methods not shown. */ } The following code segment, which appears in a class other than Toy, prints the year each Toy object in toyArray was first sold by its manufacturer. Assume that toyArray is a properly declared and initialized array of Toy objects. for (Toy k : toyArray) { System.out.println(k.getYearFirstSold()); } Which of the following could be used in place of the given code segment to produce the same output? I. for (int k = 0; k < toyArray.length; k++) { System.out.println(getYearFirstSold(k)); } II. for (int k = 0; k < toyArray.length; k++) { System.out.println(k.getYearFirstSold()); } III. for (int k = 0; k < toyArray.length; k++) { System.out.println(toyArray[k].getYearFirstSold()); }
III only
Consider the following code segment. String word = "September"; String str1 = word.substring(0, 3); String str2 = word.substring(word.length() - 3); System.out.println(str1 + str2); What is printed when the code segment is executed?
Sepber
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 = 1, b = 1, and c = 0
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?
c
Consider the following code segment. String dessert = "pie"; dessert += "straw" + dessert + "berry"; What is the value of dessert after the code segment has been executed?
piestrawpieberry
Consider the following expression. (3 + 4 == 5) != (3 + 4 >= 5) What value, if any, does the expression evaluate to?
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?
true false
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?
true false false
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?
115
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? 1. An attribute of the penelope object is email. 2. An attribute of the penelope object is Parent. 3. An attribute of the penelope object is Student. 4. An attribute of the mrsPatel object is studentName. 5. An attribute of the mrsPatel object is email.
5. An attribute of the mrsPatel object is email.
In the code segment below, assume that the int array numArr has been properly declared and initialized. The code segment is intended to reverse the order of the elements in numArr. For example, if numArr initially contains {1, 3, 5, 7, 9}, it should contain {9, 7, 5, 3, 1} after the code segment executes. /* missing loop header */ { int temp = numArr[k]; numArr[k] = numArr[numArr.length - k - 1]; numArr[numArr.length - k - 1] = temp; } Which of the following can be used to replace /* missing loop header */ so that the code segment works as intended? 1. for (int k = 0; k < numArr.length / 2; k++) 2. for (int k = 0; k < numArr.length; k++) 3. for (int k = 0; k < numArr.length / 2; k--) 4. for (int k = numArr.length - 1; k >= 0; k--) 5.vfor (int k = numArr.length - 1; k >= 0; k++)
1. for (int k = 0; k < numArr.length / 2; k++)
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? 1. greg is an instance of the Student class. 2. greg is an instance of the updateAverage method. 3. greg is an instance of three attributes. 4. Student is an instance of the greg object. 5. updateAverage is an instance of the Student class.
1. greg is an instance of the Student class.
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?
11 0
Consider the following code segment. String one = "ABC123"; String two = "C"; String three = "3"; System.out.println(one.indexOf(two)); System.out.println(one.indexOf(three)); System.out.println(two.indexOf(one)); What is printed when the code segment is executed?
2 5 -1
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? 1. (j != k) || (k < m) 2. (j != k) || (k <= m) 3. (j == k) || (k < m) 4. (j != k) && (k <= m) 5. (j == k) && (k < m)
2. (j != k) || (k <= m)
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? 1. The value of x and the value of y have not been changed. 2. Both the value of x and the value of y have been decreased. 3. Both the value of x and the value of y have been increased. 4. The value of x has been decreased and the value of y has been increased. 5. The value of x has been increased and the value of y has been decreased.
2. Both the value of x and the value of y have been decreased.
The Fibonacci numbers are a sequence of integers. The first two numbers are 1 and 1. Each subsequent number is equal to the sum of the previous two integers. For example, the first seven Fibonacci numbers are 1, 1, 2, 3, 5, 8, and 13. The following code segment is intended to fill the fibs array with the first ten Fibonacci numbers. The code segment does not work as intended. int[] fibs = new int[10]; fibs[0] = 1; fibs[1] = 1; for (int j = 1; j < fibs.length; j++) { fibs[j] = fibs[j - 2] + fibs[j - 1]; } Which of the following best identifies why the code segment does not work as intended? 1. In the for loop header, the initial value of j should be 0. 2. In the for loop header, the initial value of j should be 2. 3. The for loop condition should be j < fibs.length - 1. 4. The for loop condition should be j < fibs.length + 1. 5. The for loop should increment j by 2 instead of by 1.
2. In the for loop header, the initial value of j should be 2.
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? 1. The code segment works as intended for all nonnegative values of len1, len2, and len3. 2. 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. 3. 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. 4. 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. 5. 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.
2. 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.
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?
2.5 :o
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? 1. sunny || windy 2. !sunny || !windy 3. !(sunny || windy) 4. !(sunny && windy) 5. !(sunny && !windy)
3. !(sunny || windy)
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? 1. An instance of the story class is Book. 2. An instance of the Book object is story. 3. An attribute of the story object is isHardcover. 4. An attribute of the pages object is Book. 5. An attribute of the Book instance is story.
3. An attribute of the story object is isHardcover.
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?
3.0
Consider the following code segment. int num = 5; num *= 2; num %= 6; What is the value of num after the code segment is executed?
4
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?
4
Consider the following two code segments. I. int[] arr = {1, 2, 3, 4, 5}; for (int x = 0; x < arr.length; x++) { System.out.print(arr[x + 3]); } II. int[] arr = {1, 2, 3, 4, 5}; for (int x : arr) { System.out.print(x + 3); } Which of the following best describes the behavior of code segment I and code segment II ? 1. Both code segment I and code segment II will print 45. 2. Both code segment I and code segment II will print 45678. 3. Code segment I will cause an ArrayIndexOutOfBoundsException and code segment II will print 45. 4. Code segment I will cause an ArrayIndexOutOfBoundsException and code segment II will print 45678. 5. Both code segment I and code segment II will cause an ArrayIndexOutOfBoundsException.
4. Code segment I will cause an ArrayIndexOutOfBoundsException and code segment II will print 45678.
The method below is intended to return the area of a square whose side length is s units. The area of a square is its side length times itself. public double squareArea(double s) { return /* missing code */ } Which of the following can be used to replace /* missing code */ so that the method works as intended? 1. pow(s, 2); 2. s.pow(2); 3. Math.pow(s); 4. Math.pow(s, 2); 5. Math.pow(2, s);
4. Math.pow(s, 2);
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 ? 1. Thing someThing = new Thing("Green"); 2. Thing someThing = new Thing(""); 3. Thing someThing = new Thing(); 4. Thing someThing; 5. Thing("Green");
4. Thing someThing;
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? 1. num = 20, min = 30, max = 50 2. num = 30, min = 20, max = 40 3. num = 40, min = 10, max = 40 4. num = 50, min = 50, max = 50 5. num = 60, min = 40, max = 50
4. 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? 1. Both code segment I and code segment II produce correct output for all values of str. 2. Neither code segment I nor code segment II produce correct output for all values of str. 3. 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". 4. 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". 5. 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".
5. 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 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? 1. VetRecord(int ageP, int weightP) 2. VetRecord(int ageP, boolean needsVaccine) 3. VetRecord(String nameP, int ageP) 4. VetRecord(String nameP, boolean needsVaccine) 5. VetRecord(String nameP, int weightP, int ageP)
5. 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? 1. double f = 32.0; fahrenheitToCelsius(); System.out.println(f); 2. double f = 32.0; f = fahrenheitToCelsius(); System.out.println(f); 3. double f = 32.0; fahrenheitToCelsius(f); System.out.println(f); 4. double f = 32.0; double c = fahrenheitToCelsius(); System.out.println(c); 5. double f = 32.0; double c = fahrenheitToCelsius(f); System.out.println(c);
5. double f = 32.0; double c = fahrenheitToCelsius(f); System.out.println(c);
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? 1. if (a <= b + 1) a++; else if (b <= a - 1) b++; else { a++; b++; } 2. if (a + 1 <= b) a++; else if (b - 1 <= a) b++; else { a++; b++; } 3. if (a - b < 0) a++; else if (b - a > 0) b++; else { a++; b++; } 4. if (a != b) a++; else if (b != a) b++; else { a++; b++; } 5. if (a == b) { a++; b++; } else if (a < b) a++; else b++;
5. if (a == b) { a++; b++; } else if (a < b) a++; else b++;
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? 1. printDog(); printCat(); 2. printDog(AnimalPrinter); printCat(AnimalPrinter); 3. AnimalPrinter.printDog(); AnimalPrinter.printCat(); 4. printDog(myPrinter); printCat(myPrinter); 5. myPrinter.printDog(); myPrinter.printCat();
5. myPrinter.printDog(); myPrinter.printCat();
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? 1. double baseArea = pi * r * r; double volume = baseArea * h; 2. double volume = pi * r * r; volume = volume * h; 3. double volume = pi * r * r * h;
I, II, and III
Which of the following arithmetic expressions evaluates to 1? (int) 1. 2 / 5 % 3 2. 2 / (5 % 3) 3. 2 / 5 + 1
II and III only
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? 1. MagicNumber.add_2(); MagicNumber.displayNumber(); 2. MagicNumber n1 = newMagicNumber(); n1.add_2(); n1.displayNumber(); 3. n2.add_2(); n2.displayNumber();
II only
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? 1. int top = x - y; int bottom = y - x; 2. int top = 2 * x; int bottom = y - top; 3. int top = x + y; int bottom = 2 * top;
II only
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?
The variable x should be declared as a double and the variable y should be declared as a boolean.
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 ?
def
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?
true
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?
true true false
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 ?
x + 1