0
0
Javaprogramming~20 mins

Class definition in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Java Class 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 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();
    }
}
AToyota 2020
BCar@15db9742
Cnull 0
DCompilation error
Attempts:
2 left
πŸ’‘ Hint
Look at the constructor and the printInfo method to see what values are printed.
❓ Predict Output
intermediate
2: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);
    }
}
ARuntime NullPointerException
BPrints: null
CCompilation error: name has private access in Person
DPrints: Alice
Attempts:
2 left
πŸ’‘ Hint
Check the access modifier of the field and where it is accessed.
❓ Predict Output
advanced
2: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);
    }
}
A
Counter id: 0
Counter id: 0
Total count: 0
B
Counter id: 2
Counter id: 2
Total count: 2
C
Counter id: 1
Counter id: 2
Total count: 2
DCompilation error
Attempts:
2 left
πŸ’‘ Hint
Static variables are shared among all instances, instance variables are unique per object.
❓ Predict Output
advanced
2: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;
    }
}
ARuntime NullPointerException
BNo error, compiles successfully
CCompilation error: variable type might not have been initialized
DCompilation error: cannot assign a variable to itself
Attempts:
2 left
πŸ’‘ Hint
Check what happens when you assign a field to itself in the constructor.
🧠 Conceptual
expert
2: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);
    }
}
A2
B3
C1
D0
Attempts:
2 left
πŸ’‘ Hint
Remember that assigning one object reference to another does not create a new object.