Bird
Raised Fist0
C++programming~20 mins

Real-world modeling in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Real-world Modeling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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;
}
A
Bark
Meow
B
Some sound
Some sound
C
Bark
Some sound
D
Meow
Bark
Attempts:
2 left
πŸ’‘ Hint
Think about virtual functions and which method is called when using base class pointers.
🧠 Conceptual
intermediate
1: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; }
};
ATo hide the balance variable and control access through methods
BTo make balance a global variable
CTo allow direct access to balance from outside the class
DTo prevent the class from being inherited
Attempts:
2 left
πŸ’‘ Hint
Think about why balance is private and how deposit and withdraw work.
πŸ”§ Debug
advanced
2: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;
}
ALogical error: speed remains 0
BCompilation error: cannot call method on pointer
COutput: 10
DSegmentation fault (accessing null pointer)
Attempts:
2 left
πŸ’‘ Hint
What happens if you call a method on a null pointer?
πŸ“ Syntax
advanced
1: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.
A
struct Point {
    int x, y;
    Point(int a, int b) { x = a; y = b; }
};
B
struct Point {
    int x, y;
    Point(int a, int b) { x: a; y: b; }
};
C
struct Point {
    int x, y;
    Point(int a, int b) : x(a), y(b) {}
};
D
struct Point {
    int x, y;
    Point(int a, int b) -&gt; x(a), y(b) {}
};
Attempts:
2 left
πŸ’‘ Hint
Look for correct constructor initialization syntax in C++.
πŸš€ Application
expert
2: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;
}
A25.56636
B24.56636
C12.56636
D18.56636
Attempts:
2 left
πŸ’‘ Hint
Calculate area of rectangle (3*4) and circle (Ο€*2Β²) and add them.

Practice

(1/5)
1. What is the main purpose of using classes in real-world modeling in C++?
easy
A. To write faster code without any structure
B. To represent real-world objects with attributes and behaviors
C. To avoid using variables in the program
D. To make the program run without errors

Solution

  1. Step 1: Understand the role of classes

    Classes group data (attributes) and functions (methods) to model real-world things.
  2. Step 2: Identify the purpose in modeling

    Classes help represent objects with their properties and actions clearly.
  3. Final Answer:

    To represent real-world objects with attributes and behaviors -> Option B
  4. Quick Check:

    Classes model objects = A [OK]
Hint: Classes group data and actions like real objects [OK]
Common Mistakes:
  • Thinking classes only speed up code
  • Confusing classes with simple variables
  • Believing classes remove all errors
2. Which of the following is the correct way to declare a class named Car in C++?
easy
A. Car class { };
B. class = Car { };
C. class Car { };
D. class Car();

Solution

  1. Step 1: Recall C++ class syntax

    The correct syntax starts with the keyword class, followed by the class name and braces.
  2. Step 2: Check each option

    class Car { }; uses class Car { }; which is correct. Others have syntax errors.
  3. Final Answer:

    class Car { }; -> Option C
  4. Quick Check:

    Correct class syntax = B [OK]
Hint: Use 'class Name { };' to declare classes [OK]
Common Mistakes:
  • Writing 'Car class' instead of 'class Car'
  • Using parentheses after class name
  • Missing semicolon after class definition
3. What will be the output of this C++ code?
class Dog {
public:
  int age;
  void bark() { std::cout << "Woof!"; }
};

int main() {
  Dog myDog;
  myDog.age = 5;
  std::cout << myDog.age << " ";
  myDog.bark();
  return 0;
}
medium
A. Woof!5
B. Woof! 5
C. 5Woof!
D. 5 Woof!

Solution

  1. Step 1: Analyze output statements

    The code prints myDog.age followed by a space, then calls bark() which prints "Woof!".
  2. Step 2: Combine outputs

    Output is 5 plus Woof!, so result is "5 Woof!".
  3. Final Answer:

    5 Woof! -> Option D
  4. Quick Check:

    Print age + space + bark = 5 Woof! [OK]
Hint: Check order and spaces in output statements [OK]
Common Mistakes:
  • Assuming space after bark output
  • Mixing order of printed values
  • Ignoring no newline after prints
4. Identify the error in this C++ class definition:
class Book {
  string title;
  int pages;
public:
  void setTitle(string t) { title = t; }
  void setPages(int p) { pages = p; }
};
medium
A. Missing std:: before string
B. Missing semicolon after class definition
C. Methods should be declared outside the class
D. Private members must be declared after public

Solution

  1. Step 1: Check usage of string type

    The code uses string without std:: or using namespace std;.
  2. Step 2: Identify missing namespace

    In C++, string is in the std namespace, so std::string is needed unless using directive is present.
  3. Final Answer:

    Missing std:: before string -> Option A
  4. Quick Check:

    Use std::string or using namespace std [OK]
Hint: Always prefix string with std:: or use namespace std [OK]
Common Mistakes:
  • Forgetting std:: prefix for string
  • Thinking methods must be outside class
  • Believing private must come after public
5. You want to model a Library that holds many Book objects. Which design best represents this real-world relationship in C++?
hard
A. class Library { std::vector<Book> books; };
B. class Library { Book book; };
C. class Library { int bookCount; };
D. class Library { void addBook(Book b); };

Solution

  1. Step 1: Understand the relationship

    A library contains many books, so it needs a collection (like a vector) of Book objects.
  2. Step 2: Evaluate options

    class Library { std::vector<Book> books; }; uses std::vector<Book> to hold multiple books, matching the real-world model.
  3. Final Answer:

    class Library { std::vector<Book> books; }; -> Option A
  4. Quick Check:

    Use collection for many objects = D [OK]
Hint: Use vector to hold many objects in a class [OK]
Common Mistakes:
  • Using single object instead of collection
  • Only storing count without objects
  • Defining methods without data members
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