Challenge - 5 Problems
Java Class 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 class instantiation?
Consider the following Java class and main method. What will be printed when the program runs?
Java
public class Car { String brand; int year; public Car(String brand, int year) { this.brand = brand; this.year = year; } public void printInfo() { System.out.println(brand + " " + year); } public static void main(String[] args) { Car myCar = new Car("Toyota", 2020); myCar.printInfo(); } }
Attempts:
2 left
π‘ Hint
Look at the constructor and the printInfo method to see what values are printed.
β Incorrect
The constructor sets the brand to "Toyota" and year to 2020. The printInfo method prints these values separated by a space.
β Predict Output
intermediate2:00remaining
What happens if you try to access a private field directly?
Given this Java class, what will happen if you try to compile and run the main method?
Java
public class Person { private String name; public Person(String name) { this.name = name; } public static void main(String[] args) { Person p = new Person("Alice"); System.out.println(p.name); } }
Attempts:
2 left
π‘ Hint
Check the access modifier of the field and where it is accessed.
β Incorrect
Private fields are accessible within the same class, including from static methods via an instance. The program compiles and prints 'Alice'.
β Predict Output
advanced2:00remaining
What is the output of this Java class with static and instance variables?
Analyze the following Java code and determine what it prints when run.
Java
public class Counter { static int count = 0; int id; public Counter() { count++; id = count; } public void printId() { System.out.println("Counter id: " + id); } public static void main(String[] args) { Counter c1 = new Counter(); Counter c2 = new Counter(); c1.printId(); c2.printId(); System.out.println("Total count: " + Counter.count); } }
Attempts:
2 left
π‘ Hint
Static variables are shared among all instances, instance variables are unique per object.
β Incorrect
Each new Counter increments the static count and assigns it to the instance id. So c1 has id 1, c2 has id 2, and total count is 2.
β Predict Output
advanced2:00remaining
What error does this Java class produce?
Look at this Java class code. What error will occur when compiling?
Java
public class Animal { String type; public Animal() { this.type = type; } }
Attempts:
2 left
π‘ Hint
Check what happens when you assign a field to itself in the constructor.
β Incorrect
Assigning 'this.type = type;' assigns the field to itself because the parameter 'type' is missing, so it assigns the field to its own value (null). This is allowed and compiles without error.
π§ Conceptual
expert2:00remaining
How many objects are created in this Java code?
Consider the following Java code snippet. How many objects are created when main runs?
Java
public class Box { int size; public Box(int size) { this.size = size; } public static void main(String[] args) { Box b1 = new Box(5); Box b2 = b1; Box b3 = new Box(10); } }
Attempts:
2 left
π‘ Hint
Remember that assigning one object reference to another does not create a new object.
β Incorrect
Two new Box objects are created with 'new Box(5)' and 'new Box(10)'. The assignment 'Box b2 = b1;' does not create a new object, just another reference.