Bird
Raised Fist0
C++programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following is NOT one of the four main principles of Object-Oriented Programming (OOP)?
easy
A. Encapsulation
B. Compilation
C. Polymorphism
D. Inheritance

Solution

  1. Step 1: Recall the four main OOP principles

    The four main principles are encapsulation, inheritance, polymorphism, and abstraction.
  2. Step 2: Identify the option not in the list

    Compilation is a process of converting code, not an OOP principle.
  3. Final Answer:

    Compilation -> Option B
  4. Quick Check:

    OOP principles exclude compilation [OK]
Hint: Remember the four OOP pillars: E, I, P, A [OK]
Common Mistakes:
  • Confusing compilation with abstraction
  • Mixing OOP principles with programming processes
2. Which keyword is used in C++ to create a new class?
easy
A. class
B. struct
C. object
D. define

Solution

  1. Step 1: Identify the C++ keyword for defining a class

    In C++, the keyword to define a class is class.
  2. Step 2: Check other options

    struct defines a structure, object is not a keyword, and define is for macros.
  3. Final Answer:

    class -> Option A
  4. Quick Check:

    Use class to define classes [OK]
Hint: Use 'class' keyword to define classes in C++ [OK]
Common Mistakes:
  • Confusing struct with class
  • Using 'object' as a keyword
  • Mixing macros with class definition
3. What will be the output of the following C++ code?
class Animal {
public:
    void sound() { std::cout << "Animal sound"; }
};

class Dog : public Animal {
public:
    void sound() { std::cout << "Bark"; }
};

int main() {
    Animal* a = new Dog();
    a->sound();
    delete a;
    return 0;
}
medium
A. Runtime error
B. Bark
C. Compilation error
D. Animal sound

Solution

  1. Step 1: Understand method call without virtual keyword

    The sound method is not virtual, so the pointer type Animal* calls Animal's method.
  2. Step 2: Determine output

    Since sound is not virtual, Animal sound is printed.
  3. Final Answer:

    Animal sound -> Option D
  4. Quick Check:

    Non-virtual method calls base class method [OK]
Hint: Without virtual, base class method runs [OK]
Common Mistakes:
  • Assuming polymorphism without virtual
  • Expecting derived class method call
  • Ignoring pointer type effect
4. Identify the error in the following C++ code snippet:
class Vehicle {
private:
    int speed;
public:
    void setSpeed(int s) { speed = s; }
};

int main() {
    Vehicle v;
    v.speed = 100;
    return 0;
}
medium
A. setSpeed method is not defined
B. Missing semicolon after class definition
C. Cannot access private member 'speed' directly
D. Vehicle object not created properly

Solution

  1. Step 1: Check access to private member

    The member speed is private and cannot be accessed directly outside the class.
  2. Step 2: Identify the error in main()

    Trying to assign v.speed = 100; causes an access violation error.
  3. Final Answer:

    Cannot access private member 'speed' directly -> Option C
  4. Quick Check:

    Private members need public methods for access [OK]
Hint: Private members can't be accessed outside class [OK]
Common Mistakes:
  • Trying to access private variables directly
  • Ignoring access specifiers
  • Assuming public by default
5. You want to design a C++ class hierarchy where a base class Shape defines a method area() but each derived class calculates area differently. Which OOP principle does this best demonstrate?
hard
A. Polymorphism
B. Inheritance
C. Encapsulation
D. Abstraction

Solution

  1. Step 1: Understand the scenario of different area calculations

    Each derived class implements area() differently, but shares the same interface.
  2. Step 2: Identify the OOP principle

    This is polymorphism: same method name, different behaviors in subclasses.
  3. Final Answer:

    Polymorphism -> Option A
  4. Quick Check:

    Same method, different behaviors = Polymorphism [OK]
Hint: Different behaviors, same method name = polymorphism [OK]
Common Mistakes:
  • Confusing with inheritance or abstraction
  • Thinking encapsulation means method overriding
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