Answer
Flashcards · AP® Computer Science A · Unit 3
Unit 3: Class Creation flashcards
52 cards on topics 3.1 to 3.9. Pick a topic or study the whole unit.
Study mode needs JavaScript. Every card is listed below, so you can still cover the answers and test yourself.
Choose your cards
Session complete
Worth another look:
All 52 cards
For a quick read-through or printing. Cover the right-hand side to test yourself.
3.1 Abstraction and Program Design
- What is abstraction in program design?
- Reducing complexity by focusing on the main idea and hiding irrelevant detail. A program is easier to design and change when each piece is used for what it does, not how.
- What is the difference between data abstraction and procedural abstraction?
- Data abstraction names data without exposing how it is stored, as in a class's attributes. Procedural abstraction names a process: a method can be used knowing only what it does.
- What is method decomposition?
- Breaking a large behaviour into smaller methods that each do one job. Parameters generalise a method so one method works for a range of arguments.
- When may you change a method's internals without telling its callers?
- When its signature and what it does stay the same. Changing the signature breaks every call, so that is not an internal change.
- What should a class design list before you start coding?
- All the attributes (the data) and all the behaviours (the methods) the specification needs, in natural language or a diagram. Listing only one of the two is incomplete.
- What is the difference between an instance variable and a class variable?
- An instance variable has no static and each object has its own copy. A class variable is declared static and one copy is shared by all objects of the class.
3.2 Impact of Program Design
- What is system reliability?
- A program performs its tasks as expected, under stated conditions, without failure. A wrong answer counts as a failure just as a crash does.
- How do programmers maximise reliability through testing?
- By testing with a variety of conditions: ordinary, boundary, extreme and special-case values. Several similar ordinary tests do not show a method works.
- Why can a method that works for ordinary inputs still be unreliable?
- Extreme values can expose faults. For example used * 100 / capacity overflows int for very large inputs and returns a wrong value, with no exception thrown.
- What does it mean for a program to have an unintended effect?
- It can cause harm beyond its intended use even when it does exactly what it was designed to do. This is different from a bug.
- Programs have impacts on society, the economy and culture. What should a good answer include?
- Both beneficial and harmful impacts where relevant, a named group affected, and a mechanism from the program's design. A one-sided answer with no named group is weak.
- What are the rules on reusing other people's code?
- Open source code is published and free to use under its licence terms. Code that is not open source needs the owner's permission, and often payment.
3.3 Anatomy of a Class
- What are the four parts of a class?
- The header, instance variables, constructors and methods. In this course classes are written public class Name and constructors are public.
- What does private mean, and which parts of a class are usually private?
- private restricts access to the declaring class. Instance variables are private unless the specification says otherwise, and helper methods are often private too.
- What is encapsulation?
- Hiding implementation details: outside code uses public methods and never touches private data directly.
- What is the difference between a public and a private method?
- Public methods can be called inside or outside the class. Private methods can only be called from inside the class, so they are used as helpers.
- What is the error for accessing a private variable from another class?
- A compile-time error, such as weightKg has private access in Parcel. It is not a run-time error.
- If two objects of one class exist, does changing one object's variable affect the other?
- No. Each object has its own copy of every instance variable, so marking one Parcel delivered does not change another Parcel.
3.4 Constructors
- What is the state of an object?
- Its instance variables and their values at a given time. A constructor sets the initial state.
- What are the rules for a constructor header?
- It is public, has the same name as the class and no return type, not even void. Adding a return type turns it into an ordinary method.
- What happens if no constructor is written?
- Java supplies a no-parameter default constructor and the instance variables get default values. Once you write any constructor, the default one is no longer supplied.
- What are the default values of int, double, boolean and reference variables?
- int 0, double 0.0, boolean false, and null for a reference such as a String.
- Why copy a mutable object passed to a constructor?
- If you store the parameter itself, the class's state can be changed through the original reference. Create a new object from its values instead, so the two objects are independent.
- What do the statements seat = s; and seat = new Seat(s.getRow(), s.getNumber()); do?
- seat = s; makes two names for one object, so changes show through both. The second creates a copy, so later changes to s do not reach seat.
3.5 Methods: How to Write Them
- What must a non-void method you write always do?
- Return one value of the type in its header on every path through the method (return by value). A void method returns nothing.
- What does return do inside an if or a loop?
- It exits the whole method immediately and hands control back to the caller. Code after it on the same path never runs.
- What are an accessor and a mutator?
- An accessor returns a copy of an attribute's value (non-void), such as public int getCoins(). A mutator changes attributes and is often void, such as addCoins(int amount).
- What compile errors do missing and extra returns cause?
- missing return statement when some path of a non-void method has no return; unreachable statement for code after a return; unexpected return value for return x; in a void method.
- Does changing a primitive parameter change the caller's variable?
- No, the parameter is a copy. To give a new value to the caller, return it and assign it: n = h.addTenAndReturn(n);
- For coins = 20, what does coinsWithBonus(15) return if it computes coins + coins * percent / 100?
- 23, since 20 + 20 * 15 / 100 is 20 + 300 / 100 = 23. It returns a value and does not change the coins attribute.
3.6 Methods: Passing and Returning References of an Object
- What is passed when an argument is an object reference?
- A copy of the reference. No new copy of the object is made, so the parameter and the caller's variable refer to the same object.
- If a method calls a mutator on a parameter object, does the caller see the change?
- Yes. The parameter refers to the same mutable object, so its changed state is visible through the caller's variable.
- What does b = new Box(100); do to the caller's variable when b is a parameter?
- Nothing. Assigning a new object to the parameter only moves the parameter's reference; the caller's variable still refers to the original object.
- What does a method that returns an object give back?
- The reference, not a copy. Returning a parameter gives the same object, an alias, while return new Box(a.getItems()) gives an independent copy.
- When may a method read the private data of a parameter?
- Only when the parameter has the same type as the enclosing class, such as other.items inside class Box. From a different class it is a compile error.
- Does a swap method that swaps its two parameters swap the caller's variables?
- No. It swaps only the method's own copies of the references. The caller's variables still refer to the same objects as before.
3.7 Class Variables and Methods
- What is a class variable and how is it declared?
- A variable declared with static before its type. There is one copy for the class, shared by all objects, and a public one is reached as ClassName.NAME.
- What can a class method (static) not do?
- It cannot use instance variables or call instance methods unless it is passed an instance. It also has no this reference.
- What does final mean, and how are constants declared?
- A final variable cannot be modified once set. Constants are usually public static final, such as public static final int MAX = 4; and are used as ClassName.MAX.
- How do you give each new object a unique id?
- Use a private static int nextId. In the constructor set id = nextId; and then increment nextId. The first three objects get ids 1, 2 and 3.
- Why must a shared counter not be reset in the constructor?
- Each construction would reset it to its starting value, and the count of objects would be lost. Initialise a class variable once, where it is declared.
- What does the error non-static variable cannot be referenced from a static context mean?
- A class method used an instance variable with no object. Pass an instance to the method, or make the member static if it should be shared.
3.8 Scope and Access
- What is the scope of a local variable?
- Only the block where it is declared, after its declaration. A variable in a for header is out of scope after the loop.
- Are parameters local variables, and can they be public or private?
- Yes, parameters are local to their method or constructor. Local variables and parameters cannot be declared public or private.
- What happens when a parameter has the same name as an instance variable?
- Inside that method or constructor the name refers to the parameter, which hides the instance variable. Changes to the parameter never reach the object.
- What does name = name; do in a constructor with a parameter called name?
- It assigns the parameter to itself and leaves the instance variable at its default, so a String field stays null. Use this.name = name; or different names.
- Where are instance and class variables in scope?
- Throughout the class. Whether code in other classes can use them is decided by public or private.
3.9 this Keyword
- What is this in an instance method or constructor?
- A reference to the current object: the object whose method or constructor is being run.
- What does this.name = name; do?
- It stores the parameter name in the instance variable name of the current object. It solves the hiding problem when the parameter and the variable have the same name.
- How can an object give another object a reference to itself?
- By passing this as an argument, as in t.setCaptain(this). The other object then holds a reference to the very same object.
- How do this == other and this.age > other.age differ?
- this == other is true only if both references are the same object. this.age > other.age compares the values held by two objects.
- Why can this not be used in a class method?
- A static method belongs to the class and runs with no current object. Using this there is a compile-time error, so pass the object in as a parameter.
Study Unit 3 further
- 3.1 Abstraction and Program Design Study guide: 3.1 Abstraction and Program DesignRevision notes: 3.1 Abstraction and Program DesignPractice questions: 3.1 Abstraction and Program Design
- 3.2 Impact of Program Design Study guide: 3.2 Impact of Program DesignRevision notes: 3.2 Impact of Program DesignPractice questions: 3.2 Impact of Program Design
- 3.3 Anatomy of a Class Study guide: 3.3 Anatomy of a ClassRevision notes: 3.3 Anatomy of a ClassPractice questions: 3.3 Anatomy of a Class
- 3.4 Constructors Study guide: 3.4 ConstructorsRevision notes: 3.4 ConstructorsPractice questions: 3.4 Constructors
- 3.5 Methods: How to Write Them Study guide: 3.5 Methods: How to Write ThemRevision notes: 3.5 Methods: How to Write ThemPractice questions: 3.5 Methods: How to Write Them
- 3.6 Methods: Passing and Returning References of an Object Study guide: 3.6 Methods: Passing and Returning References of an ObjectRevision notes: 3.6 Methods: Passing and Returning References of an ObjectPractice questions: 3.6 Methods: Passing and Returning References of an Object
- 3.7 Class Variables and Methods Study guide: 3.7 Class Variables and MethodsRevision notes: 3.7 Class Variables and MethodsPractice questions: 3.7 Class Variables and Methods
- 3.8 Scope and Access Study guide: 3.8 Scope and AccessRevision notes: 3.8 Scope and AccessPractice questions: 3.8 Scope and Access
- 3.9 this Keyword Study guide: 3.9 this KeywordRevision notes: 3.9 this KeywordPractice questions: 3.9 this Keyword
AP® and Advanced Placement® are trademarks registered by the College Board, which is not affiliated with, and does not endorse, this website. Marlbridge is an independent tuition provider. It is not an authorized AP course provider, an AP school or an exam centre, and these resources are not official College Board materials. Reading them or doing simulations does not meet the laboratory requirement of an AP science course.