AlgoMaster Logo

Object-Oriented Programming - Quiz

Last Updated: January 3, 2026

Object-Oriented Programming Exercises

31 quizzes

1
Code Completion

Access the current object's instance field inside the constructor of the Product class

java
1
this.name = productName;

Click an option to fill the blank:

2
Code Completion

Mark a utility method so it can be called without creating an instance of the Utility class

java
1
public int max(int a, int b) { return a > b ? a : b; }

Click an option to fill the blank:

3
Code Completion

Declare that the PaymentConfig reference cannot point to a different object after initialization

java
1
private PaymentConfig config = new PaymentConfig();

Click an option to fill the blank:

4
Multiple Choice

Which statement best describes a Java object?

5
Multiple Choice

What happens if you define no constructor in a class?

6
Multiple Choice

Which access modifier restricts a field so it is only visible within its own class?

7
Multiple Choice

Why are fields commonly declared private with public getters and setters?

8
Multiple Choice

Which keyword lets a static nested class access only static members of its outer class directly?

9
Multiple Choice

What is a key characteristic of a record class in Java?

10
Multiple Choice

In a sealed class hierarchy, what must each direct subclass be declared as?

11
Multiple Choice

What is the effect of declaring a method as final?

12
Multiple Choice

Which situation is a typical use case for an anonymous class?

13
Multiple Choice

Which keyword allows code in an inner class to refer unambiguously to the enclosing instance?

14
Multiple Choice

Which statement about instance initializer blocks is true?

15
Sequencing

Order the steps to define and use a simple inner member class to print a User's full name

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to create and use a sealed Shape hierarchy with two final implementations

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code using a constructor and this keyword?

1class User {
2    private String name;
3    public User(String name) {
4        this.name = name;
5    }
6    public String getName() {
7        return name;
8    }
9}
10public class Main {
11    public static void main(String[] args) {
12        User u = new User("Alex");
13        System.out.println(u.getName());
14    }
15}
18
Output Prediction

What is the output when accessing a static field shared across instances?

1class Counter {
2    static int count = 0;
3    public Counter() {
4        count++;
5    }
6}
7public class Main {
8    public static void main(String[] args) {
9        new Counter();
10        new Counter();
11        System.out.println(Counter.count);
12    }
13}
19
Output Prediction

What is the output when using an instance initializer and constructors?

1class Task {
2    private String name;
3    {
4        name = "Default";
5    }
6    public Task() {
7    }
8    public Task(String name) {
9        this.name = name;
10    }
11    public String getName() {
12        return name;
13    }
14}
15public class Main {
16    public static void main(String[] args) {
17        Task t = new Task("Email");
18        System.out.println(t.getName());
19    }
20}
20
Output Prediction

What is the output when using a record to model an immutable point?

1public record Point(int x, int y) {}
2public class Main {
3    public static void main(String[] args) {
4        Point p = new Point(3, 4);
5        System.out.println(p.x() + "," + p.y());
6    }
7}
21
Output Prediction

What is the output when using an anonymous class to override behavior?

1interface Greeter {
2    String greet();
3}
4public class Main {
5    public static void main(String[] args) {
6        Greeter g = new Greeter() {
7            public String greet() {
8                return "Hi";
9            }
10        };
11        System.out.println(g.greet());
12    }
13}
22
Bug Spotting

Find the bug related to access modifiers in this code

Click on the line(s) that contain the bug.

java
1
public class Account {
2
    public double balance;
3
    public void deposit(double amount) {
4
        balance += amount;
5
    }
6
    public double getBalance() {
7
        return balance;
8
    }
9
}
23
Bug Spotting

Find the bug involving static and instance members in this code

Click on the line(s) that contain the bug.

java
1
public class Session {
2
    private static String userName;
3
    public Session(String userName) {
4
        this.userName = userName;
5
    }
6
    public String getUserName() {
7
        return userName;
8
    }
9
}
24
Matching

Match each OOP concept with its description

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match each Java type feature with its key property

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the code so the Person class is immutable and exposes its data safely

java
1
public final class Person {
2
private final String name;
3
4
public Person(String name) {
5
this.name = name;
6
}
7
8
public () {
9
return name;
10
}
11
}

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the sealed hierarchy so only specific shapes are allowed

java
1
public sealed abstract class Shape Circle, Rectangle {
2
public abstract double area();
3
}
4
5
public final class Circle extends Shape {
6
private final double radius;
7
public Circle(double radius) {
8
this.radius = radius;
9
}
10
public double area() {
11
return Math.PI * radius * radius;
12
}
13
}
14
15
public final class Rectangle extends Shape {
16
private final double width;
17
private final double height;
18
public Rectangle(double width, double height) {
19
this.width = width;
20
this.height = height;
21
}
22
public double area() {
23
return width * height;
24
}
25
}

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the record declaration to model an immutable 2D size

java
1
public Size(int , int ) {}

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the inner class code so it can access the private field of the outer class

java
1
public class Order {
2
private int id;
3
public Order(int id) {
4
this.id = id;
5
}
6
public class {
7
public String format() {
8
return "Order-" + .id;
9
}
10
}
11
}

Click an option to fill blank 1:

30
Hotspot Selection

Click the line where the anonymous class is created

Click on the line to select.

java
1
interface ClickHandler {
2
    void onClick();
3
}
4
 
5
public class Button {
6
    public void setClickHandler(ClickHandler handler) {
7
        handler.onClick();
8
    }
9
 
10
    public static void main(String[] args) {
11
        Button button = new Button();
12
        button.setClickHandler(new ClickHandler() {
13
            public void onClick() {
14
                System.out.println("Clicked");
15
            }
16
        });
17
    }
18
}
31
Hotspot Selection

Click the line where a static initializer block is defined

Click on the line to select.

java
1
public class Config {
2
    public static final String APP_NAME;
3
    public static final int TIMEOUT;
4
 
5
    static {
6
        APP_NAME = "Demo";
7
        TIMEOUT = 30;
8
    }
9
 
10
    public static void main(String[] args) {
11
        System.out.println(APP_NAME + ":" + TIMEOUT);
12
    }
13
}