Bird
Raised Fist0
C++programming~20 mins

Abstract classes in C++ - Practice Problems & Coding Challenges

Choose your learning style10 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 pointer calling derived method

What is the output of this C++ code?

C++
#include <iostream>
using namespace std;

class Shape {
public:
    virtual void draw() = 0;
};

class Circle : public Shape {
public:
    void draw() override {
        cout << "Drawing Circle" << endl;
    }
};

int main() {
    Shape* shape = new Circle();
    shape->draw();
    delete shape;
    return 0;
}
ARuntime error: pure virtual function call
BShape::draw called
CCompilation error: cannot instantiate abstract class
DDrawing Circle
Attempts:
2 left
πŸ’‘ Hint

Remember that calling a pure virtual function through a derived class pointer calls the derived override.

❓ Predict Output
intermediate
2:00remaining
Value of member after calling pure virtual function in constructor

What is the output of this C++ program?

C++
#include <iostream>
using namespace std;

class Base {
public:
    Base() {
        print();
    }
    virtual void print() = 0;
};

class Derived : public Base {
public:
    int x = 5;
    void print() override {
        cout << x << endl;
    }
};

int main() {
    Derived d;
    return 0;
}
A5
BUndefined behavior or runtime error
CCompilation error: pure virtual function call in constructor
D0
Attempts:
2 left
πŸ’‘ Hint

Think about which class's constructor is running and what members are initialized at that time.

πŸ”§ Debug
advanced
2:00remaining
Identify the error in abstract class usage

What error does this code produce?

C++
#include <iostream>
using namespace std;

class Animal {
public:
    virtual void sound() = 0;
};

class Dog : public Animal {
public:
    void sound() {
        cout << "Woof" << endl;
    }
};

int main() {
    Animal a;
    a.sound();
    return 0;
}
ACompilation error: cannot instantiate abstract class 'Animal'
BRuntime error: pure virtual function call
COutput: Woof
DCompilation error: missing override specifier
Attempts:
2 left
πŸ’‘ Hint

Can you create an object of an abstract class?

πŸ“ Syntax
advanced
2:00remaining
Which option correctly declares an abstract class?

Which of the following code snippets correctly declares an abstract class in C++?

Aclass A { virtual void f() {}; };
Bclass A { void f() = 0; };
Cclass A { virtual void f() = 0; };
Dclass A { virtual void f() = 1; };
Attempts:
2 left
πŸ’‘ Hint

Remember the syntax for pure virtual functions.

πŸš€ Application
expert
3:00remaining
Number of objects created and output

Consider the following code. How many objects are created and what is the output?

C++
#include <iostream>
using namespace std;

class Abstract {
public:
    Abstract() { cout << "Abstract created\n"; }
    virtual void action() = 0;
};

class Concrete : public Abstract {
public:
    Concrete() { cout << "Concrete created\n"; }
    void action() override { cout << "Action done\n"; }
};

int main() {
    Concrete c;
    Abstract& ref = c;
    ref.action();
    return 0;
}
A
1 object created; output:
Abstract created
Concrete created
Action done
BCompilation error: cannot instantiate Abstract
C
1 object created; output:
Concrete created
Action done
D
2 objects created; output:
Abstract created
Concrete created
Action done
Attempts:
2 left
πŸ’‘ Hint

Think about how many constructors run when creating a derived object.

Practice

(1/5)
1. What is true about an abstract class in C++?
easy
A. It can be instantiated like any other class.
B. It cannot be instantiated directly.
C. It must have no member functions.
D. It cannot have any data members.

Solution

  1. Step 1: Understand abstract class instantiation rules

    Abstract classes have at least one pure virtual function, so they cannot create objects directly.
  2. Step 2: Check other options

    The other options are incorrect because abstract classes can have member functions and data members.
  3. Final Answer:

    It cannot be instantiated directly. -> Option B
  4. Quick Check:

    Abstract class = no direct objects [OK]
Hint: Abstract classes always have pure virtual functions [OK]
Common Mistakes:
  • Thinking abstract classes can create objects
  • Believing abstract classes cannot have data members
  • Confusing abstract classes with interfaces
2. Which of the following is the correct way to declare a pure virtual function in a C++ abstract class?
easy
A. virtual void func() = 0;
B. void func() = 0;
C. virtual void func();
D. void func() {};

Solution

  1. Step 1: Recall pure virtual function syntax

    Pure virtual functions are declared with 'virtual' keyword and '= 0' at the end.
  2. Step 2: Analyze options

    virtual void func() = 0; matches the correct syntax. void func() = 0; misses 'virtual'. virtual void func(); is a normal virtual function. void func() {}; is a normal function with a body.
  3. Final Answer:

    virtual void func() = 0; -> Option A
  4. Quick Check:

    Pure virtual = virtual + '= 0' [OK]
Hint: Pure virtual always ends with '= 0;' [OK]
Common Mistakes:
  • Omitting 'virtual' keyword
  • Adding function body to pure virtual
  • Confusing normal virtual with pure virtual
3. What will be the output of the following C++ code?
#include <iostream>
class Base {
public:
    virtual void show() = 0;
};
class Derived : public Base {
public:
    void show() { std::cout << "Hello"; }
};
int main() {
    Derived d;
    d.show();
    return 0;
}
medium
A. No output
B. Compilation error
C. Hello
D. Runtime error

Solution

  1. Step 1: Check abstract class and derived implementation

    Base has a pure virtual function 'show'. Derived overrides it with a concrete implementation.
  2. Step 2: Analyze main function output

    Derived object 'd' calls 'show()', which prints "Hello".
  3. Final Answer:

    Hello -> Option C
  4. Quick Check:

    Derived implements pure virtual, so prints output [OK]
Hint: Derived must implement pure virtual to avoid errors [OK]
Common Mistakes:
  • Expecting compilation error without implementation
  • Thinking pure virtual functions run by themselves
  • Confusing output with no output
4. Identify the error in this code snippet:
#include <iostream>
class Abstract {
public:
    virtual void display() = 0;
};

class Concrete : public Abstract {
public:
    void display() { std::cout << "Hi"; }
};

int main() {
    Abstract a;
    Concrete c;
    c.display();
    return 0;
}
medium
A. Cannot instantiate abstract class 'Abstract' directly.
B. Missing implementation of display() in Concrete.
C. Syntax error in class declaration.
D. No error, code runs fine.

Solution

  1. Step 1: Check instantiation of abstract class

    Abstract class 'Abstract' has a pure virtual function, so cannot create object 'a'.
  2. Step 2: Verify other parts

    Concrete implements display(), so no error there. Syntax is correct.
  3. Final Answer:

    Cannot instantiate abstract class 'Abstract' directly. -> Option A
  4. Quick Check:

    Abstract objects not allowed [OK]
Hint: Never create objects of abstract classes [OK]
Common Mistakes:
  • Trying to instantiate abstract class
  • Ignoring pure virtual function rules
  • Assuming implementation missing in derived
5. You want to design a shape system where every shape must provide its area calculation. Which is the best way to enforce this using abstract classes in C++?
hard
A. Create a class Shape with no functions and add area() only in derived classes.
B. Create a class Shape with a normal function double area() that returns 0, and override it in derived classes.
C. Create a class Shape with a virtual function double area() with empty body.
D. Create an abstract class Shape with a pure virtual function double area() = 0; and derive specific shapes implementing area().

Solution

  1. Step 1: Understand requirement for enforcing area() implementation

    Using a pure virtual function in an abstract class forces all derived classes to implement area().
  2. Step 2: Evaluate options

    Create an abstract class Shape with a pure virtual function double area() = 0; and derive specific shapes implementing area(). uses pure virtual function, which is the best way. Create a class Shape with a normal function double area() that returns 0, and override it in derived classes. returns 0 by default, which may hide missing implementations. Create a class Shape with no functions and add area() only in derived classes. does not enforce area() in base. Create a class Shape with a virtual function double area() with empty body. has virtual but not pure, so no enforcement.
  3. Final Answer:

    Create an abstract class Shape with a pure virtual function double area() = 0; and derive specific shapes implementing area(). -> Option D
  4. Quick Check:

    Pure virtual enforces implementation [OK]
Hint: Use pure virtual to force derived class methods [OK]
Common Mistakes:
  • Using normal virtual instead of pure virtual
  • Not enforcing method in base class
  • Returning default values hiding missing overrides
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard