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.

Cosmic88·48 flashcards·48 questions
APcomputer_scienceprogramming
0
Known
1 / 48
0
Learning
Front

Define a method to calculate the factorial.

Tap to flip
Back

public int factorial(int n) { return (n <= 1) ? 1 : n * factorial(n - 1); }

Tap to flip
Got it
Still learning

Quiz(48 questions)

Question 1 of 48

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?

A.It restricts access to the internal state of an object.
B.It allows for multiple inheritance.
C.It simplifies method definitions.
D.It enables faster execution of code.

2. What is the time complexity of a linear search algorithm in an array of n elements?

A.O(n)
B.O(log n)
C.O(n^2)
D.O(1)

3. Which data structure allows for dynamic resizing as elements are added or removed?

A.ArrayList
B.Array
C.HashMap
D.LinkedList

4. What is the output of the following code? public int factorial(int n) { return (n <= 1) ? 1 : n * factorial(n - 1); } factorial(5);

A.120
B.24
C.60
D.100

5. Which of the following statements is true regarding inheritance?

A.It creates a new class that cannot share behavior from the parent class.
B.It allows a subclass to override the parent class methods.
C.It requires a subclass to implement all methods from the parent class.
D.It introduces redundancy in code.

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.

A.True
B.False
C.Only for small inputs
D.Only for large inputs

7. True or False: A Set data structure allows duplicate elements.

A.True
B.False
C.Maybe
D.Depends on implementation

8. True or False: A method can return multiple values directly.

A.True
B.False
C.Maybe
D.Depends on the method

9. Which term describes a class that cannot be instantiated and may contain abstract methods?

A.Interface
B.Concrete Class
C.Abstract Class
D.Subclass

10. Which of the following scenarios best illustrates the impact of increasing the size of input on an O(n^2) algorithm?

A.Doubling the input size quadruples the run time.
B.The run time remains constant as input increases.
C.The run time decreases as input increases.
D.Doubling the input size doubles the run time.

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?

A.ArrayList
B.HashMap
C.Set
D.Stack

12. Fill in the blank: A ____ is a reusable piece of code that performs a specific function.

A.class
B.method
C.variable
D.loop

13. What is the purpose of a constructor in a class?

A.To define the properties of the class.
B.To initialize the object's state upon creation.
C.To execute the main method of a program.
D.To provide a blueprint for class inheritance.

14. What is the purpose of using Big O notation in algorithm analysis?

A.To provide an exact run time
B.To express the efficiency of an algorithm
C.To determine memory requirements
D.To count the number of loops

15. Which of the following is NOT a characteristic of a Map data structure?

A.Stores data in key-value pairs
B.Allows for duplicate keys
C.Enables quick lookups by key
D.Can dynamically resize

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); }

A.true
B.false
C.null
D.0

17. Which of the following is NOT a characteristic of interfaces in Java?

A.They can contain default methods.
B.They can hold state.
C.They can extend other interfaces.
D.They define method signatures.

18. Which of the following is NOT a common method for analyzing algorithm efficiency?

A.Best-case analysis
B.Worst-case analysis
C.Average-case analysis
D.Random-case analysis

19. What is the average time complexity for retrieving an element from a HashMap?

A.O(1)
B.O(n)
C.O(log n)
D.O(n^2)

20. What does the 'this' keyword refer to in a class method?

A.The parent class
B.The current object instance
C.Static variables
D.Method parameters

21. What does it mean to say a method is polymorphic?

A.It can be called with different data types.
B.It can be overridden in subclasses.
C.It can be used without parameters.
D.It can only exist in abstract classes.

22. If an algorithm has a space complexity of O(1), what does this imply?

A.It uses an amount of memory that grows with input size.
B.It uses a constant amount of memory regardless of input size.
C.It requires no memory at all.
D.It uses memory that scales logarithmically with input size.

23. When is it more advantageous to use a LinkedList instead of an Array?

A.When memory usage is critical
B.When frequent insertions and deletions are required
C.When data needs to be accessed randomly
D.When the size is fixed

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; }

A.To find the minimum in an array
B.To sort an array
C.To find the maximum in an array
D.To count elements in an array

25. What is method overriding?

A.Providing a new implementation for a method inherited from a superclass.
B.Creating a method with the same name but different parameters.
C.Defining a method in an interface.
D.Calling a superclass method from a subclass.

26. In recursive algorithms, what is the role of the base case?

A.To provide a case for recursion to continue
B.To stop the recursion and prevent infinite calls
C.To increase efficiency by reducing calls
D.To track the maximum depth of recursion

27. Cause → Effect: Using a Stack for tracking function calls. What is the effect?

A.Reverses the order of function calls
B.Increases memory usage
C.Allows for quick access to the top element
D.Improves algorithm efficiency

28. True or False: A constructor must always have a return type.

A.True
B.False
C.Depends on the language
D.Only if it's overloaded

29. Which of the following best defines the relationship of aggregation?

A.A strict ownership relationship between classes.
B.A 'has-a' relationship where contained objects can exist independently.
C.A complete dependency where one class cannot function without the other.
D.A relationship that allows for multiple inheritance.

30. Which of the following describes tail recursion?

A.The recursion is at the beginning of the function.
B.The recursive call is the last operation in the function.
C.It involves multiple recursive calls before returning an answer.
D.It cannot be optimized by the compiler.

31. Which data structure is best for implementing a queue?

A.Stack
B.ArrayList
C.LinkedList
D.HashMap

32. Which best describes the difference between static and instance methods?

A.Static methods can be overridden; instance methods cannot
B.Static methods belong to the class; instance methods belong to objects
C.Both can be called without an instance
D.Static methods cannot access instance variables

33. True or False: A class can implement more than one interface.

A.True
B.False
C.Only if they are abstract classes.
D.Only if the interfaces are compatible.

34. When comparing iterative and recursive approaches, which statement is true?

A.Recursive approaches are always faster than iterative.
B.Iterative approaches often use more memory than recursive.
C.Recursive methods can be more intuitive but may use more stack space.
D.Iterative methods cannot solve problems that recursive methods can.

35. What will the countVowels method return for the input 'Hello World'?

A.3
B.2
C.5
D.4

36. What is the main purpose of access modifiers in a class?

A.To control the execution of the program.
B.To dictate the memory layout of objects.
C.To control visibility and access to class members.
D.To define the type of the class.

37. Consider the algorithm for calculating the Fibonacci sequence. What is the time complexity of a naive recursive implementation?

A.O(n)
B.O(log n)
C.O(2^n)
D.O(n^2)

38. How do method overloading and overriding differ?

A.Overloading is in the same class; overriding is in a subclass
B.Overloading requires the same return type; overriding does not
C.Both are the same in practice
D.Overloading changes the method name

39. Which design principle emphasizes reducing code duplication through shared behavior?

A.Encapsulation
B.Inheritance
C.Abstraction
D.Composition

40. What is a loop invariant?

A.A condition that must be true only at the end of the loop.
B.A condition that must be true before and after each iteration of the loop.
C.A condition that guarantees the loop will run forever.
D.A statement that shows the maximum number of iterations.

41. Which of the following is an example of a simple class with a method?

A.class Cat { void meow() {} }
B.public void Dog() {}
C.class Vehicle { int wheels; }
D.class Person { String name; }

42. In a composition relationship, how would you best describe the connection between objects?

A.A 'has-a' relationship where the contained object's lifecycle is dependent on the container.
B.A 'has-a' relationship where the contained object can exist independently.
C.A 'is-a' relationship defining a subclass.
D.A 'uses-a' relationship indicating a temporary association.

43. What is the main reason for analyzing the space complexity of an algorithm?

A.To determine how fast the algorithm runs
B.To evaluate how much memory is used by the algorithm
C.To compare two different algorithms
D.To understand the algorithm's logic structure

44. What does the keyword 'void' indicate in a method definition?

A.The method returns a value
B.The method does not return any value
C.The method is a constructor
D.The method can throw exceptions

45. What is the result of the reverseArray method when applied to an array {1, 2, 3, 4, 5}?

A.{5, 4, 3, 2, 1}
B.{1, 2, 3, 4, 5}
C.{1, 5, 4, 3, 2}
D.null

46. True or False: A method can be invoked before its definition in the same class.

A.True
B.False
C.Only for static methods
D.Only for private methods

47. What does the following method do? public int sum(int a, int b) { return a + b; }

A.Subtracts two integers
B.Multiplies two integers
C.Adds two integers
D.Divides two integers

48. Why are method parameters important?

A.They define the method's return type
B.They allow input to the method
C.They determine the method's access level
D.They control the method execution order

Related Study Sets

Create Your Own Study Set

Upload a PDF, paste your notes, or describe a topic – AI generates flashcards, quizzes and more in seconds.