AP Computer Science Multiple Choice Quiz

  1. Consider searching for a given value in an array. Which of the following must be true in order to successfully use the binary search?

    I. The values in the array must be numbers.
    II. The values in the array must be in sorted order.
    III. The array must not contain any duplicate values.

  2. Consider searching for a given value in a sorted array. Under which of the following circumstances will the sequential search be faster than the binary search?

  3. Assume the following declarations and assignments have been made.

    int[] arr1 = {1, 2, 3, 4, 5, 6, 7, 8};
    int[] arr2 = {4, 5, 6, 7, 8};

    If the following segment of code is executed, what will arr1 and arr2 contain?

    arr2 = arr1;
    arr2[3] = 0;
  4. Assume that variable myList is an ArrayList that contains at least two objects. Which of the following code segments moves the first object in the list to the end of the list?

  5. Which of the following will compile without error?

    I. ArrayList<String> words = new ArrayList<String>();
    II. ArrayList<String> words = new List<String>();
    III. List<String> words = new ArrayList<String>();

  6. Assume that words is an ArrayList<String> and that val is a non-null String. Consider the following code segment:

    	boolean tmp = false;
    	for(String word: words)
    	{
    		if(val.equals(word))
    			tmp = true;
    	}

    Which of the following best characterizes the conditions under which tmp is assigned true?

  7. Consider the following code segment.

    	ArrayList<String> letters = new ArrayList<String>();
    	letters.add("I");
    	letters.add("J");
    	letters.add("G");
    	letters.add("M");
    	letters.add("H");
    	letters.set(3,letters.get(4));
    	letters.set(4,letters.get(3));
    	System.out.println(letters);

    What is printed as a result of executing the code segment?

  8. Assume that the variable numbers is an ArrayList<Integer>. Consider the following code segment.

    	boolean flag = true;
    	for (Integer val : numbers)
    	{
    		flag = flag && (val.intValue() > 0);
    	}

    What does this code segment do?

  9. Consider the following code segment:

    	x = !y;
    	y = x || y;

    Assume that x and y are boolean variables that have been initialized before this code segment executed. What is true about the values of x and y after the code executes?

  10. What is the output of the following code segment?

    	List<String> list = new ArrayList<String>();
    	list.add("A");
    	list.add("B");
    	list.add("C");
    	list.add("D");
    	list.add("E");
    	for (int k = 1; k <= 3; k++)
    		list.remove(1);
    	for (int k = 1; k <= 3; k++)
    		list.add(1, "*");
    	System.out.println(list);
  11. The following method is supposed to calculate and return the sum of the numbers from 1 to n, where n > 0.

    public static int sumToN(int n) { int sum = 0; int count = 0; while(count < n) { sum += count; } return sum; }

    Which statement best describes what method sumToN returns?

  12. Consider the following code segment.

    	List<String> list = new ArrayList<String>();
    	list.add("P");
    	list.add("Q");
    	list.add("R");
    	list.set(2, "s");
    	list.add(2, "T");
    	list.add("u");
    	System.out.println(list);

    What is printed as a result of executing the code segment?

  13. Assume that a, b, and c are variables of type int. Consider the three expressions shown below.

    I. (a == b) && (a == c) && (b == c)
    II. (a == b) || (a == c) || (b == c)
    III. ((a – b) * (a – c) * (b – c)) == 0

    Assume that subtraction and multiplication never overflow. Which of the expressions will always be true if at least two of a, b, and c are equal?

  14. Which of the following is a reason to use an ArrayList instead of an array?

  15. The last element of the returned array (result[result.length − 1]) may not have the correct value. One or more of nums[0] … nums[nums.length / 2 − 1] may have been copied to the wrong position(s) in the returned array. Executing shuffle may cause an ArrayIndexOutOfBoundsException. The first element of the returned array (result[0]) may not have the correct value. One or more of nums[nums.length / 2] … nums[nums.length] may have been copied to the wrong position(s) in the returned array.

  16. Consider the following methods:

    	public static int add(int x, int y)
    	{
    		return x + y;
    	}
    	public static int multiply(int x, int y)
    	{
    		return x * y;
    	}

    What is the value of the expression multiply(3, add(4, 5)) ?

  17. Consider the following methods:

    public static int add(int x, int y)
    	{
    		return x + y;
    	}
    	public static int multiply(int x, int y)
    	{
    		return x * y;
    	}

    Which of the following corresponds to this expression: multiply(2, add(multiply(a, b), add(multiply(a, c), multiply(b, c))))

  18. Consider the following method:

    	public boolean somethingDifferent(boolean p, boolean q)
    	{
    		return (p || q) && !(p && q);
    	}

    Which statement is true about this method?

  19. Consider the following method. What does it print out?

    	String s = "How do you do?";
    	int index = s.indexOf("o");
    	while(index >= 0)
    	{
    		System.out.println(index + " ");
    		s = s.substring(index + 1);
    		index = s.indexOf("o");
    	}
  20. Consider two algorithms for shuffling a deck of cards. Which is a true statement concerning Algorithm 1 and Algorithm 2?

  21. Consider the shuffle method that uses Algorithm 2. Which of the following changes should be made to code the shuffle method as specified?

  22. Consider the Performer and Singer classes. Assume these declarations:

    	Performer p = new Performer();
    	Performer s = new Singer();

    What is the result of the call p.act() ?

  23. Consider the Performer and Singer classes. Assume these declarations:

    	Performer p = new Performer();
    	Performer s = new Singer();

    What is the result of the call s.act() ?

  24. Consider the abstract class Vehicle and class Car that extends Vehicle. Which statement is true about Vehicle?

  25. Consider the Dog and UnderDog classes. Assume these declarations:

    	Dog spot = new Dog();
    	Dog fido = new UnderDog();

    What is the result of the call spot.act() ?

  26. Consider the Dog and UnderDog classes. Assume these declarations:

    	Dog spot = new Dog();
    	Dog fido = new UnderDog();

    What is the result of the call fido.act() ?

  27. Consider the Vehicle and Car classes where Car extends Vehicle. Assume that the following code segment appears in client code:

    	Vehicle v = new Car();
    	v.setPrice(1000.0);

    Which of the following is true?

  28. Consider the following classes:

    	public class Base
    	{
    		private int a1;
    		public void methodA()
    		{
    			methodB(); // Statement I
    		}
    	}
    	public class Derived extends Base
    	{
    		public void methodB()
    		{
    			methodA(); // Statement II
    			a1 = 0; // Statement III
    		}
    	}

    Which of the labeled statements in the methods shown above will cause a compile-time error?

  29. The following method is intended to return the index of the "smallest" word, namely the word that would appear first in an alphabetized list:

    	public static int findMin(String[] words)
    	{
    		int minPos = 0;
    		for (int index = 1; index < words.length; index++)
    		{
    			if ( /* condition */ )
    			{
    				minPos = index;
    			}
    		}
    		return minPos;
    	}

    Which of the following should be used to replace /* condition */ so that findMin works as intended?

  30. Consider the following class:

    	public class IntCell
    	{
    		private int value;
    		public int getValue()
    		{
    			return value;
    		}
    		public String toString()
    		{
    			return "" + value;
    		}
    	}

    Assume that the following code segment appears in client code:

    	IntCell m = new IntCell();

    Which of these statements can be used in the client class?

    I. System.out.println(m.getValue());
    II. System.out.println(m.value);
    III. System.out.println(m);

  31. Consider the following classes:

    	public class Base
    	{
    		private int x;
    		public void setX(int n)
    		{
    			x = n;
    		}
    		// other methods not shown
    	}
    	public class Derived extends Base
    	{
    		private int y;
    		public void setY(int n)
    		{
    			y = n;
    		}
    		// other methods not shown
    	}

    Assume that the following statement appears in a client program:

    	Base item = new Derived();

    Which of the following statements will compile?

    I. item.y = 16;
    II. item.setY(16);
    III. item.setX(25);

  32. Assume that obj1 and obj2 are object references. Which of the following best describes when the expression obj1 == obj2 is true?

  33. A bear is an animal and a zoo contains many animals, including bears. Three classes, Animal, Bear, and Zoo are declared to represent animal, bear, and zoo objects. Which of the following is the most appropriate set of class declarations to represent these relationships?

  34. Consider the following abstract class:

    	public abstract class Ticket
    	{
    		private String eventName;
    		public Ticket(String event)
    		{
    			eventName = event;
    		}
    		public abstract double getPrice();
    		//other methods not shown
    	}

    Which of the following class headers would create an AdvanceTicket class that inherits from the Ticket class?

  35. Assume that the AdvanceTicket class inherits from the Ticket class, and the following constructor has been created and properly implemented:

    	public AdvanceTicket(String event, int daysInAdvance)

    Which of the following will compile without error?

    I. Ticket t1 = new Ticket("Frozen on Ice");
    II. AdvanceTicket t1 = new AdvanceTicket("Frozen on Ice", 15);
    III. Ticket t1 = new AdvanceTicket("Frozen on Ice", 15);

  36. If the AdvanceTicket class inherits from the Ticket class, which of the following statements are true?

    I. AdvanceTicket must implement the getPrice method or be declared abstract.
    II. AdvanceTicket must create a constructor that calls the Ticket constructor using the super constructor call.
    III. AdvanceTicket can override any of the inherited Ticket methods.

  37. Consider the following declarations:

    	public class Something
    	{
    		public String name()
    		{ /* code not shown */ }
    		public int value()
    		{ /* code not shown */ }
    	}
    	Something[] list = new Something[10];

    Assume that the elements of the array list have been initialized. Which of the following gives a reference to the name of the second item in list?

  38. Consider the following partial class declarations:

    	public abstract class RentalProperty
    	{
    		public double monthlyPayment()
    		{ /* implementation not shown */ }
    	}
    	public class HouseRental extends RentalProperty
    	{
    		public double monthlyPayment()
    		{ /* implementation*/ }
    	}

    The following code appears in a method in another class:

    	RentalProperty property = new HouseRental();
    	System.out.println(property.monthlyPayment());

    Which of the following is true?

  39. Consider the following partial class declaration:

    	public class Student
    	{
    		private String myName;
    		private double myGPA;
    		public String getName()
    		{ return myName; }
    		public double getGPA()
    		{ return myGPA; }
    		// returns the name and GPA in string format
    		public String toString()
    		{
    			return StringExpression;
    		}
    		// constructors and other methods not shown
    	}

    Consider the following replacements for StringExpression:

    I. myName + " " + myGPA;
    II. Student.name() + " " + Student.GPA();
    III. getName() + " " + getGPA();

    Which of these replacements will make the method toString work as intended?

  40. Consider the following classes:

    	public interface Item
    	{
    		double purchasePrice();
    	}
    	public abstract class TaxableItem implements Item
    	{
    		private double rate;
    		public TaxableItem(double r)
    		{
    			rate = r;
    		}
    		public double purchasePrice()
    		{ return (1 + rate) * getListPrice(); }
    		public abstract double getListPrice();
    		// Other methods not shown
    	}

    Which of the following class headers would create a Vehicle class that inherits from the TaxableItem class?

  41. Assume that the Vehicle class inherits from the TaxableItem class, and the following constructor has been created and properly implemented:

    public Vehicle(double dealerCost, double markup, double rate)

    Which of the following will compile without error?

    I. Item car = new Vehicle(20000.0, 2500.0, .05);
    II. Vehicle car = new Vehicle(20000.0, 2500.0, .05);
    III. TaxableItem car = new Vehicle(20000.0, 2500.0, .05);

  42. If the Vehicle class inherits from the TaxableItem class, which of the following statements are true?

    I. Vehicle must implement the getListPrice method or be declared abstract.
    II. Vehicle must have a constructor that calls the TaxableItem constructor using the super constructor call.
    III. Vehicle can override any of the inherited TaxableItem methods.

  43. Consider the following classes:

    public interface Lockable {
    		boolean isLocked();
    		void setKey(int key);
    		void lock(int key);
    		void unlock(int key);
    	}
    	public class Account implements Lockable {
    		private double balance;
    		private int key;
    		private boolean locked;
    		public Account(double initAmount, int initKey)
    			{ /* implementation not shown */}
    		public void deposit(double money)
    			{ /* implementation not shown */}
    		// Other methods not shown
    	}

    Consider the following code:

    Lockable acct = new Account(1000.0, 1234);

    Which of the following statements will compile?

    I. acct.setKey(12);
    II. acct.lock(12);
    III. acct.deposit(125.25);

  44. Assume that the variable numbers is an ArrayList<Integer>. Consider the following code segment:

    	boolean flag = false;
    	for (Integer val : numbers)
    	{
    		flag = flag || (val.intValue() > 0);
    	}

    Which of the following best describes what this code segment does?

  45. Consider the following two data structures for storing several million words:

    I. An array of words, not in any particular order
    II. An array of words, sorted in alphabetical order – any operation on this structure must maintain alphabetical order

    Which of the following statements most accurately describes the time needed for operations on these data structures?

  46. Consider the following method:

    public static int mystery(int a, int b)
    	{
    		if (a < b)
    			return a;
    		else
    			return b + mystery(a - 1, b + 1);
    	}

    What value does mystery(4, 2) return?

  47. Consider the following method:

    	public static int mystery(int a, int b)
    	{
    		if (a % b == 0)
    			return b;
    		else
    			return mystery(b, a % b);
    	}

    What value does mystery(111, 74) return?

  48. What does the following recursive method compute, if only positive parameters are passed to the method?

    	public int guess(int a, int b)
    	{
    		if (a == 0)
    			return 1;
    		else
    			return b * guess(a - 1, b);
    	}
  49. Consider the following method:

    	public static boolean guess(List numbers, int index)
    	{
    		 if(index < 0)
    				return false;
    		 return numbers.get(index).intValue() > 0 || guess(numbers, index – 1) ;
    	}

    If nums is a List<Integer> and contains at least one integer value, which of the following best describes what the call to guess(nums, nums.size() – 1) will return?

  50. Consider the following method:

    	public static int mystery(int num)
    	{
    		 if (num > 0)
    		 {
    				if (num / 10 % 2 == 0)
    					 return num;
    				return mystery(num / 10);
    		 }
    		 return num;
    	}

    What value is returned as a result of the call to mystery(1034)?

  51. Consider the following recursive method:

    public static void mystery(String str) {
    		int len = str.length();
    		if (len > 1)
    		{
    			String temp = str.substring(0, len – 1);
    			mystery(temp);
    			System.out.println(temp);
    		}}

    What does mystery("WATCH") output?

  52. Consider the following recursive method (Extra Credit):

    	public static int huh(int n)
    	{
    		if (n <= 10)
    			return n * 2;
    		return huh(huh(n / 3));
    	}

    What does huh(27) return?

  53. Consider the following method:

    	public static int mystery(int[] arr, int low, int high, int target)
    	{
    		int mid = (low + high) / 2;
    		if (low > high)
    			return low;
    		if (target > arr[mid])
    			return mystery(arr, mid + 1, high, target);
    		if (target < arr[mid])
    			return mystery(arr, low, mid - 1, target);
    		return mid;
    	}

    How many calls to mystery (including the initial call) are made as a result of the call mystery(arr, 0, arr.length - 1, 14) if arr is the following array: [11, 13, 25, 26, 29, 30, 31, 32]?

  54. Consider the following method:

    	public static int mystery(int num)
    	{
    		 if (num > 0)
    		 {
    				if (num / 10 % 2 == 0)
    					 return num;
    				return mystery(num / 10);
    		 }
    		 return num;
    	}

    What value is returned as a result of the call to mystery(1034)?