Writing Classes Practice Questions

AP (Advanced Placement) · AP Computer Science A · 151 free MCQs with instant results and detailed explanations.

151
Total
49
Easy
71
Medium
31
Hard

Start Practicing Writing Classes

Take a timed quiz or customize your practice session

Quick Quiz (10 Qs) → Mock Test (25 Qs) ⚙ Customize

Sample Questions from Writing Classes

Here are 10 sample questions. Start a quiz to get randomized questions with scoring.

Q1
Easy
What is the primary purpose of a constructor in a Java class?
A. To initialize object attributes when an object is created.
B. To define methods that can be called on the object.
C. To create static methods in a class.
D. To destroy an object when it is no longer needed.
Show Answer & Explanation
Correct Answer: A
A constructor's main role is to initialize the attributes of an object at the time of creation, ensuring that the object starts its life in a valid state.
Q2
Easy
Given the following class definition, what will be the output of the code `new MyClass().display();` if MyClass is defined as follows: `class MyClass { void display() { System.out.println("Hello World"); } }`?
A. Hello World
B. MyClass
C. Error: display() is not public
D. Hello
Show Answer & Explanation
Correct Answer: A
The method display() is successfully invoked, and since it prints 'Hello World', this is the expected output.
Q3
Easy
What keyword is used to create a subclass in Java?
A. extends
B. inherits
C. super
D. implements
Show Answer & Explanation
Correct Answer: A
In Java, the 'extends' keyword is used to denote that a class is inheriting from a superclass, thereby forming a subclass relationship.
Q4
Medium
Which of the following best defines encapsulation in object-oriented programming?
A. Hiding the internal state and requiring all interaction to be performed through an object's methods.
B. The ability of different classes to be treated as instances of the same class through inheritance.
C. The process of creating multiple instances of a class from a single blueprint.
D. The practice of combining data and methods into a single unit but allowing direct access to the data.
Show Answer & Explanation
Correct Answer: A
Encapsulation is a fundamental principle of object-oriented programming that restricts direct access to some of the object's components, which makes the concept of data hiding possible. This helps in maintaining the integrity of the data and allows modification of the internal state only via methods.
Q5
Medium
What will be the output of the following code snippet? class Counter { private int count = 0; public void increment() { count++; } public int getCount() { return count; } } public class Main { public static void main(String[] args) { Counter c = new Counter(); c.increment(); c.increment(); System.out.println(c.getCount()); } }
A. 0
B. 1
C. 2
D. Compilation error
Show Answer & Explanation
Correct Answer: C
The code defines a class 'Counter' with a method to increment a private integer 'count'. By calling 'increment()' twice in the main method, the value of 'count' becomes 2, which is printed by 'getCount()'.
Q6
Medium
Consider the following class definition: class Rectangle { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } public double area() { return length * width; } } What is the purpose of the constructor in this class?
A. To define the area of the rectangle.
B. To initialize the object's length and width upon creation.
C. To provide a default value for the rectangle's dimensions.
D. To create a copy of another Rectangle object.
Show Answer & Explanation
Correct Answer: B
The constructor of the Rectangle class takes parameters for length and width to initialize the instance variables when a new object of Rectangle is created. This is essential for setting the properties of the object correctly.
Q7
Medium
If you have a class named 'Person' with a method 'getAge()', which returns an integer, what will happen if you try to call 'getAge()' on a null reference of a Person object?
A. It will return 0.
B. It will throw a NullPointerException.
C. It will compile but return null.
D. It will throw a compilation error.
Show Answer & Explanation
Correct Answer: B
Calling a method on a null reference leads to a NullPointerException, as there's no actual object to invoke the method on. This is a common error in Java when dealing with references.
Q8
Hard
Consider a class called 'Circle' that has a method 'getArea()' which computes the area of the circle using the formula ฯ€rยฒ. If the radius is stored as a private field 'radius', which of the following implementations correctly returns the area of the circle?
A. public double getArea() { return Math.PI * radius * radius; }
B. public double getArea() { return 2 * Math.PI * radius; }
C. public double getArea() { return radius * radius; }
D. public double getArea() { return Math.PI * radius; }
Show Answer & Explanation
Correct Answer: A
Option A correctly implements the formula for the area of a circle by using ฯ€rยฒ, where radius is the private field. Other options do not represent the area correctly.
Q9
Hard
If you have a class 'Rectangle' with width and height as private attributes and a constructor that initializes these attributes, which of the following code snippets correctly creates a method to double the dimensions of the rectangle?
A. public void doubleDimensions() { width = width * 2; height = height * 2; }
B. public void doubleDimensions() { this.width *= 2; this.height *= 2; }
C. public void doubleDimensions() { width += width; height += height; }
D. public void doubleDimensions() { width = width + height; height = height + width; }
Show Answer & Explanation
Correct Answer: B
Option B correctly doubles the width and height using compound assignment. Option A is also correct, but B is more concise in using 'this'. Other options do not achieve the intended task.
Q10
Hard
Given the following class definition, what will be the output of the main method if the method 'display()' is called on an instance of class 'Example'? class Example { private int value; public Example(int value) { this.value = value; } public void display() { System.out.println(value + 5); } }
A. 5
B. 10
C. 15
D. Error: value not initialized
Show Answer & Explanation
Correct Answer: C
The output will be 15 because the display method adds 5 to the initialized value. If 'Example' is instantiated with 'new Example(10)', then '10 + 5' equals 15.

Showing 10 of 151 questions. Start a quiz to practice all questions with scoring and timer.

Practice All 151 Questions →

Writing Classes โ€” AP (Advanced Placement) AP Computer Science A Practice Questions Online

This page contains 151 practice MCQs for the chapter Writing Classes in AP (Advanced Placement) AP Computer Science A. The questions are organized by difficulty โ€” 49 easy, 71 medium, 31 hard โ€” so you can choose the right level for your preparation.

Every question includes a detailed explanation to help you understand the concept, not just memorize answers. Take a timed quiz to simulate exam conditions, or practice at your own pace with no time limit.