Last Updated: January 3, 2026
31 quizzes
Which statement about a C++ class is true?
What is the default access specifier for members of a class in C++?
Which constructor will the compiler generate if you declare no constructors at all?
When is a destructor for an automatic (stack) object called?
What is the main purpose of a copy constructor?
Which parameter type correctly declares a move constructor?
In a non-static member function, what does the expression *this represent?
Which statement about static data members is correct?
Why might you declare a non-member function as a friend of a class?
For a value-like class managing a dynamic array, which operator should you typically overload?
Which access specifier allows access only within the class and its derived classes?
Declare a private data member length inside a class
class Box {private: int ;public: Box(int len) : length(len) {}};Click an option to fill the blank:
Complete the declaration of a copy constructor
class Point {public: Point(int x, int y); Point(const Point& );};Click an option to fill the blank:
Declare a static member function that returns the object count
class Counter {public: static int ();};Click an option to fill the blank:
What is the output of this code about constructors and destructors?
1#include <iostream>
2using namespace std;
3
4class A {
5public:
6 A() { cout << "A"; }
7 ~A() { cout << "a"; }
8};
9
10class B {
11public:
12 B() { cout << "B"; }
13 ~B() { cout << "b"; }
14private:
15 A a_;
16};
17
18int main() {
19 B b;
20 return 0;
21}What does this code print about copy constructor usage?
1#include <iostream>
2using namespace std;
3
4class Sample {
5public:
6 Sample() { cout << "D"; }
7 Sample(const Sample&) { cout << "C"; }
8};
9
10Sample makeSample() {
11 Sample s;
12 return s;
13}
14
15int main() {
16 Sample s1 = makeSample();
17 return 0;
18}What is the output regarding this pointer usage?
1#include <iostream>
2using namespace std;
3
4class Box {
5 int length;
6public:
7 Box(int length) { this->length = length; }
8 void print() const { cout << length << "\n"; }
9};
10
11int main() {
12 Box b(10);
13 b.print();
14 return 0;
15}What is the output of this operator overloading example?
1#include <iostream>
2using namespace std;
3
4class Vector2 {
5public:
6 int x, y;
7 Vector2(int x, int y) : x(x), y(y) {}
8 Vector2 operator+(const Vector2& other) const {
9 return Vector2(x + other.x, y + other.y);
10 }
11};
12
13int main() {
14 Vector2 a(1, 2);
15 Vector2 b(3, 4);
16 Vector2 c = a + b;
17 cout << c.x << "," << c.y << "\n";
18}What does this static member example print?
1#include <iostream>
2using namespace std;
3
4class Counter {
5public:
6 static int count;
7 Counter() { ++count; }
8};
9
10int Counter::count = 0;
11
12int main() {
13 Counter a;
14 {
15 Counter b;
16 Counter c;
17 cout << Counter::count << " ";
18 }
19 cout << Counter::count << "\n";
20}Find the bug in this copy constructor implementation
Click on the line(s) that contain the bug.
#include <cstring> class Buffer {private: char* data_; std::size_t size_;public: Buffer(const char* src) { size_ = std::strlen(src); data_ = new char[size_ + 1]; std::strcpy(data_, src); } Buffer(const Buffer& other) { data_ = other.data_; size_ = other.size_; } ~Buffer() { delete[] data_; }};Find the bug in this move constructor
Click on the line(s) that contain the bug.
#include <utility> class Holder {private: int* ptr_;public: Holder(int v) : ptr_(new int(v)) {} Holder(Holder&& other) noexcept { ptr_ = other.ptr_; } ~Holder() { delete ptr_; }};Click the line that violates encapsulation by accessing a private member directly
Click on the line to select.
#include <iostream>using namespace std; class Account {private: double balance_;public: Account(double b) : balance_(b) {} double getBalance() const { return balance_; }}; int main() { Account acc(100.0); cout << acc.getBalance() << "\n"; acc.balance_ = 200.0; cout << acc.getBalance() << "\n";}Click the line where the copy constructor is invoked
Click on the line to select.
#include <iostream>using namespace std; class Widget {public: Widget() { cout << "D"; } Widget(const Widget&) { cout << "C"; }}; void use(Widget w) { cout << "U";} int main() { Widget w; use(w);}Complete the class with a default constructor and a destructor message
#include <iostream>class Logger {public: Logger() { std::cout << ; } ~Logger() { std::cout << ; }};int main() { Logger log;}Click an option to fill blank 1:
Complete the class so that setName supports method chaining using this pointer
#include <string>class User { std::string name_;public: User& setName(const std::string& name) { = name; return ; }};Click an option to fill blank 1:
Complete the code to declare and define a static member counter
#include <iostream>class Instance {public: static int ; Instance() { ++; }};int Instance:: = ;int main() { Instance a, b; std::cout << Instance::count << "\n";}Click an option to fill blank 1:
Complete friend function declaration and definition to access private data
#include <iostream>class Box {private: int width_;public: Box(int w) : width_(w) {} friend void (const Box& b);};void (const Box& b) { std::cout << << "\n";}int main() { Box b(10); printWidth(b);}Click an option to fill blank 1:
Match the special member function with its primary responsibility
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match the operator overload with the typical use for a value-like class
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Order the steps to correctly implement a class managing a dynamic array (Rule of Three)
Drag and drop to reorder, or use the arrows.
Order the steps when overloading operator+ as a non-member for a class
Drag and drop to reorder, or use the arrows.