Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
You're offline
Unit 2 MCQsPointers, Reference Variables, Arrays and String Concepts
nullptr has type std::nullptr_t and converts to pointer types. It does not implicitly convert to int, so the pointer overload is selected.
Question 27 of 60 · Classes containing pointers · Medium
What is printed before the program ends?
C++
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;
}
Not quite. Try another option.
Answer: 9
The compiler-generated copy constructor performs a shallow copy, so both objects contain the same pointer. Changing the value through b is visible through a.
Question 28 of 60 · Pointer to objects · Medium
What is the output of the following code?
C++
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();
Not quite. Try another option.
Answer: 15
The -> operator accesses members through an object pointer. Calling add(5) changes the balance from 10 to 15.
Question 29 of 60 · this pointer · Medium
What does the following code display?
C++
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();
Not quite. Try another option.
Answer: 9
this->value refers to the data member, and returning *this allows method chaining. The value becomes 2 + 3 + 4 = 9.
Question 30 of 60 · Array of objects · Medium
What is printed by the following program fragment?
C++
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();
Not quite. Try another option.
Answer: 6
items[0] contains 1, while items[2] contains 5. Their sum is 6.
Question 31 of 60 · Defining and assigning Standard C++ string objects · Medium
What is the output of this code?
C++
std::string a(4, 'x');
std::string b = a;
b[1] = 'A';
std::cout << a << " " << b;
Not quite. Try another option.
Answer: xxxx xAxx
std::string b = a creates an independent copy. Modifying b[1] does not change a.
Question 32 of 60 · String class member functions · Medium
The search begins at index 1, so the occurrence at index 0 is skipped. The next complete occurrence of "cat" starts at index 12.
Question 33 of 60 · String class modifiers · Medium
What is the final value of s?
C++
std::string s = "abcdef";
s.erase(2, 2);
s.insert(2, "XY");
Not quite. Try another option.
Answer: abXYef
erase(2, 2) removes c and d, producing "abef". Inserting "XY" at index 2 produces "abXYef".
Question 34 of 60 · Differences between pointer and reference variables · Medium
What does the following code print?
C++
int a = 3, b = 7;
int* p = &a;
int& r = a;
p = &b;
r = b;
std::cout << a << " " << b << " " << *p << " " << r;
Not quite. Try another option.
Answer: 7 7 7 7
The pointer is reassigned to b. The reference cannot be reseated, so r = b assigns b's value to a, making all displayed values 7.
Question 35 of 60 · Declaration and processing of multidimensional arrays inside main and classes · Medium
What is the output of the following code?
C++
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int (*p)[3] = a;
std::cout << *(*(p + 1) + 2);
Not quite. Try another option.
Answer: 6
p + 1 points to the second row. Moving two elements within that row reaches a[1][2], which contains 6.
Question 36 of 60 · Declaration and processing of multidimensional arrays inside main and classes · Medium
What does g.sumColumn(1) return?
C++
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;
Not quite. Try another option.
Answer: 7
The array becomes {{1, 2, 3}, {4, 5, 6}}. Column 1 contains 2 and 5, so the method returns 7.
Question 37 of 60 · Pointer to data member · Medium
What is printed by the following code?
C++
struct Sample {
int x;
};
Sample s{8};
int Sample::*pm = &Sample::x;
s.*pm = 11;
std::cout << s.x;
Not quite. Try another option.
Answer: 11
pm is a pointer to the x data member of Sample. The .* operator applies it to s, allowing s.x to be assigned 11.
Question 38 of 60 · Declaration and processing of multidimensional arrays inside main and classes · Medium
Which function declaration can directly receive a built-in array declared as int table[3][4]?
Not quite. Try another option.
Answer: void process(int a[][4], int rows);
For a multidimensional array parameter, all dimensions except the first must be specified. An int[3][4] also does not decay to int**.
Question 39 of 60 · Pointer to data member · Medium
Which expression correctly accesses value through both an object pointer and a pointer to data member?
C++
struct Node { int value; };
Node n{20};
Node* p = &n;
int Node::*pm = &Node::value;
Not quite. Try another option.
Answer: p->*pm
The ->* operator applies a pointer to data member to a pointer to an object. Thus, p->*pm accesses n.value.
Question 40 of 60 · Pointer arithmetic · Medium
What is printed by the following code?
C++
int a[6] = {};
int* p = &a[1];
int* q = &a[5];
std::cout << q - p;
Not quite. Try another option.
Answer: 4
Subtracting pointers into the same array gives the number of array elements between them. The difference is .
Question 41 of 60 · Void pointer · Hard
Under standard C++17 rules, which operation on vp is well-formed?
int x = 42; void* vp = &x;
Not quite. Try another option.
Answer: int value = *static_cast<int*>(vp);
A void* must be explicitly converted to an appropriate object-pointer type before dereferencing. Standard C++ does not allow dereferencing or arithmetic on void*, and conversion from void* to another object pointer is not implicit.
Question 42 of 60 · Pointer arithmetic · Hard
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);
Not quite. Try another option.
Answer: 21
p + 2 points to row 2, and adding 1 after dereferencing selects column 1. Thus the expression accesses a[2][1], whose value is .
Question 43 of 60 · Pointer to pointer · Hard
Given int** pp, which implicit initialization is valid in standard C++?
Not quite. Try another option.
Answer: const int* const* q = pp;
The added intermediate const prevents replacing an inner pointer with one that could violate const-correctness. Direct conversion from int** to const int** is rejected because it could otherwise permit modification of a const object.
Question 44 of 60 · Dangling pointer · Hard
After which statement is p guaranteed to be dangling?
Requesting a capacity greater than the current capacity forces reallocation, invalidating all pointers and iterators into the vector. The other operations do not invalidate the pointer to the first element in this example.
Question 45 of 60 · Wild pointer · Hard
Consider the following declarations:
struct Holder { int* p; Holder() {} };
static Holder a; Holder b; Holder c{};
Which statement is correct immediately after construction?
Not quite. Try another option.
Answer: a.p is null, while b.p and c.p are indeterminate.
a has static storage duration and is zero-initialized before its constructor runs, so a.p is null. The user-provided constructor does not initialize p for the automatic objects, leaving b.p and c.p indeterminate.
Question 46 of 60 · Null pointer assignment · Hard
Given the overloads below, which call is unambiguous and invokes f(int*)?
void f(int*); void f(long);
Not quite. Try another option.
Answer: f(nullptr);
nullptr has type std::nullptr_t and converts to int*, but not to long. The call f(0) is ambiguous, while false and 0L select the integral overload.
Question 47 of 60 · Classes containing pointers · Hard
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?
Not quite. Try another option.
Answer: Deep-copy constructor and deep-copy assignment
Both copy operations must allocate separate storage and copy the elements. Default copying would duplicate only the pointer, causing shared ownership and likely double deletion.
Question 48 of 60 · Pointer to objects · Hard
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;
Not quite. Try another option.
Answer: Declare Base::~Base() as virtual.
Deleting a derived object through a base pointer requires a virtual base destructor. Without it, the behavior is undefined because the complete derived object is not destroyed correctly.
Question 49 of 60 · this pointer · Hard
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;
Not quite. Try another option.
Answer: 231
[*this] captures a copy of the current object. The mutable lambda changes its copied x from 1 to 2 and then 3, while the original s.x remains 1.
Question 50 of 60 · Array of objects · Hard
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();
Not quite. Try another option.
Answer: It is undefined because base-pointer arithmetic does not traverse a derived array.
The conversion to Base* refers to the base subobject of the first Derived. Pointer arithmetic on that pointer cannot be used to locate the base subobject of the next Derived, so dereferencing p + 1 is undefined.
Question 51 of 60 · Defining and assigning Standard C++ string objects · Hard
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);
Not quite. Try another option.
Answer: 2, 5, 3
Construction from a C-string stops at the first null character, so the first size is 2. The count-based constructor stores all five characters, and assign(pointer, 3) stores exactly three characters, including the embedded null.
Question 52 of 60 · String class member functions · Hard
find starts at index 2, so it finds the occurrence beginning at index 3. rfind searches backward for an occurrence beginning no later than index 4, which is also the occurrence at index 3.
Question 53 of 60 · String class modifiers · Hard
What is the value of s after the operation below?
std::string s = "abcdef"; s.replace(1, 3, s, 2, 3);
Not quite. Try another option.
Answer: acdeef
The substring bcd at indices 1 through 3 is replaced by the source substring cde. The resulting string is the prefix a, followed by cde, followed by the original suffix ef.
Question 54 of 60 · Differences between pointer and reference variables · Hard
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;
Not quite. Try another option.
Answer: 2,5,2
r = b assigns b's value to a; it does not reseat the reference. The pointer is then redirected to b, so *p = 5 changes only b. The reference still denotes a.
Question 55 of 60 · Declaration and processing of multidimensional arrays inside main and classes · Hard
Which function template can accept int matrix[2][3] without a cast while retaining both array extents at compile time?
A reference to a two-dimensional array preserves both dimensions and allows their sizes to be deduced. int** is not compatible with a contiguous built-in two-dimensional array.
Question 56 of 60 · Declaration and processing of multidimensional arrays inside main and classes · Hard
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?
Not quite. Try another option.
Answer: int (*row(std::size_t i))[3] { return &data[i]; }
data[i] is an array of three integers, so &data[i] has type int (*)[3]. This preserves the row boundary rather than decaying the row to int*.
Question 57 of 60 · Pointer to data member · Hard
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;
Not quite. Try another option.
Answer: 72
A pointer to a data member of an unambiguous non-virtual base can convert to the corresponding derived-class member-pointer type. pd still identifies the inherited x, so x becomes 7 while y remains 2.
Question 58 of 60 · Pointer arithmetic · Hard
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;
Not quite. Try another option.
Answer: 5:50
a + 5 is the valid one-past-the-end pointer, so subtracting a gives 5. Decrementing it produces a + 4, which points to the final element, 50.
Question 59 of 60 · Classes containing pointers · Hard
For the owning class below, which move operations correctly transfer ownership without leaking or double-deleting the allocation?
The source pointer must be set to null, and move assignment must first release the destination's existing allocation. The self-assignment check also prevents destroying the resource during self-move.
Question 60 of 60 · String class modifiers · Hard
What does this code print?
std::string s = "abcdef"; auto it = s.erase(s.begin() + 1, s.begin() + 4); std::cout << *it << s;
Not quite. Try another option.
Answer: eaef
The half-open range removes b, c, and d, leaving aef. The returned iterator points to the element following the erased range, which is now e.
That's unit 2
Answer a few questions and your score shows up here.
Nearly through. Did unit 2 of CSE202 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 2 of CSE202 save you a night before the exam?
Then you can help keep it free for the next batch. 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.