0
0
Javaprogramming~20 mins

Abstract vs concrete classes in Java - Practice Questions

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 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();
    }
}
ABark
BAnimal sound
CCompilation error: cannot instantiate abstract class
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
Look at which class is instantiated and which method is called.
❓ Predict Output
intermediate
2:00remaining
What happens when instantiating an abstract class?
What error does the following code produce?
Java
abstract class Vehicle {
    abstract void move();
}

public class Main {
    public static void main(String[] args) {
        Vehicle v = new Vehicle();
        v.move();
    }
}
ARuntime error: NullPointerException
BCompilation error: cannot instantiate abstract class
CNo output, program runs successfully
DCompilation error: method move() not implemented
Attempts:
2 left
πŸ’‘ Hint
Can you create an object of an abstract class directly?
❓ Predict Output
advanced
2:00remaining
Output with abstract and concrete class methods
What is the output of this Java program?
Java
abstract class Shape {
    void display() {
        System.out.println("Shape display");
    }
    abstract void draw();
}

class Circle extends Shape {
    void draw() {
        System.out.println("Circle draw");
    }
    void display() {
        System.out.println("Circle display");
    }
}

public class Main {
    public static void main(String[] args) {
        Shape s = new Circle();
        s.display();
        s.draw();
    }
}
A
Circle display
Circle draw
B
Shape display
Shape draw
C
Shape display
Circle draw
DCompilation error: cannot override display()
Attempts:
2 left
πŸ’‘ Hint
Which display() method is called? The one in Shape or Circle?
❓ Predict Output
advanced
2:00remaining
Which option causes a compilation error?
Given the abstract class and subclass below, which option will cause a compilation error?
Java
abstract class Device {
    abstract void start();
}

class Phone extends Device {
    void start() {
        System.out.println("Phone starting");
    }
}

public class Main {
    public static void main(String[] args) {
        // Options below
    }
}
ADevice d = new Phone(); d.start();
BPhone p = new Phone(); p.start();
CDevice d = new Device(); d.start();
DDevice d; d = new Phone(); d.start();
Attempts:
2 left
πŸ’‘ Hint
Can you create an instance of an abstract class?
🧠 Conceptual
expert
2:00remaining
Why use abstract classes instead of concrete classes?
Which of the following is the best reason to use an abstract class instead of a concrete class in Java?
ATo improve runtime performance by avoiding method calls
BTo prevent any subclass from overriding methods
CTo allow creating objects directly from the abstract class
DTo provide a common template with some methods implemented and others left for subclasses to define
Attempts:
2 left
πŸ’‘ Hint
Think about what abstract classes allow you to do with methods.