0
0
Javaprogramming~20 mins

Object creation in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Java Object Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Java code creating objects?
Consider the following Java class and main method. What will be printed when this code runs?
Java
class Box {
    int width;
    int height;
    Box(int w, int h) {
        width = w;
        height = h;
    }
    int area() {
        return width * height;
    }
}

public class Main {
    public static void main(String[] args) {
        Box b1 = new Box(3, 4);
        Box b2 = new Box(5, 6);
        System.out.println(b1.area() + "," + b2.area());
    }
}
A0,0
B3,5
C12,30
D7,11
Attempts:
2 left
πŸ’‘ Hint
Remember area is width multiplied by height for each Box object.
🧠 Conceptual
intermediate
1:30remaining
Which statement correctly creates an object of class Car?
Given a class named Car with a constructor that takes a String model, which of the following lines correctly creates a Car object?
Java
class Car {
    String model;
    Car(String m) {
        model = m;
    }
}
ACar myCar = new Car("Tesla");
BCar myCar = Car("Tesla");
CCar myCar = new Car();
DCar myCar = new Car;
Attempts:
2 left
πŸ’‘ Hint
Remember to use the new keyword and provide the constructor argument.
πŸ”§ Debug
advanced
2:00remaining
What error does this object creation code produce?
Examine the code below. What error will the compiler show?
Java
class Person {
    String name;
    Person() {
        name = "Unknown";
    }
}

public class Main {
    public static void main(String[] args) {
        Person p = new Person("Alice");
        System.out.println(p.name);
    }
}
ACompile-time error: missing semicolon
BRuntime NullPointerException
CPrints: Unknown
DCompile-time error: constructor Person(String) not found
Attempts:
2 left
πŸ’‘ Hint
Check if the constructor with a String parameter exists.
πŸ“ Syntax
advanced
1:30remaining
Which option correctly creates an array of 3 new Dog objects?
Given class Dog with a no-argument constructor, which code correctly creates an array of 3 Dog objects?
Java
class Dog {
    Dog() {}
}
ADog[] dogs = new Dog[3];
BDog[] dogs = {new Dog(), new Dog(), new Dog()};
CDog dogs = new Dog[3];
DDog[] dogs = new Dog(3);
Attempts:
2 left
πŸ’‘ Hint
Remember that new Dog[3] creates an array but does not create Dog objects inside.
πŸš€ Application
expert
2:00remaining
What is the value of count after creating objects?
Consider this class with a static count variable. What is the value of count after main runs?
Java
class Item {
    static int count = 0;
    Item() {
        count++;
    }
}

public class Main {
    public static void main(String[] args) {
        Item i1 = new Item();
        Item i2 = new Item();
        Item i3 = new Item();
        System.out.println(Item.count);
    }
}
A3
B0
C1
DCompilation error
Attempts:
2 left
πŸ’‘ Hint
Static variables are shared across all objects and incremented in constructor.