Unit 2: Pointers, Reference Variables, Arrays and String Concepts - Practice Quiz
1
What must usually be done before dereferencing a void* pointer in C++?
nullptr value
2
If p is an int*, what does p + 1 point to?
3 Which declaration creates a pointer to a pointer to an integer?
int *p;
int p[2];
int **p;
int &p;
4 When does a pointer commonly become a dangling pointer?
nullptr
5 What is a wild pointer?
nullptr
6 Which statement is the preferred modern C++ way to assign a null value to an integer pointer?
int *p = new int;
int *p = void;
int *p = &p;
int *p = nullptr;
7 Which class member function is commonly used to release dynamic memory owned by an object?
8 Which operator is normally used to access a member through a pointer to an object?
->
.
::
&
9
Inside a non-static member function, what does the this pointer refer to?
10
Which statement declares an array containing five Student objects?
Student students[5];
Student students(5);
Student *students[0];
Student students = 5;
11
Which statement correctly defines a C++ string object containing Hello?
string::std s = "Hello";
std::string s = "Hello";
std::string s = 'Hello';
std::string = s("Hello");
12
Which std::string member function returns the number of characters in a string?
size()
find()
clear()
append()
13
Which std::string member function adds characters to the end of an existing string?
append()
length()
find()
compare()
14 Which statement correctly describes a basic difference between pointers and references?
* for normal access
nullptr
15 Which statement declares a two-dimensional integer array with 3 rows and 4 columns?
int a[12, 1];
int a[3][4];
int a[3, 4];
int a(3)(4);
16
For int matrix[2][3];, which expression accesses the element in the second row and third column?
matrix[2][3]
matrix[1][2]
matrix[1][3]
matrix[2][2]
17
Assuming Student has a public integer member named marks, which statement declares a pointer to that data member?
int *p = Student::marks;
int Student::p* = &Student::marks;
int Student::*p = &Student::marks;
int *Student::p = &Student::marks;
18
What kind of address can a void* generally store?
19
Which std::string member function removes all characters from a string?
empty()
find()
size()
clear()
20
If objPtr is a pointer to a Book object, which expression calls its display() member function?
objPtr->display();
objPtr&display();
objPtr::display();
objPtr.display();
21
What does the following C++ code print?
int value = 25;
void* vp = &value;
int* ip = static_cast<int*>(vp);
std::cout << *ip;
25
value
22
What is the output of the following code?
int a[] = {10, 20, 30, 40};
int* p = a;
p += 2;
std::cout << *(p - 1);
30
20
10
40
23
What does the following program fragment display?
int x = 4;
int* p = &x;
int** pp = &p;
**pp += 3;
std::cout << x;
4
7
x
3
24
Consider the following code:
std::vector<int> values = {1, 2};
int* p = &values[0];
values.push_back(3);
Which statement about p is correct after push_back?
nullptr
25
Which change correctly prevents p from being a wild pointer before it is dereferenced?
int* p;
*p = 10;
delete p; before assignment
p to a void* first
int* p = nullptr; only
int x; int* p = &x;
26
Given the overloaded functions below, which function is selected by the call?
void select(int);
void select(int*);
select(nullptr);
select(int)
select(int*)
27
What is printed before the program ends?
class Box {
public:
int* value;
Box(int v) : value(new int(v)) {}
};
int main() {
Box a(5);
Box b = a;
*b.value = 9;
std::cout << *a.value;
}
5
9
28
What is the output of the following code?
class Account {
int balance;
public:
Account() : balance(10) {}
void add(int x) { balance += x; }
int get() const { return balance; }
};
Account a;
Account* p = &a;
p->add(5);
std::cout << p->get();
5
50
10
15
29
What does the following code display?
class Number {
int value;
public:
Number(int value) { this->value = value; }
Number& add(int value) {
this->value += value;
return *this;
}
int get() const { return value; }
};
Number n(2);
std::cout << n.add(3).add(4).get();
7
5
4
9
30
What is printed by the following program fragment?
class Item {
int value;
public:
Item(int v) : value(v) {}
int get() const { return value; }
};
Item items[3] = {Item(1), Item(3), Item(5)};
std::cout << items[0].get() + items[2].get();
6
4
5
9
31
What is the output of this code?
std::string a(4, 'x');
std::string b = a;
b[1] = 'A';
std::cout << a << " " << b;
xAxx xAxx
xxxx xxxx
xAxx xxxx
xxxx xAxx
32
What value is assigned to pos?
std::string text = "cat scatter cat";
std::size_t pos = text.find("cat", 1);
12
0
4
5
33
What is the final value of s?
std::string s = "abcdef";
s.erase(2, 2);
s.insert(2, "XY");
abXYef
abcdXY
XYabef
abXYcd
34
What does the following code print?
int a = 3, b = 7;
int* p = &a;
int& r = a;
p = &b;
r = b;
std::cout << a << " " << b << " " << *p << " " << r;
3 7 3 7
7 7 7 7
7 3 3 7
3 7 7 3
35
What is the output of the following code?
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int (*p)[3] = a;
std::cout << *(*(p + 1) + 2);
4
5
3
6
36
What does g.sumColumn(1) return?
class Grid {
int a[2][3];
public:
Grid() {
int value = 1;
for (int r = 0; r < 2; ++r)
for (int c = 0; c < 3; ++c)
a[r][c] = value++;
}
int sumColumn(int c) const {
int sum = 0;
for (int r = 0; r < 2; ++r)
sum += a[r][c];
return sum;
}
};
Grid g;
10
5
8
7
37
What is printed by the following code?
struct Sample {
int x;
};
Sample s{8};
int Sample::*pm = &Sample::x;
s.*pm = 11;
std::cout << s.x;
x
11
8
38
Which function declaration can directly receive a built-in array declared as int table[3][4]?
void process(int** a, int rows);
void process(int a[][4], int rows);
void process(int a[][], int rows);
void process(int a[4][], int rows);
39
Which expression correctly accesses value through both an object pointer and a pointer to data member?
struct Node { int value; };
Node n{20};
Node* p = &n;
int Node::*pm = &Node::value;
p->pm
p->*pm
p.*pm
*p.pm
40
What is printed by the following code?
int a[6] = {};
int* p = &a[1];
int* q = &a[5];
std::cout << q - p;
3
6
4
5
41
Under standard C++17 rules, which operation on vp is well-formed?
int x = 42;
void* vp = &x;
int value = *static_cast<int*>(vp);
int value = *vp;
double* q = vp;
void* next = vp + 1;
42
What does the following program fragment print?
int a[3][4];
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 4; ++j)
a[i][j] = 10 * i + j;
int (*p)[4] = a;
std::cout << *(*(p + 2) + 1);
22
20
21
12
43
Given int** pp, which implicit initialization is valid in standard C++?
const int* const* q = pp;
void** q = pp;
double** q = pp;
const int** q = pp;
44
After which statement is p guaranteed to be dangling?
std::vector<int> v{1, 2};
int* p = &v[0];
std::size_t oldCapacity = v.capacity();
v.pop_back();
v[1] = 9;
v.reserve(oldCapacity + 1);
v.reserve(oldCapacity);
45
Consider the following declarations:
struct Holder {
int* p;
Holder() {}
};
static Holder a;
Holder b;
Holder c{};
Which statement is correct immediately after construction?
All three pointer members are null pointers.
a.p is null, while b.p and c.p are indeterminate.
a.p is indeterminate, while b.p and c.p are null.
All three pointer members have indeterminate values.
46
Given the overloads below, which call is unambiguous and invokes f(int*)?
void f(int*);
void f(long);
f(0L);
f(nullptr);
f(false);
f(0);
47 A class owns a dynamically allocated array through a raw pointer and deletes it in its destructor. Which copy-policy pair provides independent ownership while preserving copyability?
48
What change is required to make deletion through p well-defined and ensure both destructors run?
struct Base { ~Base() {} };
struct Derived : Base { ~Derived() {} };
Base* p = new Derived;
delete p;
Base::~Base() as virtual.
p to void* before deletion.
Derived::~Derived() as virtual.
delete p with delete[] p.
49
Assuming C++17, what does the following code print?
struct S {
int x = 1;
auto make() {
return [*this]() mutable { return ++x; };
}
};
S s;
auto g = s.make();
std::cout << g() << g() << s.x;
222
231
343
233
50
What is the status of the final statement?
struct Base { virtual int id() const { return 1; } };
struct Derived : Base { int extra = 0; int id() const override { return 2; } };
Derived objects[2];
Base* p = objects;
std::cout << p[1].id();
2.
1.
Derived* cannot convert to Base*.
51
What are the values of a.size(), b.size(), and a.size() after the final assignment?
std::string a = "ab\0cd";
std::string b("ab\0cd", 5);
std::size_t first = a.size();
std::size_t second = b.size();
a.assign(b.data() + 1, 3);
2, 2, 2
5, 5, 3
2, 5, 3
5, 5, 5
52
What pair is printed by this code?
std::string s = "bananana";
std::cout << s.find("ana", 2) << ','
<< s.rfind("ana", 4);
5,3
3,5
1,3
3,3
53
What is the value of s after the operation below?
std::string s = "abcdef";
s.replace(1, 3, s, 2, 3);
abccef
acdef
acdecf
acdeef
54
What does the following code print?
int a = 1, b = 2;
int& r = a;
int* p = &a;
r = b;
p = &b;
*p = 5;
std::cout << a << ',' << b << ',' << r;
2,2,2
2,5,2
5,5,5
1,5,5
55
Which function template can accept int matrix[2][3] without a cast while retaining both array extents at compile time?
void process(int** matrix);
void process(int matrix[][]);
template<std::size_t R, std::size_t C> void process(int (&matrix)[R][C]);
void process(int* matrix[3]);
56
A class has the member int data[2][3];. Which member-function declaration returns a pointer to the entire selected row, preserving its three-element array type?
int* row(std::size_t i) { return data[i]; }
int** row(std::size_t i) { return &data[i]; }
int (&row(std::size_t i))[2] { return data[i]; }
int (*row(std::size_t i))[3] { return &data[i]; }
57
What does this code print?
struct B { int x = 1; };
struct D : B { int y = 2; };
int B::* pb = &B::x;
int D::* pd = pb;
D d;
d.*pd = 7;
std::cout << d.x << d.y;
17
27
72
12
58
What does the following code print?
int a[] = {10, 20, 30, 40, 50};
int* p = a + 5;
std::ptrdiff_t n = p - a;
--p;
std::cout << n << ':' << *p;
4:50
5:40
4:40
5:50
59
For the owning class below, which move operations correctly transfer ownership without leaking or double-deleting the allocation?
class Owner {
int* p;
public:
explicit Owner(int v) : p(new int(v)) {}
~Owner() { delete p; }
Owner(const Owner&) = delete;
Owner& operator=(const Owner&) = delete;
};
Owner(Owner&& o) noexcept : p(std::exchange(o.p, nullptr)) {} Owner& operator=(Owner&& o) noexcept { if (this != &o) { delete p; p = std::exchange(o.p, nullptr); } return *this; }
Owner(Owner&& o) : p(o.p) { o.p = nullptr; } Owner& operator=(Owner&& o) { delete p; p = o.p; return *this; }
Owner(Owner&& o) : p(std::exchange(o.p, nullptr)) {} Owner& operator=(Owner&& o) { p = std::exchange(o.p, nullptr); return *this; }
Owner(Owner&& o) : p(o.p) {} Owner& operator=(Owner&& o) { p = o.p; return *this; }
60
What does this code print?
std::string s = "abcdef";
auto it = s.erase(s.begin() + 1, s.begin() + 4);
std::cout << *it << s;
faef
eaef
eabef
daef
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →