0
0
Javaprogramming~20 mins

Abstract methods in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Abstract Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of abstract method implementation
What is the output of the following Java code?
Java
abstract class Animal {
    abstract void sound();
}

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

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
    }
}
AAnimal sound
BBark
CCompilation error: Cannot instantiate abstract class
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
Look at which class implements the abstract method and what method is called.
🧠 Conceptual
intermediate
1:30remaining
Purpose of abstract methods
What is the main purpose of declaring a method as abstract in Java?
ATo force subclasses to provide their own implementation of the method
BTo provide a method implementation that cannot be overridden
CTo allow the method to be called without creating an object
DTo make the method private and inaccessible outside the class
Attempts:
2 left
πŸ’‘ Hint
Think about what happens if a subclass does not implement an abstract method.
πŸ”§ Debug
advanced
2:00remaining
Identify the compilation error
What error will this code produce?
Java
abstract class Shape {
    abstract void draw();
}

class Circle extends Shape {
    // No draw() method implemented
}

public class Main {
    public static void main(String[] args) {
        Shape s = new Circle();
        s.draw();
    }
}
ACompilation error: Cannot instantiate abstract class Shape
BRuntime error: draw() method not found
CCompilation error: Circle must implement abstract method draw()
DNo error, prints nothing
Attempts:
2 left
πŸ’‘ Hint
Check if all abstract methods are implemented in subclasses.
πŸ“ Syntax
advanced
1:30remaining
Correct syntax for abstract method declaration
Which option shows the correct way to declare an abstract method in Java?
Apublic abstract void run() {}
Bvoid abstract run();
Cpublic void abstract run();
Dabstract void run();
Attempts:
2 left
πŸ’‘ Hint
Abstract methods have no body and must end with a semicolon.
πŸš€ Application
expert
2:00remaining
Number of abstract methods in a class
Given the following code, how many abstract methods does class B have?
Java
abstract class A {
    abstract void m1();
    abstract void m2();
}

abstract class B extends A {
    void m1() {
        System.out.println("Implemented m1");
    }
}
A1
B2
C0
DCannot determine
Attempts:
2 left
πŸ’‘ Hint
Count abstract methods not implemented in class B.