Challenge - 5 Problems
Real-world Modeling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of class inheritance and method overriding
What is the output of this C++ program modeling animals with inheritance and method overriding?
C++
#include <iostream> using namespace std; class Animal { public: virtual void sound() { cout << "Some sound" << endl; } }; class Dog : public Animal { public: void sound() override { cout << "Bark" << endl; } }; class Cat : public Animal { public: void sound() override { cout << "Meow" << endl; } }; int main() { Animal* a1 = new Dog(); Animal* a2 = new Cat(); a1->sound(); a2->sound(); delete a1; delete a2; return 0; }
Attempts:
2 left
π‘ Hint
Think about virtual functions and which method is called when using base class pointers.
β Incorrect
The base class Animal has a virtual method sound(). Dog and Cat override this method. When calling sound() through Animal pointers to Dog and Cat objects, the overridden methods in Dog and Cat are called, printing "Bark" and "Meow" respectively.
π§ Conceptual
intermediate1:30remaining
Understanding encapsulation in a bank account model
Which of the following best describes the purpose of encapsulation in the BankAccount class below?
C++
class BankAccount { private: double balance; public: BankAccount(double initial) : balance(initial) {} void deposit(double amount) { if(amount > 0) balance += amount; } bool withdraw(double amount) { if(amount > 0 && amount <= balance) { balance -= amount; return true; } return false; } double getBalance() const { return balance; } };
Attempts:
2 left
π‘ Hint
Think about why balance is private and how deposit and withdraw work.
β Incorrect
Encapsulation means hiding internal data and only allowing controlled access. Here, balance is private and can only be changed through deposit and withdraw methods, protecting it from invalid changes.
π§ Debug
advanced2:00remaining
Identify the runtime error in the vehicle speed simulation
What runtime error will this C++ code produce when simulating vehicle speed changes?
C++
#include <iostream> using namespace std; class Vehicle { public: int speed; Vehicle() : speed(0) {} void accelerate(int amount) { speed += amount; } }; int main() { Vehicle* v = nullptr; v->accelerate(10); cout << v->speed << endl; return 0; }
Attempts:
2 left
π‘ Hint
What happens if you call a method on a null pointer?
β Incorrect
The pointer v is null and calling v->accelerate(10) tries to access memory through a null pointer, causing a segmentation fault at runtime.
π Syntax
advanced1:30remaining
Which option correctly defines a struct for a 2D point with a constructor?
Select the option that compiles without errors and correctly defines a struct Point with x and y coordinates and a constructor.
Attempts:
2 left
π‘ Hint
Look for correct constructor initialization syntax in C++.
β Incorrect
Option C uses the member initializer list syntax which is correct. Option C uses assignment in constructor body which is valid but less preferred. Option C uses invalid syntax with colons inside constructor body. Option C uses invalid arrow syntax.
π Application
expert2:30remaining
Calculate total area of mixed shapes using polymorphism
Given the classes below modeling shapes, what is the output of the program calculating total area?
C++
#include <iostream> #include <vector> #include <memory> using namespace std; class Shape { public: virtual double area() const = 0; virtual ~Shape() = default; }; class Rectangle : public Shape { double width, height; public: Rectangle(double w, double h) : width(w), height(h) {} double area() const override { return width * height; } }; class Circle : public Shape { double radius; public: Circle(double r) : radius(r) {} double area() const override { return 3.14159 * radius * radius; } }; int main() { vector<unique_ptr<Shape>> shapes; shapes.push_back(make_unique<Rectangle>(3, 4)); shapes.push_back(make_unique<Circle>(2)); double total = 0; for (const auto& shape : shapes) { total += shape->area(); } cout << total << endl; return 0; }
Attempts:
2 left
π‘ Hint
Calculate area of rectangle (3*4) and circle (Ο*2Β²) and add them.
β Incorrect
Rectangle area = 3*4 = 12. Circle area = 3.14159*2Β² = 12.56636. Total = 24.56636. Polymorphism ensures the correct area() is called for each shape via base class pointers.