Last Updated: January 3, 2026
32 quizzes
Which expression correctly declares a pointer to an int in C++?
What does the & operator do in this line: int* p = &value;
Given int arr[5]; and int* p = arr;, what does p+1 point to?
Which is TRUE about passing a pointer to a function?
Which statement about C++ references is correct?
What is the preferred way to represent a null pointer in modern C++?
Which smart pointer expresses exclusive ownership of a dynamically allocated object?
What happens when the last std::shared_ptr owning an object is destroyed?
What is the main purpose of std::weak_ptr?
Which operation is INVALID for a std::unique_ptr<int> p?
Which is the safest way to create a std::shared_ptr<Foo>?
Given int x = 10; int* p = &x; which changes x to 20?
Declare a pointer to const int from an existing int variable value.
int value = 42; const int* = &value;Click an option to fill the blank:
Use std::make_unique to allocate an int.
auto ptr = std::<int>(5);Click an option to fill the blank:
Pass a pointer to a function that takes an int* parameter.
void process(int* p); int x = 10; process();Click an option to fill the blank:
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}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}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}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}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}Find the bug related to pointers and object lifetime.
Click on the line(s) that contain the bug.
#include <string>const std::string* getName() { std::string name = "temp"; return &name;}int main() { const std::string* p = getName(); // use *p here} Find the memory management bug.
Click on the line(s) that contain the bug.
#include <memory>void process() { std::unique_ptr<int> p(new int(5)); int* raw = p.get(); delete raw;}int main() { process();} Click the line that risks dereferencing a null pointer.
Click on the line to select.
#include <iostream>void printLength(const char* p) { if (!p) { std::cout << "null" << std::endl; } std::cout << p[0] << std::endl;}int main() { printLength(nullptr);} Click the line responsible for a reference becoming dangling.
Click on the line to select.
#include <vector>int& getElement() { std::vector<int> v = {1, 2, 3}; return v[0];}int main() { int& ref = getElement();} Fill in the blanks to declare and use a reference parameter that increments a value.
#include <iostream>void inc( value) { ;}int main() { int x = 5; inc(x); std::cout << x << std::endl;}Click an option to fill blank 1:
Complete the code to safely check and use a pointer.
#include <iostream>void print(int* p) { if () { std::cout << *p << std::endl; }}int main() { int x = 7; int* p = ; print(p);}Click an option to fill blank 1:
Fill in the blanks to create and share ownership of a dynamically allocated int.
#include <memory>int main() { auto p1 = std::<int>(10); std::<int> p2 = p1;}Click an option to fill blank 1:
Complete the code so weak_ptr safely accesses a shared object.
#include <iostream>#include <memory>int main() { auto sp = std::make_shared<int>(42); std::weak_ptr<int> wp = sp; if (auto locked = wp.()) { std::cout << *locked << std::endl; } sp.();}Click an option to fill blank 1:
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.
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.
Order the steps to correctly use a dynamically allocated array with a raw pointer.
Drag and drop to reorder, or use the arrows.
Order the steps to safely use std::weak_ptr with std::shared_ptr.
Drag and drop to reorder, or use the arrows.