Challenge - 5 Problems
OOP Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
π§ Conceptual
intermediateMain benefit of encapsulation in OOP
Which of the following best describes why encapsulation is used in object-oriented programming?
Attempts:
2 left
π‘ Hint
Think about how OOP keeps data safe inside objects.
β Incorrect
Encapsulation means bundling data and methods inside objects and restricting direct access to some components. This protects data integrity and hides complexity.
β Predict Output
intermediateOutput 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; }
Attempts:
2 left
π‘ Hint
Which speak() method is called on the Dog object?
β Incorrect
The Dog class has its own speak() method which overrides the Animal's speak(). So calling d.speak() prints "Dog barks".
β Predict Output
advancedOutput 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; }
Attempts:
2 left
π‘ Hint
Virtual functions allow calling the derived class method through base pointer.
β Incorrect
Because show() is virtual, the call b->show() invokes Derived's show(), printing "Derived show".
π§ Conceptual
advancedWhy use inheritance in OOP?
Which reason best explains why inheritance is used in object-oriented programming?
Attempts:
2 left
π‘ Hint
Think about how child classes relate to parent classes.
β Incorrect
Inheritance allows new classes to reuse code from existing classes and organize them in a hierarchy, promoting code reuse and logical structure.
π§ Conceptual
expertWhy is polymorphism important in OOP?
What is the main advantage of polymorphism in object-oriented programming?
Attempts:
2 left
π‘ Hint
Polymorphism helps with flexibility and code generalization.
β Incorrect
Polymorphism lets different classes be accessed through the same interface, enabling flexible and extensible code.
