Challenge - 5 Problems
Java Object Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2: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()); } }
Attempts:
2 left
π‘ Hint
Remember area is width multiplied by height for each Box object.
β Incorrect
The Box constructor sets width and height. The area method returns width * height. For b1: 3*4=12, for b2: 5*6=30.
π§ Conceptual
intermediate1: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; } }
Attempts:
2 left
π‘ Hint
Remember to use the new keyword and provide the constructor argument.
β Incorrect
Option A correctly uses new with the constructor and passes the required String argument. Option A misses new, A misses parentheses and argument, D misses argument.
π§ Debug
advanced2: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); } }
Attempts:
2 left
π‘ Hint
Check if the constructor with a String parameter exists.
β Incorrect
The Person class only has a no-argument constructor. Trying to call Person("Alice") causes a compile error because no such constructor exists.
π Syntax
advanced1: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() {}
}Attempts:
2 left
π‘ Hint
Remember that new Dog[3] creates an array but does not create Dog objects inside.
β Incorrect
Option B creates an array and fills it with 3 new Dog objects. Option B creates an array of 3 null Dog references. Option B is invalid type assignment. Option B tries to call Dog constructor with int.
π Application
expert2: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); } }
Attempts:
2 left
π‘ Hint
Static variables are shared across all objects and incremented in constructor.
β Incorrect
Each new Item increments count by 1. After 3 objects, count is 3.