Challenge - 5 Problems
Object Interaction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediateOutput 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; }
Attempts:
2 left
π‘ Hint
Check how the Engine object is initialized inside the Car constructor.
β Incorrect
The Car constructor initializes its Engine member with power 150. The showPower method calls Engine's getPower, printing 150.
π§ Conceptual
intermediateUnderstanding 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? }
Attempts:
2 left
π‘ Hint
Passing by reference allows the method to modify the original object.
β Incorrect
The process method receives Box by reference and calls doubleLength, which doubles length from 5 to 10.
π§ Debug
advancedIdentify 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; }
Attempts:
2 left
π‘ Hint
Local variables inside functions are destroyed when the function ends.
β Incorrect
In add(), Node n is a local variable. Its address is assigned to head, but after add() returns, n is destroyed. Accessing head causes undefined behavior and crash.
π Syntax
advancedCorrect 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 }
Attempts:
2 left
π‘ Hint
Use the arrow operator to call methods on pointers.
β Incorrect
To call printMessage on p1, use p1->printMessage(). p2->printMessage() calls p2's method, not p1's. Options B and C are invalid syntax.
π Application
expertDetermine 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; }
Attempts:
2 left
π‘ Hint
The Manager's counter is updated by adding each Worker's id.
β Incorrect
assignWork calls doWork which adds Worker's id to Manager's counter. After both calls, counter = 3 + 7 = 10.
