0
0
C++programming~20 mins

Object interaction in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Object Interaction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of method interaction between objects
What is the output of the following C++ code involving two interacting objects?
C++
#include <iostream>
using namespace std;

class Engine {
public:
    int power;
    Engine(int p) : power(p) {}
    int getPower() { return power; }
};

class Car {
public:
    Engine engine;
    Car(int p) : engine(p) {}
    void showPower() {
        cout << "Power: " << engine.getPower() << endl;
    }
};

int main() {
    Car car(150);
    car.showPower();
    return 0;
}
A
Power: 150
B
Power: 0
C
Compilation error due to missing default constructor
D
Runtime error due to uninitialized engine
Attempts:
2 left
πŸ’‘ Hint
Check how the Engine object is initialized inside the Car constructor.
🧠 Conceptual
intermediate
2:00remaining
Understanding object references in method calls
Consider two classes where one object calls a method on another object passed by reference. What is the effect of modifying the passed object inside the method?
C++
class Box {
public:
    int length;
    Box(int l) : length(l) {}
    void doubleLength() { length *= 2; }
};

class Worker {
public:
    void process(Box &b) {
        b.doubleLength();
    }
};

int main() {
    Box box(5);
    Worker w;
    w.process(box);
    // What is box.length now?
}
A10
B5
CCompilation error due to missing const
DUndefined behavior due to dangling reference
Attempts:
2 left
πŸ’‘ Hint
Passing by reference allows the method to modify the original object.
πŸ”§ Debug
advanced
3:00remaining
Identify the cause of runtime error in object interaction
This code compiles but crashes at runtime. What causes the crash?
C++
#include <iostream>
using namespace std;

class Node {
public:
    int value;
    Node* next;
    Node(int v) : value(v), next(nullptr) {}
};

class List {
public:
    Node* head;
    List() : head(nullptr) {}
    void add(int v) {
        Node n(v);
        n.next = head;
        head = &n;
    }
    void print() {
        Node* current = head;
        while(current) {
            cout << current->value << " ";
            current = current->next;
        }
        cout << endl;
    }
};

int main() {
    List l;
    l.add(10);
    l.add(20);
    l.print();
    return 0;
}
AThe Node constructor is missing initialization of next pointer
BThe head pointer is never initialized causing null pointer dereference
CThe add method stores address of a local variable which is destroyed after add returns
DThe print method modifies the list causing infinite loop
Attempts:
2 left
πŸ’‘ Hint
Local variables inside functions are destroyed when the function ends.
πŸ“ Syntax
advanced
2:00remaining
Correct syntax for object interaction with pointers
Which option correctly creates two objects and makes one object call a method on the other using pointers?
C++
class Printer {
public:
    void printMessage() { std::cout << "Hello" << std::endl; }
};

int main() {
    Printer* p1 = new Printer();
    Printer* p2 = new Printer();
    // Call printMessage of p1 using p2
}
Ap2->printMessage();
Bp1->printMessage();
Cp2->p1->printMessage();
D(*p2).p1.printMessage();
Attempts:
2 left
πŸ’‘ Hint
Use the arrow operator to call methods on pointers.
πŸš€ Application
expert
3:00remaining
Determine the final state of interacting objects after method calls
Given these classes, what is the final value of the counter in the Manager object after main runs?
C++
#include <iostream>
using namespace std;

class Worker {
public:
    int id;
    Worker(int i) : id(i) {}
    void doWork(int &counter) {
        counter += id;
    }
};

class Manager {
public:
    int counter = 0;
    void assignWork(Worker &w) {
        w.doWork(counter);
    }
};

int main() {
    Manager m;
    Worker w1(3), w2(7);
    m.assignWork(w1);
    m.assignWork(w2);
    cout << m.counter << endl;
    return 0;
}
A3
B0
C7
D10
Attempts:
2 left
πŸ’‘ Hint
The Manager's counter is updated by adding each Worker's id.