Java Classes & Constructors Quiz

This Java classes quiz checks how objects are built and how variables refer to them. You will predict output involving constructors, this, default field values and references, and spot the mistakes that stop a class from compiling.

Easy 10 questions ~5 minutes Pass at 80% Java 17

Choose a mode

Playing as a guest: sign in to keep your XP and streak.

Sample question

Question 1 of 10

What does this print?

public class Main {
    static class Point {
        int x, y;

        Point(int x, int y) {
            x = x;
            this.y = y;
        }
    }

    public static void main(String[] args) {
        Point p = new Point(3, 4);
        System.out.println(p.x + " " + p.y);
    }
}
Show the answer

B. 0 4 Inside the constructor the parameter x hides the field, so x = x just assigns the parameter to itself. The field keeps its default 0; write this.x = x like the line for y.

What this quiz covers

  • Writing constructors and using this
  • The default constructor and when it disappears
  • Chaining constructors with this(...)
  • References, null and passing objects to methods
  • Overriding toString()

Questions people ask

Do I need an account?

No. Anyone can play. Sign in to keep your XP, streak, badges and history.

Can I retake it?

Yes. Your best score is kept, and the questions and answers are shuffled each time.