AlgoMaster Logo

Pointers & References - Quiz

Last Updated: January 3, 2026

Pointers & References Quiz

32 quizzes

1
Multiple Choice

Which expression correctly declares a pointer to an int in C++?

2
Multiple Choice

What does the & operator do in this line: int* p = &value;

3
Multiple Choice

Given int arr[5]; and int* p = arr;, what does p+1 point to?

4
Multiple Choice

Which is TRUE about passing a pointer to a function?

5
Multiple Choice

Which statement about C++ references is correct?

6
Multiple Choice

What is the preferred way to represent a null pointer in modern C++?

7
Multiple Choice

Which smart pointer expresses exclusive ownership of a dynamically allocated object?

8
Multiple Choice

What happens when the last std::shared_ptr owning an object is destroyed?

9
Multiple Choice

What is the main purpose of std::weak_ptr?

10
Multiple Choice

Which operation is INVALID for a std::unique_ptr<int> p?

11
Multiple Choice

Which is the safest way to create a std::shared_ptr<Foo>?

12
Multiple Choice

Given int x = 10; int* p = &x; which changes x to 20?

13
Code Completion

Declare a pointer to const int from an existing int variable value.

cpp
1
int value = 42; const int* = &value;

Click an option to fill the blank:

14
Code Completion

Use std::make_unique to allocate an int.

cpp
1
auto ptr = std::<int>(5);

Click an option to fill the blank:

15
Code Completion

Pass a pointer to a function that takes an int* parameter.

cpp
1
void process(int* p); int x = 10; process();

Click an option to fill the blank:

16
Output Prediction

What is the output of this code?

1#include <iostream>
2int main() {
3    int value = 10;
4    int* p = &value;
5    *p = 15;
6    int& ref = value;
7    ref += 5;
8    std::cout << value << std::endl;
9    return 0;
10}
17
Output Prediction

What is the output?

1#include <iostream>
2int main() {
3    int arr[3] = {1, 2, 3};
4    int* p = arr;
5    std::cout << *p << " ";
6    p++;
7    std::cout << *p << " ";
8    std::cout << *(p + 1) << std::endl;
9    return 0;
10}
18
Output Prediction

What does this program print?

1#include <iostream>
2#include <memory>
3int main() {
4    auto p1 = std::make_shared<int>(5);
5    std::shared_ptr<int> p2 = p1;
6    std::weak_ptr<int> w = p1;
7    p1.reset();
8    if (auto sp = w.lock()) {
9        std::cout << *sp << " " << sp.use_count() << std::endl;
10    } else {
11        std::cout << "expired" << std::endl;
12    }
13    return 0;
14}
19
Output Prediction

Predict the output.

1#include <iostream>
2void increment(int* p) {
3    if (p) (*p)++;
4}
5int main() {
6    int* p = nullptr;
7    increment(p);
8    int x = 3;
9    p = &x;
10    increment(p);
11    std::cout << x << std::endl;
12    return 0;
13}
20
Output Prediction

What is the output?

1#include <iostream>
2int main() {
3    int a = 1;
4    int b = 2;
5    int* p = &a;
6    int*& pref = p;
7    pref = &b;
8    *pref += 3;
9    std::cout << a << " " << b << std::endl;
10    return 0;
11}
21
Bug Spotting

Find the bug related to pointers and object lifetime.

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

cpp
1
#include <string>
2
const std::string* getName() {
3
    std::string name = "temp";
4
    return &name;
5
}
6
int main() {
7
    const std::string* p = getName();
8
    // use *p here
9
}
10
 
22
Bug Spotting

Find the memory management bug.

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

cpp
1
#include <memory>
2
void process() {
3
    std::unique_ptr<int> p(new int(5));
4
    int* raw = p.get();
5
    delete raw;
6
}
7
int main() {
8
    process();
9
}
10
 
23
Hotspot Selection

Click the line that risks dereferencing a null pointer.

Click on the line to select.

cpp
1
#include <iostream>
2
void printLength(const char* p) {
3
    if (!p) {
4
        std::cout << "null" << std::endl;
5
    }
6
    std::cout << p[0] << std::endl;
7
}
8
int main() {
9
    printLength(nullptr);
10
}
11
 
24
Hotspot Selection

Click the line responsible for a reference becoming dangling.

Click on the line to select.

cpp
1
#include <vector>
2
int& getElement() {
3
    std::vector<int> v = {1, 2, 3};
4
    return v[0];
5
}
6
int main() {
7
    int& ref = getElement();
8
}
9
 
25
Fill in the Blanks

Fill in the blanks to declare and use a reference parameter that increments a value.

cpp
1
#include <iostream>
2
void inc( value) {
3
;
4
}
5
int main() {
6
int x = 5;
7
inc(x);
8
std::cout << x << std::endl;
9
}
10

Click an option to fill blank 1:

26
Fill in the Blanks

Complete the code to safely check and use a pointer.

cpp
1
#include <iostream>
2
void print(int* p) {
3
if () {
4
std::cout << *p << std::endl;
5
}
6
}
7
int main() {
8
int x = 7;
9
int* p = ;
10
print(p);
11
}
12

Click an option to fill blank 1:

27
Fill in the Blanks

Fill in the blanks to create and share ownership of a dynamically allocated int.

cpp
1
#include <memory>
2
int main() {
3
auto p1 = std::<int>(10);
4
std::<int> p2 = p1;
5
}
6

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code so weak_ptr safely accesses a shared object.

cpp
1
#include <iostream>
2
#include <memory>
3
int main() {
4
auto sp = std::make_shared<int>(42);
5
std::weak_ptr<int> wp = sp;
6
if (auto locked = wp.()) {
7
std::cout << *locked << std::endl;
8
}
9
sp.();
10
}
11

Click an option to fill blank 1:

29
Matching

Match each pointer-related term with its description.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

30
Matching

Match the smart pointer with its primary ownership model.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

31
Sequencing

Order the steps to correctly use a dynamically allocated array with a raw pointer.

Drag and drop to reorder, or use the arrows.

32
Sequencing

Order the steps to safely use std::weak_ptr with std::shared_ptr.

Drag and drop to reorder, or use the arrows.