AP CSA free response question types study guide
This study guide covers the various types of free response questions (FRQs) found on the AP Computer Science A exam, including coding problems, design questions, and algorithm analysis.
Quiz(48 questions)
1. What is the primary benefit of encapsulation in object-oriented programming?
Terms in this Study Set(48)
Coding Questions(16)
Define a method to calculate the factorial.
public int factorial(int n) { return (n <= 1) ? 1 : n * factorial(n - 1); }
True or False: Methods can return multiple values.
False - A method can only return one value. Use arrays or objects for multiple values.
Fill in the blank: A ____ is a block of code that performs a specific task.
method
Write a method to check if a string is a palindrome.
public boolean isPalindrome(String str) { String rev = new StringBuilder(str).reverse().toString(); return str.equals(rev); }
What is the purpose of the 'this' keyword?
'this' refers to the current object instance, allowing access to its attributes and methods.
Create a method to find the maximum in an array.
public int findMax(int[] arr) { int max = arr[0]; for (int num : arr) { if (num > max) { max = num; } } return max; }
True or False: A constructor can have a return type.
False - Constructors do not have a return type, not even void.
What do the 'static' and 'instance' methods represent?
Static methods belong to the class; instance methods belong to an object. Static can be called without an instance.
Write a method to count vowels in a string.
public int countVowels(String str) { int count = 0; for (char c : str.toCharArray()) { if ('aeiouAEIOU'.indexOf(c) != -1) { count++; } } return count; }
Compare method overloading and overriding.
Overloading: Same method name, different parameters. Overriding: Same method name and parameters in a subclass.
Show an example of a simple class with a method.
class Dog { String name; Dog(String name) { this.name = name; } void bark() { System.out.println("Woof!"); } }
What does the keyword 'void' signify in a method?
Void signifies that the method does not return a value.
Write a method that reverses an array.
public void reverseArray(int[] arr) { int left = 0, right = arr.length - 1; while (left < right) { int temp = arr[left]; arr[left++] = arr[right]; arr[right--] = temp; } }
True or False: A method can be called before it is defined.
True - If the method is defined within the same class or is a static method.
Create a method to calculate the sum of two integers.
public int sum(int a, int b) { return a + b; }
What is the significance of method parameters?
Parameters allow methods to accept inputs, which can be used within the method to perform operations.
Design Questions(12)
What is encapsulation?
Encapsulation is the bundling of data and methods that operate on that data within a single unit, typically a class. It restricts direct access to some components.
True or False: Inheritance promotes code duplication.
False. Inheritance allows a new class to inherit methods and properties from an existing class, promoting code reuse.
Define polymorphism.
Polymorphism allows methods to do different things based on the object that it is acting upon. This can be achieved through method overriding or interfaces.
Fill in the blank: A ____ is a blueprint for creating objects.
class
Compare interfaces and abstract classes.
Interfaces: only method signatures, no implementation. Abstract classes: can have implemented methods and can hold state.
What is a constructor?
A constructor is a special method called when an object is instantiated. It initializes the object's properties.
Cause → Effect: Why use an interface?
To define a contract that implementing classes must follow, ensuring they provide specific methods.
What is method overriding?
Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.
True or False: A class can implement multiple interfaces.
True. A class in Java can implement multiple interfaces, allowing for more flexible design.
What is the purpose of access modifiers?
Access modifiers (public, private, protected) control the visibility of classes, methods, and variables, protecting data integrity.
Give an example of composition.
A Car class that contains an Engine class as a member. The Car 'has-a' Engine.
Define aggregation.
Aggregation is a 'has-a' relationship where the contained objects can exist independently of the containing object.
Algorithm Analysis(12)
What is algorithm efficiency?
Algorithm efficiency refers to the amount of computational resources (time and space) required by an algorithm to complete its task.
True or False: More loops always mean slower performance.
False. The performance depends on loop complexity and other factors, not just the number of loops.
O(n) vs O(n^2): Which is faster?
O(n) is generally faster than O(n^2) for larger input sizes because it scales linearly compared to quadratic growth.
Fill in the blank: The time complexity of a binary search is ______.
O(log n) - it divides the search interval in half each time.
Cause → Effect: Increasing recursion depth leads to what?
Increased memory usage - each recursive call adds to the call stack.
What are common methods for analyzing algorithms?
- Big O notation - Best, average, worst-case analysis - Space complexity
What does 'base case' mean in recursion?
The base case is a condition that stops the recursive calls, preventing infinite recursion.
Compare iterative and recursive approaches.
Iterative: uses loops, generally more memory efficient. Recursive: uses function calls, more intuitive for problems like tree traversals.
True or False: Every recursive algorithm can be rewritten as an iterative algorithm.
True. Any recursive algorithm can be converted to iterative form, often using a stack.
Example of algorithm efficiency: Sorting.
Consider Bubble Sort: O(n^2) vs Quick Sort: O(n log n). Quick Sort is more efficient.
What is the purpose of loop invariants?
Loop invariants help prove the correctness of loops by establishing a condition that holds before and after each iteration.
Define the term 'tail recursion'.
Tail recursion occurs when the recursive call is the last operation in the function, allowing for optimizations.
Data Structure Usage(8)
Array vs. ArrayList: Key difference?
Arrays have a fixed size, while ArrayLists can dynamically resize as elements are added or removed.
True or False: Maps store data in key-value pairs.
True - Maps use unique keys to associate with specific values, allowing for efficient data retrieval.
Fill in the blank: A list that allows duplicates is called a __________.
ArrayList - Unlike sets, ArrayLists can contain multiple occurrences of the same element.
Give an example of when to use a Map.
Use a Map when you need to associate student IDs with student names for quick lookups.
What is the time complexity of accessing an array element?
O(1) - Accessing an element by index in an array is constant time.
Linked List vs. Array: When is a linked list better?
Linked lists are better for frequent insertions/deletions as they don't require shifting elements.
Cause → Effect: Using a HashMap for lookups.
Cause: Using a HashMap for lookups. Effect: Achieves average O(1) time complexity for retrieval.
What is the primary use of a Stack?
Stacks are used for managing function calls, reversing data, and implementing backtracking algorithms.
Questions in this Study Set(48)
1. What is the primary benefit of encapsulation in object-oriented programming?
2. What is the time complexity of a linear search algorithm in an array of n elements?
3. Which data structure allows for dynamic resizing as elements are added or removed?
4. What is the output of the following code? public int factorial(int n) { return (n <= 1) ? 1 : n * factorial(n - 1); } factorial(5);
5. Which of the following statements is true regarding inheritance?
6. True or False: An algorithm with a time complexity of O(n log n) will always be faster than one with O(n^2) for all input sizes.
7. True or False: A Set data structure allows duplicate elements.
8. True or False: A method can return multiple values directly.
9. Which term describes a class that cannot be instantiated and may contain abstract methods?
10. Which of the following scenarios best illustrates the impact of increasing the size of input on an O(n^2) algorithm?
11. Given a scenario where you need to store a list of names without any specific order and allow duplicates, which data structure should you use?
12. Fill in the blank: A ____ is a reusable piece of code that performs a specific function.
13. What is the purpose of a constructor in a class?
14. What is the purpose of using Big O notation in algorithm analysis?
15. Which of the following is NOT a characteristic of a Map data structure?
16. Given the string 'racecar', what does the method isPalindrome return? public boolean isPalindrome(String str) { String rev = new StringBuilder(str).reverse().toString(); return str.equals(rev); }
17. Which of the following is NOT a characteristic of interfaces in Java?
18. Which of the following is NOT a common method for analyzing algorithm efficiency?
19. What is the average time complexity for retrieving an element from a HashMap?
20. What does the 'this' keyword refer to in a class method?
21. What does it mean to say a method is polymorphic?
22. If an algorithm has a space complexity of O(1), what does this imply?
23. When is it more advantageous to use a LinkedList instead of an Array?
24. What is the purpose of the following method? public int findMax(int[] arr) { int max = arr[0]; for (int num : arr) { if (num > max) { max = num; } } return max; }
25. What is method overriding?
26. In recursive algorithms, what is the role of the base case?
27. Cause → Effect: Using a Stack for tracking function calls. What is the effect?
28. True or False: A constructor must always have a return type.
29. Which of the following best defines the relationship of aggregation?
30. Which of the following describes tail recursion?
31. Which data structure is best for implementing a queue?
32. Which best describes the difference between static and instance methods?
33. True or False: A class can implement more than one interface.
34. When comparing iterative and recursive approaches, which statement is true?
35. What will the countVowels method return for the input 'Hello World'?
36. What is the main purpose of access modifiers in a class?
37. Consider the algorithm for calculating the Fibonacci sequence. What is the time complexity of a naive recursive implementation?
38. How do method overloading and overriding differ?
39. Which design principle emphasizes reducing code duplication through shared behavior?
40. What is a loop invariant?
41. Which of the following is an example of a simple class with a method?
42. In a composition relationship, how would you best describe the connection between objects?
43. What is the main reason for analyzing the space complexity of an algorithm?
44. What does the keyword 'void' indicate in a method definition?
45. What is the result of the reverseArray method when applied to an array {1, 2, 3, 4, 5}?
46. True or False: A method can be invoked before its definition in the same class.
47. What does the following method do? public int sum(int a, int b) { return a + b; }
48. Why are method parameters important?
Related Study Sets
Abitur Rekursion
Abitur: Abitur Klassen und Objekte
Was ist ein Algorithmus Schritt für Schritt
if und Schleifen Notizen
Wiederholung: Funktionen
Test: Binärzahlen
Listen Notizen
Schleife Alltag Beispiel Begriffe
Create Your Own Study Set
Upload a PDF, paste your notes, or describe a topic – AI generates flashcards, quizzes and more in seconds.

