AlgoMaster Logo

Object-Oriented Programming - Quiz

Last Updated: January 3, 2026

1 min read

Object-Oriented Programming Quiz

31 quizzes

1
Multiple Choice

Which statement about a C++ class is true?

2
Multiple Choice

What is the default access specifier for members of a class in C++?

3
Multiple Choice

Which constructor will the compiler generate if you declare no constructors at all?

4
Multiple Choice

When is a destructor for an automatic (stack) object called?

5
Multiple Choice

What is the main purpose of a copy constructor?

6
Multiple Choice

Which parameter type correctly declares a move constructor?

7
Multiple Choice

In a non-static member function, what does the expression *this represent?

8
Multiple Choice

Which statement about static data members is correct?

9
Multiple Choice

Why might you declare a non-member function as a friend of a class?

10
Multiple Choice

For a value-like class managing a dynamic array, which operator should you typically overload?

11
Multiple Choice

Which access specifier allows access only within the class and its derived classes?

12
Code Completion

Declare a private data member length inside a class

cpp
1
class Box {
2
private:
3
int ;
4
public:
5
Box(int len) : length(len) {}
6
};

Click an option to fill the blank:

13
Code Completion

Complete the declaration of a copy constructor

cpp
1
class Point {
2
public:
3
Point(int x, int y);
4
Point(const Point& );
5
};

Click an option to fill the blank:

14
Code Completion

Declare a static member function that returns the object count

cpp
1
class Counter {
2
public:
3
static int ();
4
};

Click an option to fill the blank:

15
Output Prediction

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}
16
Output Prediction

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}
17
Output Prediction

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}
18
Output Prediction

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}
19
Output Prediction

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}
20
Bug Spotting

Find the bug in this copy constructor implementation

Click on the line(s) that contain the bug.

cpp
1
#include <cstring>
2
 
3
class Buffer {
4
private:
5
    char* data_;
6
    std::size_t size_;
7
public:
8
    Buffer(const char* src) {
9
        size_ = std::strlen(src);
10
        data_ = new char[size_ + 1];
11
        std::strcpy(data_, src);
12
    }
13
 
14
    Buffer(const Buffer& other) {
15
        data_ = other.data_;
16
        size_ = other.size_;
17
    }
18
 
19
    ~Buffer() {
20
        delete[] data_;
21
    }
22
};
21
Bug Spotting

Find the bug in this move constructor

Click on the line(s) that contain the bug.

cpp
1
#include <utility>
2
 
3
class Holder {
4
private:
5
    int* ptr_;
6
public:
7
    Holder(int v) : ptr_(new int(v)) {}
8
 
9
    Holder(Holder&& other) noexcept {
10
        ptr_ = other.ptr_;
11
    }
12
 
13
    ~Holder() {
14
        delete ptr_;
15
    }
16
};
22
Hotspot Selection

Click the line that violates encapsulation by accessing a private member directly

Click on the line to select.

cpp
1
#include <iostream>
2
using namespace std;
3
 
4
class Account {
5
private:
6
    double balance_;
7
public:
8
    Account(double b) : balance_(b) {}
9
    double getBalance() const { return balance_; }
10
};
11
 
12
int main() {
13
    Account acc(100.0);
14
    cout << acc.getBalance() << "\n";
15
    acc.balance_ = 200.0;
16
    cout << acc.getBalance() << "\n";
17
}
23
Hotspot Selection

Click the line where the copy constructor is invoked

Click on the line to select.

cpp
1
#include <iostream>
2
using namespace std;
3
 
4
class Widget {
5
public:
6
    Widget() { cout << "D"; }
7
    Widget(const Widget&) { cout << "C"; }
8
};
9
 
10
void use(Widget w) {
11
    cout << "U";
12
}
13
 
14
int main() {
15
    Widget w;
16
    use(w);
17
}
24
Fill in the Blanks

Complete the class with a default constructor and a destructor message

cpp
1
#include <iostream>
2
class Logger {
3
public:
4
Logger() { std::cout << ; }
5
~Logger() { std::cout << ; }
6
};
7
8
int main() {
9
Logger log;
10
}

Click an option to fill blank 1:

25
Fill in the Blanks

Complete the class so that setName supports method chaining using this pointer

cpp
1
#include <string>
2
class User {
3
std::string name_;
4
public:
5
User& setName(const std::string& name) {
6
= name;
7
return ;
8
}
9
};

Click an option to fill blank 1:

26
Fill in the Blanks

Complete the code to declare and define a static member counter

cpp
1
#include <iostream>
2
class Instance {
3
public:
4
static int ;
5
Instance() { ++; }
6
};
7
8
int Instance:: = ;
9
10
int main() {
11
Instance a, b;
12
std::cout << Instance::count << "\n";
13
}

Click an option to fill blank 1:

27
Fill in the Blanks

Complete friend function declaration and definition to access private data

cpp
1
#include <iostream>
2
class Box {
3
private:
4
int width_;
5
public:
6
Box(int w) : width_(w) {}
7
friend void (const Box& b);
8
};
9
10
void (const Box& b) {
11
std::cout << << "\n";
12
}
13
14
int main() {
15
Box b(10);
16
printWidth(b);
17
}

Click an option to fill blank 1:

28
Matching

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.

29
Matching

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.

30
Sequencing

Order the steps to correctly implement a class managing a dynamic array (Rule of Three)

Drag and drop to reorder, or use the arrows.

31
Sequencing

Order the steps when overloading operator+ as a non-member for a class

Drag and drop to reorder, or use the arrows.