Challenge - 5 Problems
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2: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; }
Attempts:
2 left
π‘ Hint
Think about how virtual functions work when called through a base class pointer.
β Incorrect
The pointer is of type IShape*, but it points to a Circle object. The draw() function is virtual and overridden in Circle, so Circle's draw() is called, printing "Circle".
β Predict Output
intermediate2: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; }
Attempts:
2 left
π‘ Hint
Each interface has a pure virtual function. The class File implements both.
β Incorrect
The File class implements both read() and write(). The pointers r and w call the respective functions, printing "Reading file" then "Writing file".
β Predict Output
advanced2: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; }
Attempts:
2 left
π‘ Hint
The function makeSpeak calls speak() on the interface pointer.
β Incorrect
Dog and Cat override speak(). The calls print "Woof" then "Meow" in order.
β Predict Output
advanced2: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; }
Attempts:
2 left
π‘ Hint
IPrinter has a default implementation of print(), so it can be instantiated.
β Incorrect
IPrinter's print() prints "Default print". CustomPrinter overrides print() to print "Custom print". The calls print those lines in order.
β Predict Output
expert3: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; }
Attempts:
2 left
π‘ Hint
Virtual inheritance avoids duplicate IDevice base. The AllInOne class overrides info().
β Incorrect
AllInOne overrides info(), so calling info() through IDevice* calls AllInOne::info(), printing "AllInOne".