AP AP Computer Science & Technology 2 — Questions and Answers
Question 1: In AP Computer Science, what is the purpose of a constructor in object-oriented programming?
- To destroy an object
- To initialize a new object's state (Correct answer)
- To sort data
- To compare two objects
Correct answer: To initialize a new object's state
A constructor is a special method called when an object is created, used to initialize its attributes.
Question 2: Which sorting algorithm repeatedly swaps adjacent elements that are in the wrong order?
- Merge Sort
- Quick Sort
- Bubble Sort (Correct answer)
- Insertion Sort
Correct answer: Bubble Sort
Bubble sort compares and swaps adjacent elements repeatedly, causing larger values to 'bubble' to the end.
Question 3: What does the term 'inheritance' mean in object-oriented programming?
- A class receives properties and methods from a parent class (Correct answer)
- A class is deleted after use
- A variable stores multiple values
- An object is copied exactly
Correct answer: A class receives properties and methods from a parent class
Inheritance allows a subclass to acquire the attributes and methods of a superclass, promoting code reuse.
Question 4: In a binary number system, what is the decimal equivalent of 1010?
- 8
- 10 (Correct answer)
- 12
- 14
Correct answer: 10
Binary 1010 equals 1×8 + 0×4 + 1×2 + 0×1 = 10 in decimal.
Question 5: What is the primary purpose of an array in programming?
- To store a single value
- To store multiple values of the same type under one name (Correct answer)
- To create loops
- To define functions
Correct answer: To store multiple values of the same type under one name
An array stores a collection of elements of the same data type, accessible via index positions.
Question 6: Which of the following best describes the internet protocol IP?
- A programming language
- A set of rules for routing data packets across networks (Correct answer)
- A type of computer hardware
- A method for encrypting passwords
Correct answer: A set of rules for routing data packets across networks
IP is a protocol that defines how data packets are addressed and routed between devices on a network.
In AP Computer Science, what is the purpose of a constructor in object-oriented programming?