Term 2: Lesson 5 - AP-Style MC Practice
Consider the following recursive method. public static void doWhat(int num) { if(num < 15) { doWhat(num + 4); System.out.print(num + " "); } } What is printed as a result of the call doWhat(2)?
14 10 6 2
Consider the following methods which appear within the same class. public static void mystery(int[] data) { for (int k = 0; k < data.length - 1; k+=2) { int t = data[k]; data[k] = data[k+1]; data[k + 1] = t; } } public static String toString(int[] data) { String str = ""; for (int d : data) str = str + d + " "; return str; } The following code segment appears in the main method of the same class.
2 1 3 7 5
Consider the following method public static void mystery(ArrayList words) { int k = 0; int newSize = 3*words.size(); while (words.size() < newSize && words.size() > 0) { words.add(words.get(k)); k++; } } Which of the following describes what the method mystery() does to the ArrayList words?
The ArrayList words is extended to three times its size by repeating the entire contents of the list three times in sequence.
public static int magic(int a) { int x = 9; while (x < 20) { x = 2*x; x = x - a; a--; } return x; } Which of the following is true about the behavior of method mystery?
The method will result in an infinite loop for some arguments, but will return a number for all arguments which are sufficiently small.
Consider the following method. public static int mystery(int[] data) { int times = 0; int counter=0; for (int j = 0; j < data.length; j++) { counter = 0; for (int k = j; k < data.length; k++) { if (data[j] == data[k]) { counter ++; } } if (counter > times) { times = counter; } } return times; } Assume that data has been declared and initialized as an array of integer values. Which of the following best describes the value returned by the call mystery(data)?
The number of times that a most frequently occurring value appears in data
Consider the following method. public static String shorten(String word, int cut) { return word.substring(0, cut) + word.substring(word.length() - cut); } What value is returned as a result of the call shorten(debugging, 2)?
deng
Consider the following method, hasRepeats, which is intended to return true if an array of integers contains the same value more than once and false otherwise. /** @param arr an array of integers * @return true if an element in the array is repeated */ public static boolean hasRepeats(int[] arr) { for (int k = 0; k < arr.length; k++) { for (/* missing code */) { if (arr[j] == arr [k]) return true; } } return false; } Which of the following should be used to replace /* missing code */ so that hasRepeats works as intended?
int j = k + 1; j < arr.length; j++
Consider the following class declaration. public class Car { private String carMake; private String carModel; public Car(String make, String model) { carMake = make; carModel = model; } public String getMake() { return carMake; } public void setMake(String make) { carMake = make; } // There may be instance variables, constructors, and methods that are not shown. } Assume that the following declaration has been made. Car myRide = new Car("VW", "Polo"); Which of the following statements is the most appropriate for changing the make of myRide from "VW" to "Volkswagen"?
myRide.setMake("Volkswagen");
Consider the following method. public static boolean mystery(int num) { int temp = num; int newNum = 0; while (temp > 0) { newNum = 10*newNum + temp % 10; temp /= 10; } return (newNum == num); } Which of the following calls to mystery will return true?
mystery(184481)
Consider the following method public static String mashup(String str, int[] arr) { String result = ""; for (int x : arr) result = result + str.substring(0, x); return result; } The following code appears in another method in the same class. int[] nums = {1, 5, 3}; String word = "program"; System.out.println(mashup(word, nums)); What is printed when the code above is executed?
pprogrpro