Classes & objects (OOP basics)

Computer Science · Java

Question 1 of 10

0

    What number does this Java code print? public class Main { public static void main(String[] args) { Robot a = new Robot(); a.size = 5; Robot b = a; b.size = 4; System.out.println(a.size); } } class Robot { int size; }

    Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.

    How to type your answer
    • Fractions: 3/4. Mixed numbers: 1 1/2.
    • Decimals with a point or a comma: 0.75 or 0,75.
    • Negative numbers start with a minus: -3/4.
    • Percentages need the % sign: 75%.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Classes & objects (OOP basics) explained

    In Java a class is a blueprint, and an object is an instance created from it with the new keyword. Each object has its own fields, which store its data, and methods, which are the actions it can perform. A variable of a class type holds a reference to the object, not the object itself.

    Fields are declared inside the class with a type, such as int age;, and a method is called with dot notation, such as dog.birthday(). Inside a method, a field can be used directly by name. Two variables can refer to the same object, so a change made through one is visible through the other. When an object is passed to a method, the method receives a copy of the reference. It can change the object's fields, but it cannot make the caller's variable point to a different object.

    class Dog {
        int age;
    
        void birthday() {
            age++;
        }
    }
    
    Dog rex = new Dog();
    Dog copy = rex;
    copy.birthday();
    System.out.println(rex.age);
    

    Worked example

    Trace the code above.

    1. new Dog() creates one object with age 0, and rex holds a reference to it.
    2. copy = rex copies the reference, not the object, so both names refer to the same Dog.
    3. copy.birthday() increments age on that shared object, making it 1.
    4. rex.age prints 1.

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      What number does this Java code print?

      public class Main { public static void main(String[] args) { Crate a = new Crate(); a.size = 4; Crate b = a; b.size = 2; System.out.println(a.size); } }

      class Crate { int size; }

    • Medium

      What number does this Java code print?

      public class Main { public static void main(String[] args) { Token a = new Token(); a.size = 8; Token b = new Token(); b.size += 1; System.out.println(a.size + b.size); } }

      class Token { int size; }

    • Hard

      What number does this Java code print?

      public class Main { static void grow(int n) { n = n + 1; }

      static void bump(Token box) { box.size = box.size + 1; }

      static void reassign(Token box) { box = new Token(); box.size = 99; }

      public static void main(String[] args) { Token b = new Token(); b.size = 7; int n = 7; bump(b); bump(b); grow(n); System.out.println(b.size); } }

      class Token { int size; }