Challenge - 5 Problems
Object Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of object creation and member access
What is the output of this C++ code when creating an object and accessing its member?
C++
class Box { public: int length; Box(int l) : length(l) {} }; int main() { Box b(10); std::cout << b.length << std::endl; return 0; }
Attempts:
2 left
π‘ Hint
Look at how the constructor initializes the length member.
β Incorrect
The constructor sets length to 10, so printing b.length outputs 10.
β Predict Output
intermediate2:00remaining
Output when creating an object with default constructor
What will be the output of this code snippet?
C++
class Point { public: int x, y; Point() { x = 5; y = 7; } }; int main() { Point p; std::cout << p.x << "," << p.y << std::endl; return 0; }
Attempts:
2 left
π‘ Hint
The default constructor sets x and y explicitly.
β Incorrect
The default constructor assigns 5 to x and 7 to y, so output is '5,7'.
β Predict Output
advanced2:00remaining
Output of object creation with private constructor
What happens when you try to compile and run this code?
C++
class Secret { private: Secret() {} public: static Secret create() { return Secret(); } }; int main() { Secret s = Secret::create(); return 0; }
Attempts:
2 left
π‘ Hint
The static method can access the private constructor.
β Incorrect
The private constructor is accessible inside the static create method, so object creation works fine.
β Predict Output
advanced2:00remaining
Output of object creation with deleted copy constructor
What will happen when this code is compiled and run?
C++
class NoCopy { public: NoCopy() {} NoCopy(const NoCopy&) = delete; }; int main() { NoCopy a; NoCopy b = a; return 0; }
Attempts:
2 left
π‘ Hint
Copying is disabled explicitly.
β Incorrect
The copy constructor is deleted, so copying object 'a' to 'b' causes a compilation error.
π§ Conceptual
expert2:00remaining
Number of objects created in this code
How many objects are created in total when this code runs?
C++
class Sample { public: Sample() {} }; Sample createSample() { Sample s; return s; } int main() { Sample a = createSample(); return 0; }
Attempts:
2 left
π‘ Hint
Consider the local object and the returned object.
β Incorrect
One object is created inside createSample, and another is created when returning it to 'a'.