Challenge - 5 Problems
Partial Abstraction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2: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(); } }
Attempts:
2 left
π‘ Hint
Remember that abstract classes can have both abstract and concrete methods, and the subclass must implement abstract methods.
β Incorrect
The abstract class Vehicle has an abstract method start() and a concrete method stop(). The subclass Car implements start(). When we call start() on the Vehicle reference pointing to Car, Car's start() runs. The stop() method is inherited and runs as is.
π§ Conceptual
intermediate1:30remaining
Understanding partial abstraction in Java
Which statement best describes partial abstraction in Java?
Attempts:
2 left
π‘ Hint
Think about what makes a class abstract and what partial abstraction means.
β Incorrect
Partial abstraction means a class has some methods with implementation (concrete) and some without (abstract). Such a class is abstract and cannot be instantiated directly.
π§ Debug
advanced2: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(); } }
Attempts:
2 left
π‘ Hint
Check if all abstract methods are implemented in the subclass.
β Incorrect
The Dog class does not implement the abstract method sound(), so it must be declared abstract or implement the method. Since it is not abstract and does not implement sound(), compilation fails.
π Syntax
advanced1: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?
Attempts:
2 left
π‘ Hint
Remember abstract methods cannot have a body and abstract classes must be declared with the abstract keyword.
β Incorrect
Option C correctly declares an abstract class with an abstract method draw() (no body) and a concrete method erase() with a body. Option C is invalid because abstract methods cannot be in non-abstract classes. Options C and D have syntax errors with abstract methods having bodies or concrete methods without bodies.
π Application
expert2: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? } }
Attempts:
2 left
π‘ Hint
Remember the reference type determines accessible methods, not the object type.
β Incorrect
The variable obj is of type Parent, so only methods declared in Parent are accessible: methodA() (abstract, implemented in Child) and methodB() (concrete). methodC() is declared only in Child and is not accessible via Parent reference.