0
0
Javaprogramming~20 mins

Abstract classes in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Abstract Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of abstract class method call
What is the output of this Java program that uses an abstract class and a subclass?
Java
abstract class Animal {
    abstract void sound();
}

class Dog extends Animal {
    void sound() {
        System.out.println("Woof");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
    }
}
AAnimal
BRuntime error: NullPointerException
CCompilation error: Cannot instantiate abstract class
DWoof
Attempts:
2 left
πŸ’‘ Hint
Look at which class is instantiated and which method is called.
🧠 Conceptual
intermediate
1:30remaining
Why use abstract classes?
Which of the following is the main reason to use an abstract class in Java?
ATo create objects directly without subclassing
BTo allow multiple inheritance of implementation
CTo provide a common base with some shared code and force subclasses to implement specific methods
DTo improve runtime performance by avoiding method calls
Attempts:
2 left
πŸ’‘ Hint
Think about what abstract classes enforce and what they provide.
πŸ”§ Debug
advanced
2:00remaining
Identify the compilation error
What error does this code produce when compiled?
Java
abstract class Vehicle {
    abstract void start();
}

class Car extends Vehicle {
    void start() {
        System.out.println("Car started");
    }
}

class Bike extends Vehicle {
}

public class Test {
    public static void main(String[] args) {
        Vehicle v = new Bike();
        v.start();
    }
}
ARuntime error: NullPointerException
BCompilation error: Bike must implement abstract method start()
CCompilation error: Cannot instantiate abstract class Vehicle
DNo error, output: Car started
Attempts:
2 left
πŸ’‘ Hint
Check if all abstract methods are implemented in subclasses.
πŸ“ Syntax
advanced
1:30remaining
Correct abstract method declaration
Which option shows the correct way to declare an abstract method in an abstract class?
Java
abstract class Shape {
    // method declaration here
}
Aabstract void draw();
Bvoid abstract draw();
Cabstract void draw() {}
Dvoid draw();
Attempts:
2 left
πŸ’‘ Hint
Abstract methods have no body and use the 'abstract' keyword before the return type.
πŸš€ Application
expert
2:00remaining
Number of objects created
Given the code below, how many objects are created when main runs?
Java
abstract class Fruit {
    Fruit() {
        System.out.println("Fruit created");
    }
}

class Apple extends Fruit {
    Apple() {
        System.out.println("Apple created");
    }
}

public class Main {
    public static void main(String[] args) {
        Fruit f = new Apple();
    }
}
AOne object: an Apple instance
BTwo objects: one Fruit and one Apple
CNo objects, only references
DCompilation error: Cannot instantiate abstract class Fruit
Attempts:
2 left
πŸ’‘ Hint
Remember how constructors work in inheritance and abstract classes.