0
0
C++programming~20 mins

Why object-oriented programming is used in C++ - Challenge Your Understanding

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!
🧠 Conceptual
intermediate
2:00remaining
Main benefit of encapsulation in OOP
Which of the following best describes why encapsulation is used in object-oriented programming?
ATo make all variables global for easy access
BTo hide internal details and protect object data from outside interference
CTo increase the speed of program execution
DTo allow multiple inheritance from different classes
Attempts:
2 left
πŸ’‘ Hint
Think about how OOP keeps data safe inside objects.
❓ Predict Output
intermediate
2:00remaining
Output of inheritance example
What is the output of this C++ code using inheritance?
C++
#include <iostream>
using namespace std;

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

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

int main() {
    Dog d;
    d.speak();
    return 0;
}
ADog barks
BAnimal speaks
CCompilation error due to speak() redefinition
DNo output
Attempts:
2 left
πŸ’‘ Hint
Which speak() method is called on the Dog object?
❓ Predict Output
advanced
2:00remaining
Output of polymorphism with virtual function
What is the output of this C++ code demonstrating polymorphism?
C++
#include <iostream>
using namespace std;

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

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

int main() {
    Base* b = new Derived();
    b->show();
    delete b;
    return 0;
}
ABase show
BCompilation error due to missing override keyword
CDerived show
DRuntime error due to invalid pointer
Attempts:
2 left
πŸ’‘ Hint
Virtual functions allow calling the derived class method through base pointer.
🧠 Conceptual
advanced
2:00remaining
Why use inheritance in OOP?
Which reason best explains why inheritance is used in object-oriented programming?
ATo convert procedural code into assembly
BTo make all functions static
CTo prevent any changes to class behavior
DTo reuse code and create a hierarchy of classes
Attempts:
2 left
πŸ’‘ Hint
Think about how child classes relate to parent classes.
🧠 Conceptual
expert
2:00remaining
Why is polymorphism important in OOP?
What is the main advantage of polymorphism in object-oriented programming?
AIt allows objects of different classes to be treated through a common interface
BIt forces all methods to have the same name
CIt eliminates the need for constructors
DIt makes programs run faster by skipping method calls
Attempts:
2 left
πŸ’‘ Hint
Polymorphism helps with flexibility and code generalization.