0
0
Javaprogramming~20 mins

Classes and objects in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Java Classes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of method call on object
What is the output of this Java program?
Java
public class Car {
    String brand;
    int year;

    public Car(String brand, int year) {
        this.brand = brand;
        this.year = year;
    }

    public void displayInfo() {
        System.out.println(brand + " " + year);
    }

    public static void main(String[] args) {
        Car myCar = new Car("Toyota", 2015);
        myCar.displayInfo();
    }
}
A2015 Toyota
BToyota 2015
CCar@15db9742
DCompilation error
Attempts:
2 left
πŸ’‘ Hint
Look at what the displayInfo method prints.
🧠 Conceptual
intermediate
2:00remaining
Understanding object reference assignment
What will be the output of this Java code snippet?
Java
class Box {
    int size;

    Box(int size) {
        this.size = size;
    }
}

public class Test {
    public static void main(String[] args) {
        Box b1 = new Box(5);
        Box b2 = b1;
        b2.size = 10;
        System.out.println(b1.size);
    }
}
A10
B5
CCompilation error
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
Remember that b1 and b2 refer to the same object.
πŸ”§ Debug
advanced
2:00remaining
Identify the error in constructor usage
What error will this Java code produce when compiled?
Java
public class Person {
    String name;
    int age;

    public Person() {
        name = "Unknown";
        age = 0;
    }

    public Person(String name) {
        this.name = name;
    }

    public static void main(String[] args) {
        Person p = new Person("Alice", 30);
        System.out.println(p.name + " " + p.age);
    }
}
ACompilation error: no constructor Person(String, int)
BRuntime error: NullPointerException
COutput: Alice 0
DOutput: Unknown 30
Attempts:
2 left
πŸ’‘ Hint
Check the constructors defined and the one used in main.
πŸ“ Syntax
advanced
2:00remaining
Identify the syntax error in class definition
Which option correctly fixes the syntax error in this Java class?
Java
public class Animal {
    String type
    int age;

    public Animal(String type, int age) {
        this.type = type;
        this.age = age;
    }
}
ARemove the constructor
BChange int age to Integer age
CAdd a constructor return type: public void Animal(String type, int age)
DAdd a semicolon after String type: String type;
Attempts:
2 left
πŸ’‘ Hint
Look carefully at the variable declarations.
πŸš€ Application
expert
2:00remaining
Determine the number of objects created
How many objects are created when this Java program runs?
Java
public class Node {
    int value;
    Node next;

    public Node(int value) {
        this.value = value;
        this.next = null;
    }

    public static void main(String[] args) {
        Node first = new Node(1);
        Node second = new Node(2);
        first.next = second;
        Node third = second;
    }
}
A1
B3
C2
D0
Attempts:
2 left
πŸ’‘ Hint
Count how many times 'new' is used.