AlgoMaster Logo

Object-Oriented Programming - Quiz

Last Updated: January 13, 2026

Object-Oriented Programming Exercises

21 quizzes

1
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:

2
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:

3
Multiple Choice

Which statement best describes a Java object?

4
Multiple Choice

What happens if you define no constructor in a class?

5
Multiple Choice

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

6
Multiple Choice

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

7
Multiple Choice

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

8
Multiple Choice

What is the effect of declaring a method as final?

9
Multiple Choice

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

10
Multiple Choice

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

11
Multiple Choice

Which statement about instance initializer blocks is true?

12
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.

13
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}
14
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}
15
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}
16
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}
17
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}
18
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.

19
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.

20
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
}
21
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
}