0
0
Javaprogramming~20 mins

Partial abstraction in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Partial Abstraction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of partial abstraction with abstract and concrete methods
What is the output of this Java code that uses partial abstraction with an abstract class and a concrete subclass?
Java
abstract class Vehicle {
    abstract void start();
    void stop() {
        System.out.println("Vehicle stopped");
    }
}

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

public class Main {
    public static void main(String[] args) {
        Vehicle v = new Car();
        v.start();
        v.stop();
    }
}
ACar started\nVehicle stopped
BVehicle started\nVehicle stopped
CCar started
DCompilation error due to abstract method
Attempts:
2 left
πŸ’‘ Hint
Remember that abstract classes can have both abstract and concrete methods, and the subclass must implement abstract methods.
🧠 Conceptual
intermediate
1:30remaining
Understanding partial abstraction in Java
Which statement best describes partial abstraction in Java?
AA class that cannot have any methods.
BA class that has only concrete methods and can be instantiated.
CA class that has only abstract methods and can be instantiated.
DA class that has both abstract and concrete methods and cannot be instantiated.
Attempts:
2 left
πŸ’‘ Hint
Think about what makes a class abstract and what partial abstraction means.
πŸ”§ Debug
advanced
2:00remaining
Identify the error in partial abstraction usage
What error will this code produce?
Java
abstract class Animal {
    abstract void sound();
    void sleep() {
        System.out.println("Sleeping");
    }
}

class Dog extends Animal {
    // Missing implementation of sound()
}

public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
        a.sleep();
    }
}
ACompilation error: Dog must implement abstract method sound()
BOutput:\nSleeping
CRuntime error: NullPointerException
DCompilation error: Cannot instantiate abstract class Animal
Attempts:
2 left
πŸ’‘ Hint
Check if all abstract methods are implemented in the subclass.
πŸ“ Syntax
advanced
1:30remaining
Correct syntax for partial abstraction
Which option shows the correct syntax for declaring a partially abstract class with one abstract and one concrete method?
Aabstract class Shape { abstract void draw() {} void erase(); }
Bclass Shape { abstract void draw(); void erase() { System.out.println("Erased"); } }
Cabstract class Shape { abstract void draw(); void erase() { System.out.println("Erased"); } }
Dabstract class Shape { void draw(); abstract void erase() { System.out.println("Erased"); } }
Attempts:
2 left
πŸ’‘ Hint
Remember abstract methods cannot have a body and abstract classes must be declared with the abstract keyword.
πŸš€ Application
expert
2:00remaining
Number of methods in a partially abstract class hierarchy
Given this code, how many methods can be called on the object 'obj' of type Parent?
Java
abstract class Parent {
    abstract void methodA();
    void methodB() { System.out.println("B"); }
}

class Child extends Parent {
    void methodA() { System.out.println("A"); }
    void methodC() { System.out.println("C"); }
}

public class Test {
    public static void main(String[] args) {
        Parent obj = new Child();
        // How many methods can be called on obj?
    }
}
ACannot call any methods on obj
B2 methods: methodA() and methodB()
C1 method: methodA() only
D3 methods: methodA(), methodB(), and methodC()
Attempts:
2 left
πŸ’‘ Hint
Remember the reference type determines accessible methods, not the object type.