AP CSA Practice Test: Constructors and Methods
AP CSA Practice Test: Constructors and Methods
Unit 3: Class Creation | 10 Questions | AP Computer Science A
public class Box {
private int w, h;
public Box() {
this(1, 1);
}
public Box(int s) {
this(s, s);
}
public Box(int w, int h) {
this.w = w;
this.h = h;
}
public int area() {
return w * h;
}
}
// In main:
Box b = new Box(5);
System.out.println(b.area());
Explanation: new Box(5) calls the one-parameter constructor, which calls this(5, 5). So w=5, h=5. area() returns 5*5 = 25.
Common Mistake: Not following the constructor chaining. this(s, s) calls the two-parameter constructor.
private double price?public double setPrice(double p)
public void setPrice(double p)
private void setPrice(double p)
public double getPrice()
Explanation: A mutator (setter) should be public, return void, and accept a parameter matching the variable's type. D is an accessor. A has a return type (unusual for a setter). C is private (not accessible externally).
Common Mistake: Choosing the one with a return type. Standard mutators are void.
public static int mystery(int a, int b) {
a = a + b;
b = a - b;
a = a - b;
return a;
}
// In main:
int x = 3, y = 7;
int result = mystery(x, y);
System.out.println(x + " " + y + " " + result);
Explanation: Java passes primitives by value. Inside mystery: a becomes 7, b becomes 3, a becomes 3. Returns 3... wait, let me trace again. a=3+7=10. b=10-7=3. a=10-3=7. Returns 7. But x and y are unchanged (pass by value). Output: 3 7 7.
Common Mistake: Thinking x and y change. Primitives are passed by value; the originals are never modified.
Explanation: Overloaded methods must have the SAME name but different parameter lists (different number or types of parameters). Having different names would just be different methods, not overloading.
Common Mistake: Confusing overloading (same name, different params) with overriding (same name, same params, different class).
public class Num {
private int val;
public Num(int val) {
this.val = val;
}
public Num add(Num other) {
return new Num(this.val + other.val);
}
public int getVal() {
return val;
}
}
// In main:
Num a = new Num(3);
Num b = new Num(4);
Num c = a.add(b).add(a);
System.out.println(c.getVal());
Explanation: a.add(b) creates a new Num(3+4) = Num(7). Then .add(a) creates Num(7+3) = Num(10). Method chaining works because add() returns a Num.
Common Mistake: Not following the chain. Each add() returns a NEW Num that the next add() is called on.
public class Item {
private String name;
public Item(String name) {
this.name = name;
}
public static void rename(Item it) {
it = new Item("Changed");
}
}
// In main:
Item obj = new Item("Original");
Item.rename(obj);
System.out.println(obj.name);
Explanation: Compile error: obj.name is accessed outside the class but name is private. Even if it were public, the rename method reassigns the local reference it, not the original obj.
Common Mistake: Overlooking the private access modifier. name is private and cannot be accessed directly outside the class.
public class Calc {
public static int add(int a, int b) {
return a + b;
}
public static double add(double a, double b) {
return a + b;
}
}
// In main:
System.out.println(Calc.add(3, 4));
System.out.println(Calc.add(3.0, 4.0));
Explanation: Java selects the method based on argument types. add(3, 4) matches add(int, int) returning 7. add(3.0, 4.0) matches add(double, double) returning 7.0.
Common Mistake: Thinking Java can't distinguish between int and double parameters.
age from being set to a negative value?public void setAge(int a) { age = a; }
public void setAge(int a) { if (a >= 0) age = a; }
public int setAge(int a) { age = Math.abs(a); return age; }
private void setAge(int a) { if (a >= 0) age = a; }
Explanation: B validates the input before setting it. A has no validation. C uses Math.abs which converts negatives silently (may not be desired). D is private so callers cannot use it.
Common Mistake: Using Math.abs to 'fix' bad input. That changes the meaning of the input rather than rejecting it.
temp?public void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
Explanation: temp is a local variable declared inside the swap method. Its scope is limited to that method only. It is created when swap runs and destroyed when swap returns.
Common Mistake: Thinking local variables persist after the method returns.
public class Chain {
private int val;
public Chain(int v) {
val = v;
}
public Chain doubleIt() {
val *= 2;
return this;
}
public int get() {
return val;
}
}
// In main:
Chain c = new Chain(3);
System.out.println(c.doubleIt().doubleIt().get());
Explanation: Method chaining using return this. Start: val=3. First doubleIt(): val=6, returns same object. Second doubleIt(): val=12, returns same object. get() returns 12.
Common Mistake: Thinking each doubleIt() creates a new object. return this returns the SAME object.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]