0
0
C++programming~20 mins

OOP principles overview in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
OOP Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this C++ code demonstrating encapsulation?

Consider this C++ class that uses encapsulation to protect its data.

class Box {
private:
    int length;
public:
    void setLength(int len) { length = len; }
    int getLength() { return length; }
};

int main() {
    Box box;
    box.setLength(10);
    std::cout << box.getLength() << std::endl;
    return 0;
}

What will this program print?

C++
#include <iostream>
class Box {
private:
    int length;
public:
    void setLength(int len) { length = len; }
    int getLength() { return length; }
};

int main() {
    Box box;
    box.setLength(10);
    std::cout << box.getLength() << std::endl;
    return 0;
}
AUndefined behavior due to uninitialized variable
B0
CCompilation error due to private member access
D10
Attempts:
2 left
πŸ’‘ Hint

Think about how the setter and getter methods control access to the private variable.

❓ Predict Output
intermediate
2:00remaining
What is the output of this C++ code demonstrating inheritance?

Look at this code with a base class and a derived class:

class Animal {
public:
    void speak() { std::cout << "Animal speaks" << std::endl; }
};

class Dog : public Animal {
public:
    void speak() { std::cout << "Dog barks" << std::endl; }
};

int main() {
    Dog dog;
    dog.speak();
    return 0;
}

What will this program print?

C++
#include <iostream>
class Animal {
public:
    void speak() { std::cout << "Animal speaks" << std::endl; }
};

class Dog : public Animal {
public:
    void speak() { std::cout << "Dog barks" << std::endl; }
};

int main() {
    Dog dog;
    dog.speak();
    return 0;
}
ADog barks
BCompilation error due to method overriding
CAnimal speaks
DRuntime error due to ambiguous call
Attempts:
2 left
πŸ’‘ Hint

Which class's speak method is called on the Dog object?

❓ Predict Output
advanced
2:00remaining
What is the output of this C++ code demonstrating polymorphism with virtual functions?

Consider this code using virtual functions:

class Base {
public:
    virtual void show() { std::cout << "Base show" << std::endl; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived show" << std::endl; }
};

int main() {
    Base* ptr = new Derived();
    ptr->show();
    delete ptr;
    return 0;
}

What will this program print?

C++
#include <iostream>
class Base {
public:
    virtual void show() { std::cout << "Base show" << std::endl; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived show" << std::endl; }
};

int main() {
    Base* ptr = new Derived();
    ptr->show();
    delete ptr;
    return 0;
}
ADerived show
BBase show
CCompilation error due to virtual function
DRuntime error due to invalid pointer
Attempts:
2 left
πŸ’‘ Hint

Virtual functions allow the program to decide which method to call at runtime.

❓ Predict Output
advanced
2:00remaining
What is the output of this C++ code demonstrating abstraction with pure virtual functions?

Look at this abstract class example:

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

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

int main() {
    Circle c;
    c.draw();
    return 0;
}

What will this program print?

C++
#include <iostream>
class Shape {
public:
    virtual void draw() = 0; // pure virtual function
};

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

int main() {
    Circle c;
    c.draw();
    return 0;
}
ACompilation error due to abstract class instantiation
BDrawing Circle
CRuntime error due to pure virtual function call
DNo output, program hangs
Attempts:
2 left
πŸ’‘ Hint

Can you create an object of the abstract class? What about the derived class?

🧠 Conceptual
expert
2:00remaining
Which OOP principle is best demonstrated by this C++ code snippet?

Consider this code:

class Logger {
public:
    void log(const std::string& message) {
        std::cout << "Log: " << message << std::endl;
    }
};

class FileLogger : public Logger {
public:
    void log(const std::string& message) {
        // code to write message to a file
        std::cout << "File log: " << message << std::endl;
    }
};

Which OOP principle does this best illustrate?

C++
#include <iostream>
#include <string>
class Logger {
public:
    void log(const std::string& message) {
        std::cout << "Log: " << message << std::endl;
    }
};

class FileLogger : public Logger {
public:
    void log(const std::string& message) {
        // code to write message to a file
        std::cout << "File log: " << message << std::endl;
    }
};
AEncapsulation - hiding data inside classes
BPolymorphism - using the same interface with different behaviors
CInheritance - reusing code from a base class
DAbstraction - hiding complex implementation details
Attempts:
2 left
πŸ’‘ Hint

Look at how FileLogger relates to Logger.