Using Objects Practice Questions

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

150
Total
46
Easy
76
Medium
28
Hard

Start Practicing Using Objects

Take a timed quiz or customize your practice session

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

Sample Questions from Using Objects

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

Q1
Easy
What is the purpose of a constructor in a class?
A. To initialize objects of the class.
B. To define methods of the class.
C. To create static variables.
D. To compile the class.
Show Answer & Explanation
Correct Answer: A
A constructor is a special method used to initialize objects of a class. It sets initial values for the object's attributes.
Q2
Easy
Which of the following statements correctly creates an object of the class 'Car'?
A. Car myCar = new Car();
B. Car myCar();
C. myCar = Car();
D. new Car myCar = Car();
Show Answer & Explanation
Correct Answer: A
The syntax 'Car myCar = new Car();' correctly creates a new instance of the Car class by calling its constructor.
Q3
Easy
If a class 'Dog' has a method 'bark()', which prints 'Woof!', how can you call this method on an object 'myDog'?
A. myDog.bark();
B. bark(myDog);
C. Dog.bark(myDog);
D. myDog->bark();
Show Answer & Explanation
Correct Answer: A
The correct way to call a method on an object in Java is using the dot notation, as seen in 'myDog.bark();'.
Q4
Medium
What will be the output of the following code snippet? ```java public class Test { public static void main(String[] args) { String s = "Hello"; String t = s; t += " World!"; System.out.println(s); } } ```
A. Hello
B. Hello World!
C. Compilation Error
D. Null
Show Answer & Explanation
Correct Answer: A
The variable 's' remains unchanged because strings in Java are immutable. The operation on 't' does not affect 's'.
Q5
Medium
Consider the following classes: ```java class Animal { public void sound() { System.out.println("Animal sound"); } } class Dog extends Animal { public void sound() { System.out.println("Bark"); } } ``` What will happen if you run the following code? ```java Animal myDog = new Dog(); myDog.sound(); ```
A. Animal sound
B. Bark
C. Compilation Error
D. Runtime Error
Show Answer & Explanation
Correct Answer: B
The 'sound()' method in the Dog class overrides the method in the Animal class. Thus, the output will be 'Bark'.
Q6
Medium
Given the following class definition: ```java class Rectangle { private int length; private int width; public Rectangle(int l, int w) { length = l; width = w; } public int area() { return length * width; } } ``` What will be the area of a Rectangle created with `new Rectangle(5, 10)`?
A. 15
B. 50
C. 5
D. 10
Show Answer & Explanation
Correct Answer: B
The area of a rectangle is calculated by multiplying its length and width. Here, 5 * 10 equals 50.
Q7
Medium
What is the result of the following code snippet? ```java public class Test { public static void main(String[] args) { Integer a = 10; Integer b = 10; if (a == b) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } } } ```
A. Equal
B. Not Equal
C. Compilation Error
D. Runtime Error
Show Answer & Explanation
Correct Answer: A
In Java, Integer values between -128 and 127 are cached and reused, so 'a' and 'b' refer to the same object, making them equal.
Q8
Hard
Given the following class definition: class Rectangle { private int width; private int height; public Rectangle(int w, int h) { width = w; height = h; } public int area() { return width * height; } } What will be the output of the following code snippet? Rectangle rect = new Rectangle(5, 10); System.out.println(rect.area());
A. 50
B. 15
C. 5
D. 10
Show Answer & Explanation
Correct Answer: A
The area of a rectangle is calculated using the formula width * height. In this case, the width is 5 and the height is 10, so the area will be 5 * 10 = 50.
Q9
Hard
Consider the following class hierarchy: class Animal { String sound(); } class Dog extends Animal { String sound() { return "Bark"; } } class Cat extends Animal { String sound() { return "Meow"; } } If you have a reference of type Animal pointing to an object of Dog, which of the following statements correctly invokes the sound method to produce output?
A. Animal animal = new Dog(); System.out.println(animal.sound());
B. Dog dog = new Animal(); System.out.println(dog.sound());
C. Animal animal = new Dog(); System.out.println(Dog.sound());
D. Dog dog = new Dog(); System.out.println(animal.sound());
Show Answer & Explanation
Correct Answer: A
In option A, the Animal reference correctly points to a Dog object, and invoking animal.sound() will call the overridden method in the Dog class, producing "Bark". The other options either have syntax errors or do not invoke the method correctly.
Q10
Hard
Consider the following Java code snippet: ```java public class MathUtils { public static int multiply(int a, int b) { return a * b; } } ``` What will be the output of the following code when executed? ```java public class Main { public static void main(String[] args) { MathUtils mu = new MathUtils(); System.out.println(mu.multiply(5, 6)); } } ```
A. 30
B. 11
C. 5
D. None of the above
Show Answer & Explanation
Correct Answer: A
The multiply method in MathUtils takes two integers (5 and 6) and returns their product, which is 30.

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

Practice All 150 Questions →

Using Objects โ€” AP (Advanced Placement) AP Computer Science A Practice Questions Online

This page contains 150 practice MCQs for the chapter Using Objects in AP (Advanced Placement) AP Computer Science A. The questions are organized by difficulty โ€” 46 easy, 76 medium, 28 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.