Bird
Raised Fist0
C++programming~20 mins

Interface-like behavior in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of virtual function call through base pointer
What is the output of this C++ code?
C++
#include <iostream>

class IShape {
public:
    virtual void draw() const = 0;
    virtual ~IShape() = default;
};

class Circle : public IShape {
public:
    void draw() const override {
        std::cout << "Circle" << std::endl;
    }
};

int main() {
    IShape* shape = new Circle();
    shape->draw();
    delete shape;
    return 0;
}
ACircle
BIShape
CCompilation error due to abstract class instantiation
DRuntime error due to pure virtual function call
Attempts:
2 left
πŸ’‘ Hint
Think about how virtual functions work when called through a base class pointer.
❓ Predict Output
intermediate
2:00remaining
Output of multiple inheritance with interface classes
What will this program print?
C++
#include <iostream>

class IReadable {
public:
    virtual void read() const = 0;
    virtual ~IReadable() = default;
};

class IWritable {
public:
    virtual void write() const = 0;
    virtual ~IWritable() = default;
};

class File : public IReadable, public IWritable {
public:
    void read() const override {
        std::cout << "Reading file" << std::endl;
    }
    void write() const override {
        std::cout << "Writing file" << std::endl;
    }
};

int main() {
    File f;
    IReadable* r = &f;
    IWritable* w = &f;
    r->read();
    w->write();
    return 0;
}
ACompilation error due to ambiguous base classes
BReading file\nWriting file
CWriting file\nReading file
DRuntime error due to multiple inheritance
Attempts:
2 left
πŸ’‘ Hint
Each interface has a pure virtual function. The class File implements both.
❓ Predict Output
advanced
2:00remaining
Output of interface pointer to derived object with overridden method
What is the output of this code?
C++
#include <iostream>

class IAnimal {
public:
    virtual void speak() const = 0;
    virtual ~IAnimal() = default;
};

class Dog : public IAnimal {
public:
    void speak() const override {
        std::cout << "Woof" << std::endl;
    }
};

class Cat : public IAnimal {
public:
    void speak() const override {
        std::cout << "Meow" << std::endl;
    }
};

void makeSpeak(const IAnimal* animal) {
    animal->speak();
}

int main() {
    Dog dog;
    Cat cat;
    makeSpeak(&dog);
    makeSpeak(&cat);
    return 0;
}
ARuntime error due to calling pure virtual function
BMeow\nWoof
CCompilation error due to missing virtual destructor
DWoof\nMeow
Attempts:
2 left
πŸ’‘ Hint
The function makeSpeak calls speak() on the interface pointer.
❓ Predict Output
advanced
2:00remaining
Output of interface with default method and override
What will this program print?
C++
#include <iostream>

class IPrinter {
public:
    virtual void print() const {
        std::cout << "Default print" << std::endl;
    }
    virtual ~IPrinter() = default;
};

class CustomPrinter : public IPrinter {
public:
    void print() const override {
        std::cout << "Custom print" << std::endl;
    }
};

int main() {
    IPrinter* p1 = new IPrinter();
    IPrinter* p2 = new CustomPrinter();
    p1->print();
    p2->print();
    delete p1;
    delete p2;
    return 0;
}
ADefault print\nCustom print
BCustom print\nDefault print
CCompilation error: cannot instantiate abstract class IPrinter
DRuntime error due to calling virtual function on deleted object
Attempts:
2 left
πŸ’‘ Hint
IPrinter has a default implementation of print(), so it can be instantiated.
❓ Predict Output
expert
3:00remaining
Output of diamond inheritance with virtual inheritance
What is the output of this program?
C++
#include <iostream>

class IDevice {
public:
    virtual void info() const = 0;
    virtual ~IDevice() = default;
};

class Scanner : virtual public IDevice {
public:
    void info() const override {
        std::cout << "Scanner" << std::endl;
    }
};

class Printer : virtual public IDevice {
public:
    void info() const override {
        std::cout << "Printer" << std::endl;
    }
};

class AllInOne : public Scanner, public Printer {
public:
    void info() const override {
        std::cout << "AllInOne" << std::endl;
    }
};

int main() {
    AllInOne aio;
    IDevice* device = &aio;
    device->info();
    return 0;
}
APrinter
BScanner
CAllInOne
DCompilation error due to ambiguous inheritance
Attempts:
2 left
πŸ’‘ Hint
Virtual inheritance avoids duplicate IDevice base. The AllInOne class overrides info().

Practice

(1/5)
1. What is the main purpose of an interface-like class in C++?
easy
A. To store data members for derived classes
B. To provide full function implementations for reuse
C. To define functions that derived classes must implement
D. To create objects directly without inheritance

Solution

  1. Step 1: Understand interface concept

    Interfaces specify what functions a class must have but do not provide their code.
  2. Step 2: Identify interface role in C++

    In C++, interfaces are created using pure virtual functions that derived classes must implement.
  3. Final Answer:

    To define functions that derived classes must implement -> Option C
  4. Quick Check:

    Interface = function declarations only [OK]
Hint: Interfaces declare required functions without code [OK]
Common Mistakes:
  • Thinking interfaces provide function code
  • Assuming interfaces hold data members
  • Believing interfaces can be instantiated
2. Which of the following is the correct syntax to declare a pure virtual function in a C++ class?
easy
A. virtual void func() = 0;
B. virtual void func();
C. void func() override;
D. virtual void func() = 1;

Solution

  1. Step 1: Recall pure virtual function syntax

    Pure virtual functions are declared with '= 0' after the function signature.
  2. Step 2: Check options for correct syntax

    virtual void func() = 0; uses '= 0' correctly, others do not match pure virtual syntax.
  3. Final Answer:

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

    Pure virtual = '= 0' [OK]
Hint: Pure virtual functions end with '= 0;' [OK]
Common Mistakes:
  • Using '= 1' instead of '= 0'
  • Omitting 'virtual' keyword
  • Confusing override with pure virtual
3. Consider the following code:
class IShape {
public:
    virtual double area() const = 0;
};

class Circle : public IShape {
    double radius;
public:
    Circle(double r) : radius(r) {}
    double area() const override { return 3.14 * radius * radius; }
};

int main() {
    Circle c(2);
    std::cout << c.area();
    return 0;
}

What is the output of this program?
medium
A. 4
B. Compilation error
C. 3.14
D. 12.56

Solution

  1. Step 1: Calculate area of circle with radius 2

    Area = Ο€ * rΒ² = 3.14 * 2 * 2 = 12.56
  2. Step 2: Confirm code compiles and runs correctly

    Circle implements pure virtual function area(), so no error; output is area value.
  3. Final Answer:

    12.56 -> Option D
  4. Quick Check:

    Area = 3.14 * 4 = 12.56 [OK]
Hint: Calculate area formula carefully [OK]
Common Mistakes:
  • Forgetting to implement pure virtual function
  • Using wrong radius value
  • Expecting compilation error
4. What is wrong with this code snippet?
class IAnimal {
public:
    virtual void speak() = 0;
};

class Dog : public IAnimal {
public:
    void speak() { std::cout << "Woof"; }
};

int main() {
    IAnimal a;
    a.speak();
    return 0;
}
medium
A. Missing override keyword in Dog::speak()
B. Cannot create object of abstract class IAnimal
C. speak() should return a value
D. Dog class must be abstract

Solution

  1. Step 1: Identify abstract class usage

    IAnimal has a pure virtual function, so it is abstract and cannot be instantiated.
  2. Step 2: Check main() for object creation

    Code tries to create IAnimal a; which is illegal and causes compilation error.
  3. Final Answer:

    Cannot create object of abstract class IAnimal -> Option B
  4. Quick Check:

    Abstract class objects cannot be created [OK]
Hint: Abstract classes cannot be instantiated [OK]
Common Mistakes:
  • Ignoring abstract class instantiation rules
  • Thinking override keyword is mandatory
  • Assuming pure virtual functions must return values
5. You want to create an interface-like class IPrinter with a pure virtual function print(). Then create two classes ConsolePrinter and FilePrinter that implement print(). Which of the following code snippets correctly demonstrates this?
hard
A. class IPrinter { public: virtual void print() = 0; }; class ConsolePrinter : public IPrinter { public: void print() override { std::cout << "Console output"; } }; class FilePrinter : public IPrinter { public: void print() override { /* write to file */ } };
B. class IPrinter { public: void print() { } }; class ConsolePrinter : public IPrinter { public: void print() { std::cout << "Console output"; } };
C. class IPrinter { public: virtual void print(); }; class ConsolePrinter : public IPrinter { };
D. class IPrinter { public: virtual void print() = 1; };

Solution

  1. Step 1: Define interface with pure virtual function

    IPrinter declares print() as pure virtual with '= 0', making it an interface.
  2. Step 2: Implement print() in derived classes

    ConsolePrinter and FilePrinter override print() with their own code, fulfilling interface contract.
  3. Final Answer:

    Code snippet D correctly defines interface and implementations -> Option A
  4. Quick Check:

    Pure virtual = 0 + override implementations [OK]
Hint: Use '= 0' for pure virtual and override in derived [OK]
Common Mistakes:
  • Using '= 1' instead of '= 0' for pure virtual
  • Not implementing pure virtual functions in derived classes
  • Defining interface functions with bodies
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