AP CSA Unit 3 Practice Exam: Class Creation (Part 1)
Unit 3: Class Creation
Part 1: Questions 1-25
Class Structure • Instance Variables • Constructors • The this Keyword
New AP CSA Curriculum (2025-2026): This exam covers Unit 3 (Class Creation) - designing classes, constructors, instance variables, methods, encapsulation, and static vs instance members. This is Part 1 of 2.
0 Correct |
0 Incorrect |
0/25 Answered
Class Structure & Instance Variables (Questions 1-12)
Question 1
Which access modifier should be used for instance variables to properly implement encapsulation?
Encapsulation means hiding internal data. Instance variables should be private so they can only be accessed through methods (getters/setters) that you control.
Question 2
What is the default value of count when a Dog object is created?
public class Dog {
private String name;
private int age;
private int count;
}
Instance variables get default values automatically. For int, the default is 0. For objects like String, it is null. For boolean, it is false.
Question 3
What is printed when this code runs?
public class Student {
private String name;
private double gpa;
public void printInfo() {
System.out.println(name + " " + gpa);
}
}
// In main method:
Student s = new Student();
s.printInfo();
String defaults to null, double defaults to 0.0. When concatenating null with a String, Java converts it to the text "null", so the output is: null 0.0
Question 4
Which statement correctly describes instance variables?
Instance variables belong to each individual object. If you create two Dog objects, each has its own name and age. Static variables are shared among all objects.
Question 5
What is the best practice concern with this class?
public class Circle {
public double radius;
public double getArea() {
return 3.14159 * radius * radius;
}
}
While this compiles, radius being public violates encapsulation. Anyone can set it to invalid values (like -5). Making it private with a setter allows validation.
Question 6
What is printed?
public class Counter {
private int value;
public void increment() { value++; }
public int getValue() { return value; }
}
// In main:
Counter c1 = new Counter();
Counter c2 = new Counter();
c1.increment();
c1.increment();
c2.increment();
System.out.println(c1.getValue() + " " + c2.getValue());
Each Counter has its own value. c1 is incremented twice (value=2), c2 once (value=1). They are independent objects.
Question 7
What is the default value of isActive when an Account object is created?
public class Account {
private String owner;
private double balance;
private boolean isActive;
}
Default value for boolean instance variables is false. Remember: int to 0, double to 0.0, boolean to false, objects to null.
Question 8
Which line causes a compile error?
public class BankAccount {
private double balance;
public double getBalance() { return balance; }
}
// In a separate class:
public class Main {
public static void main(String[] args) {
BankAccount acct = new BankAccount();
double x = acct.balance; // Line A
double y = acct.getBalance(); // Line B
}
}
Line A directly accesses balance, which is private. Private members cannot be accessed outside the class. Line B uses the public getter, which is allowed.
Question 9
What does this code print?
public class Point {
private int x, y;
public void setLocation(int newX, int newY) {
x = newX;
y = newY;
}
public String toString() {
return "(" + x + ", " + y + ")";
}
}
// In main:
Point p = new Point();
p.setLocation(5, 10);
System.out.println(p);
When you print an object, Java calls its toString() method. After setLocation(5, 10), x=5 and y=10, so it returns "(5, 10)".
Question 10
What is the scope of the instance variable name?
public class Person {
private String name;
public void greet() {
String greeting = "Hello, " + name;
System.out.println(greeting);
}
}
Instance variables have class scope - accessible by any method in the class. Local variables (like greeting) only exist within their method.
Question 11
What happens when you compile this code?
public class Rectangle {
private int width, height;
public int getArea() {
int result;
result = width * height;
return result;
}
}
This compiles fine. result is assigned before use. Instance variables (width, height) get default values automatically. Local variables must be assigned before reading.
Question 12
Which represents a "has-a" relationship (composition)?
// Option 1:
public class Car {
private Engine engine;
}
// Option 2:
public class Car extends Engine { }
Option 1 shows composition ("has-a"): a Car has an Engine as an instance variable. Option 2 shows inheritance ("is-a"), which is illogical here - a Car is not a type of Engine.
Constructors (Questions 13-25)
Question 13
What is printed?
public class Book {
private String title;
private int pages;
public Book(String t, int p) {
title = t;
pages = p;
}
public void print() {
System.out.println(title + ": " + pages);
}
}
// In main:
Book b = new Book("Java", 500);
b.print();
The constructor assigns parameters to instance variables: title="Java", pages=500. The print method outputs "Java: 500".
Question 14
What is the result of this code? (Look carefully at the constructor!)
public class Cat {
private String name;
public Cat(String name) {
name = name; // Bug here!
}
public String getName() { return name; }
}
// In main:
Cat c = new Cat("Whiskers");
System.out.println(c.getName());
Common bug! name = name assigns the parameter to itself. The instance variable is never set, so it stays null. Fix: use this.name = name;
Question 15
What is the result with the fixed constructor using this?
public class Cat {
private String name;
public Cat(String name) {
this.name = name; // Fixed!
}
public String getName() { return name; }
}
// In main:
Cat c = new Cat("Whiskers");
System.out.println(c.getName());
Using this.name refers to the instance variable, while name alone refers to the parameter. Now the instance variable is correctly set to "Whiskers".
Question 16
What happens when this code runs?
public class Employee {
private String name;
private double salary;
public Employee(String n, double s) {
name = n;
salary = s;
}
}
// In main:
Employee e = new Employee();
When you write ANY constructor, Java no longer provides the default no-argument constructor. Since only a 2-parameter constructor exists, new Employee() causes a compile error.
Question 17
What is printed? (Constructor overloading)
public class Product {
private String name;
private double price;
public Product() {
name = "Unknown";
price = 0.0;
}
public Product(String n, double p) {
name = n;
price = p;
}
public void print() {
System.out.println(name + ": $" + price);
}
}
// In main:
Product p1 = new Product();
Product p2 = new Product("Widget", 9.99);
p1.print();
p2.print();
Constructor overloading means multiple constructors with different parameter lists. p1 uses the no-arg constructor (name="Unknown", price=0.0); p2 uses the parameterized one (name="Widget", price=9.99).
Question 18
Which statement about constructors is FALSE?
Constructors do NOT have a return type - not even void. If you write public void ClassName(), that is a regular method, not a constructor!
Question 19
What is printed? (Constructor chaining with this())
public class Timer {
private int seconds;
public Timer() {
this(0);
}
public Timer(int s) {
seconds = s;
}
public int getSeconds() { return seconds; }
}
// In main:
Timer t = new Timer();
System.out.println(t.getSeconds());
this(0) calls another constructor in the same class. The no-arg constructor calls the one-arg with 0. seconds=0. This is called constructor chaining.
Question 20
What is wrong with this constructor?
public class Game {
private int score;
public Game(int s) {
System.out.println("Starting");
this(0); // Problem here
score = s;
}
public Game() { score = 0; }
}
When calling another constructor with this(), it MUST be the very first statement. The println before this(0) causes a compile error.
Question 21
What does this refer to in a method?
public class Car {
private String model;
public Car(String model) {
this.model = model;
}
public void compare(Car other) {
if (this == other) {
System.out.println("Same car!");
}
}
}
this is a reference to the current object - the instance on which the method was invoked. In myCar.compare(other), this refers to myCar.
Question 22
What is printed?
public class Box {
private int size;
public Box() {
this(10);
size = size + 5;
}
public Box(int s) { size = s; }
public int getSize() { return size; }
}
// In main:
Box b = new Box();
System.out.println(b.getSize());
No-arg calls this(10), setting size=10. Then size=size+5=15. Constructor chaining happens first, then remaining code executes.
Question 23
How many times is the constructor called?
public class Item {
private static int count = 0;
public Item() { count++; }
public static int getCount() { return count; }
}
// In main:
Item a = new Item();
Item b = new Item();
Item c = a;
System.out.println(Item.getCount());
new Item() is called twice (for a and b). Item c = a just copies the reference - no new object created, constructor not called. count=2.
Question 24
What is the primary purpose of a constructor?
public class Student {
private String name;
private int id;
private double gpa;
public Student(String name, int id) {
this.name = name;
this.id = id;
this.gpa = 0.0;
}
}
Constructors initialize the state of a new object. They set instance variables to starting values, from parameters or defaults like gpa=0.0.
Question 25
What is printed?
public class Temperature {
private double celsius;
public Temperature(double fahrenheit) {
celsius = (fahrenheit - 32) * 5.0 / 9.0;
}
public double getCelsius() { return celsius; }
}
// In main:
Temperature t = new Temperature(32);
System.out.println(t.getCelsius());
The constructor converts F to C: (32-32)*5/9 = 0*5/9 = 0.0. 32 degrees F equals 0 degrees C (the freezing point of water).
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.
Typically responds within 24 hours
✓
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.