Bird
Raised Fist0
Javaprogramming~20 mins

Object creation in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. Which of the following is the correct way to create an object of class Car in Java?
easy
A. Car myCar = new Car();
B. Car myCar = Car();
C. new Car myCar();
D. Car myCar = new car();

Solution

  1. Step 1: Understand object creation syntax

    In Java, objects are created using the new keyword followed by the class name and parentheses.
  2. Step 2: Check each option

    Car myCar = new Car(); uses new Car() correctly with proper capitalization and assignment. Others have syntax errors or wrong capitalization.
  3. Final Answer:

    Car myCar = new Car(); -> Option A
  4. Quick Check:

    Use new ClassName() to create objects [OK]
Hint: Remember: new + ClassName() creates an object [OK]
Common Mistakes:
  • Omitting the new keyword
  • Using wrong capitalization for class name
  • Missing parentheses after class name
2. Which of the following lines will cause a syntax error when creating an object of class Book?
easy
A. Book b=new Book();
B. Book b = new Book();
C. Book b = new Book;
D. Book b = new Book( );

Solution

  1. Step 1: Recall syntax for object creation

    In Java, when creating an object, parentheses must follow the class name even if the constructor has no parameters.
  2. Step 2: Identify the incorrect option

    Book b = new Book; misses the parentheses after new Book, causing a syntax error. Others are correct.
  3. Final Answer:

    Book b = new Book; -> Option C
  4. Quick Check:

    Always use parentheses after class name in new [OK]
Hint: Always include () after class name when using new [OK]
Common Mistakes:
  • Forgetting parentheses after class name
  • Confusing object creation with method calls
  • Using semicolon inside parentheses
3. What will be the output of the following code?
class Dog {
    String name;
    Dog(String n) {
        name = n;
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog("Buddy");
        System.out.println(d.name);
    }
}
medium
A. Buddy
B. null
C. Dog@someHashCode
D. Compilation error

Solution

  1. Step 1: Understand constructor assignment

    The constructor sets the name field to the string passed, which is "Buddy".
  2. Step 2: Check output of print statement

    Printing d.name outputs the string "Buddy" stored in the object.
  3. Final Answer:

    Buddy -> Option A
  4. Quick Check:

    Constructor sets field, printing field shows assigned value [OK]
Hint: Constructor sets values; print field to see stored data [OK]
Common Mistakes:
  • Expecting default null instead of assigned value
  • Confusing object reference print with field print
  • Missing constructor parameters
4. Identify the error in the following code snippet:
class Person {
    String name;
    Person(String n) {
        name = n;
    }
}

public class Test {
    public static void main(String[] args) {
        Person p = Person("Alice");
        System.out.println(p.name);
    }
}
medium
A. Missing semicolon after System.out.println
B. Constructor name does not match class name
C. Variable p is not declared
D. Missing new keyword when creating object

Solution

  1. Step 1: Check object creation syntax

    The code tries to create an object with Person("Alice") but misses the new keyword.
  2. Step 2: Confirm other parts are correct

    Constructor name matches class name, variable is declared, and semicolon is present.
  3. Final Answer:

    Missing new keyword when creating object -> Option D
  4. Quick Check:

    Use new keyword to create objects [OK]
Hint: Always use new before class name to create objects [OK]
Common Mistakes:
  • Omitting new keyword
  • Confusing method call with object creation
  • Incorrect constructor naming
5. Given the class below, which code correctly creates two Student objects with names "John" and "Jane" and prints their names?
class Student {
    String name;
    Student(String n) {
        name = n;
    }
}
hard
A. Student s1 = new Student("John"); Student s2 = new Student(); System.out.println(s1.name + ", " + s2.name);
B. Student s1 = new Student("John"); Student s2 = new Student("Jane"); System.out.println(s1.name + ", " + s2.name);
C. Student s1 = new Student(); Student s2 = new Student(); System.out.println(s1.name + ", " + s2.name);
D. Student s1 = Student("John"); Student s2 = Student("Jane"); System.out.println(s1.name + ", " + s2.name);

Solution

  1. Step 1: Check constructor usage

    The constructor requires a String parameter. Student s1 = new Student("John"); Student s2 = new Student("Jane"); System.out.println(s1.name + ", " + s2.name); correctly passes names "John" and "Jane".
  2. Step 2: Verify object creation and printing

    Student s1 = new Student("John"); Student s2 = new Student("Jane"); System.out.println(s1.name + ", " + s2.name); creates both objects properly and prints their names separated by a comma.
  3. Final Answer:

    Student s1 = new Student("John"); Student s2 = new Student("Jane"); System.out.println(s1.name + ", " + s2.name); -> Option B
  4. Quick Check:

    Use new with constructor parameters to create objects [OK]
Hint: Pass required parameters in new ClassName(params) [OK]
Common Mistakes:
  • Calling constructor without new keyword
  • Using default constructor when none exists
  • Not passing required parameters