Unit 2: Pointers, Reference Variables, Arrays and String Concepts - Practice Quiz

CSE202 — Object Oriented Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 What must usually be done before dereferencing a void* pointer in C++?

Void pointer Easy
A. Cast it to an appropriate pointer type
B. Assign it to the nullptr value
C. Convert it into a reference variable
D. Increment it by exactly one byte

2 If p is an int*, what does p + 1 point to?

Pointer arithmetic Easy
A. The same integer element
B. The next integer element
C. The next memory byte
D. The previous integer element

3 Which declaration creates a pointer to a pointer to an integer?

Pointer to pointer Easy
A. int p[2];
B. int **p;
C. int &p;
D. int *p;

4 When does a pointer commonly become a dangling pointer?

Dangling pointer Easy
A. After it is compared with nullptr
B. After its dynamic memory is deleted
C. After it is assigned a valid address
D. After it points to an array element

5 What is a wild pointer?

Wild pointer Easy
A. A pointer to a pointer
B. A pointer assigned nullptr
C. An uninitialized pointer
D. A pointer to a constant

6 Which statement is the preferred modern C++ way to assign a null value to an integer pointer?

Null pointer assignment Easy
A. int *p = nullptr;
B. int *p = void;
C. int *p = &p;
D. int *p = new int;

7 Which class member function is commonly used to release dynamic memory owned by an object?

Classes containing pointers Easy
A. The constructor
B. The accessor
C. The destructor
D. The iterator

8 Which operator is normally used to access a member through a pointer to an object?

Pointer to objects Easy
A. &
B. ::
C. .
D. ->

9 Inside a non-static member function, what does the this pointer refer to?

this pointer Easy
A. The next object
B. The parent class
C. The current object
D. The first data member

10 Which statement declares an array containing five Student objects?

Array of objects Easy
A. Student students = 5;
B. Student students[5];
C. Student *students[0];
D. Student students(5);

11 Which statement correctly defines a C++ string object containing Hello?

Defining and assigning Standard C++ string objects Easy
A. string::std s = "Hello";
B. std::string = s("Hello");
C. std::string s = 'Hello';
D. std::string s = "Hello";

12 Which std::string member function returns the number of characters in a string?

String class member functions Easy
A. find()
B. size()
C. clear()
D. append()

13 Which std::string member function adds characters to the end of an existing string?

String class modifiers Easy
A. compare()
B. find()
C. append()
D. length()

14 Which statement correctly describes a basic difference between pointers and references?

Differences between pointer and reference variables Easy
A. A pointer must be initialized when declared
B. A reference can always be assigned nullptr
C. A pointer can be reassigned to another address
D. A reference uses * for normal access

15 Which statement declares a two-dimensional integer array with 3 rows and 4 columns?

Declaration and processing of multidimensional arrays inside main and classes Easy
A. int a[3, 4];
B. int a[3][4];
C. int a(3)(4);
D. int a[12, 1];

16 For int matrix[2][3];, which expression accesses the element in the second row and third column?

Declaration and processing of multidimensional arrays inside main and classes Easy
A. matrix[1][2]
B. matrix[1][3]
C. matrix[2][2]
D. matrix[2][3]

17 Assuming Student has a public integer member named marks, which statement declares a pointer to that data member?

Pointer to data member Easy
A. int Student::p* = &Student::marks;
B. int Student::*p = &Student::marks;
C. int *Student::p = &Student::marks;
D. int *p = Student::marks;

18 What kind of address can a void* generally store?

Void pointer Easy
A. Only a character object's address
B. An address of any object type
C. Only an integer object's address
D. Only another void pointer's address

19 Which std::string member function removes all characters from a string?

String class modifiers Easy
A. clear()
B. empty()
C. find()
D. size()

20 If objPtr is a pointer to a Book object, which expression calls its display() member function?

Pointer to objects Easy
A. objPtr.display();
B. objPtr->display();
C. objPtr::display();
D. objPtr&display();

21 What does the following C++ code print?

CPP
int value = 25;
void* vp = &value;
int* ip = static_cast<int*>(vp);
std::cout << *ip;

Void pointer Medium
A. A compilation error
B. The address of value
C. 25
D. An undefined value

22 What is the output of the following code?

CPP
int a[] = {10, 20, 30, 40};
int* p = a;
p += 2;
std::cout << *(p - 1);

Pointer arithmetic Medium
A. 40
B. 10
C. 20
D. 30

23 What does the following program fragment display?

CPP
int x = 4;
int* p = &x;
int** pp = &p;
**pp += 3;
std::cout << x;

Pointer to pointer Medium
A. 7
B. 3
C. The address of x
D. 4

24 Consider the following code:

CPP
std::vector<int> values = {1, 2};
int* p = &values[0];
values.push_back(3);



Which statement about p is correct after push_back?

Dangling pointer Medium
A. It points to the newly added element
B. It automatically changes to nullptr
C. It always points to the first element
D. It may dangle if the vector reallocates

25 Which change correctly prevents p from being a wild pointer before it is dereferenced?

CPP
int* p;
*p = 10;

Wild pointer Medium
A. Call delete p; before assignment
B. Declare int* p = nullptr; only
C. Declare int x; int* p = &x;
D. Cast p to a void* first

26 Given the overloaded functions below, which function is selected by the call?

CPP
void select(int);
void select(int*);

select(nullptr);

Null pointer assignment Medium
A. Both overloads are ambiguous
B. select(int*)
C. Neither overload is valid
D. select(int)

27 What is printed before the program ends?

CPP
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;
}

Classes containing pointers Medium
A. 9
B. A compilation error
C. 5
D. A null pointer value

28 What is the output of the following code?

CPP
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();

Pointer to objects Medium
A. 5
B. 15
C. 10
D. 50

29 What does the following code display?

CPP
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();

this pointer Medium
A. 4
B. 9
C. 7
D. 5

30 What is printed by the following program fragment?

CPP
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();

Array of objects Medium
A. 6
B. 5
C. 4
D. 9

31 What is the output of this code?

CPP
std::string a(4, 'x');
std::string b = a;
b[1] = 'A';
std::cout << a << " " << b;

Defining and assigning Standard C++ string objects Medium
A. xAxx xxxx
B. xxxx xxxx
C. xxxx xAxx
D. xAxx xAxx

32 What value is assigned to pos?

CPP
std::string text = "cat scatter cat";
std::size_t pos = text.find("cat", 1);

String class member functions Medium
A. 12
B. 4
C. 0
D. 5

33 What is the final value of s?

CPP
std::string s = "abcdef";
s.erase(2, 2);
s.insert(2, "XY");

String class modifiers Medium
A. abXYef
B. abXYcd
C. abcdXY
D. XYabef

34 What does the following code print?

CPP
int a = 3, b = 7;
int* p = &a;
int& r = a;
p = &b;
r = b;
std::cout << a << " " << b << " " << *p << " " << r;

Differences between pointer and reference variables Medium
A. 7 3 3 7
B. 3 7 3 7
C. 3 7 7 3
D. 7 7 7 7

35 What is the output of the following code?

CPP
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int (*p)[3] = a;
std::cout << *(*(p + 1) + 2);

Declaration and processing of multidimensional arrays inside main and classes Medium
A. 3
B. 4
C. 5
D. 6

36 What does g.sumColumn(1) return?

CPP
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;

Declaration and processing of multidimensional arrays inside main and classes Medium
A. 8
B. 7
C. 10
D. 5

37 What is printed by the following code?

CPP
struct Sample {
    int x;
};

Sample s{8};
int Sample::*pm = &Sample::x;
s.*pm = 11;
std::cout << s.x;

Pointer to data member Medium
A. 8
B. The address of x
C. A compilation error
D. 11

38 Which function declaration can directly receive a built-in array declared as int table[3][4]?

Declaration and processing of multidimensional arrays inside main and classes Medium
A. void process(int a[4][], int rows);
B. void process(int** a, int rows);
C. void process(int a[][], int rows);
D. void process(int a[][4], int rows);

39 Which expression correctly accesses value through both an object pointer and a pointer to data member?

CPP
struct Node { int value; };
Node n{20};
Node* p = &n;
int Node::*pm = &Node::value;

Pointer to data member Medium
A. *p.pm
B. p->*pm
C. p->pm
D. p.*pm

40 What is printed by the following code?

CPP
int a[6] = {};
int* p = &a[1];
int* q = &a[5];
std::cout << q - p;

Pointer arithmetic Medium
A. 5
B. 3
C. 6
D. 4

41 Under standard C++17 rules, which operation on vp is well-formed?

int x = 42;
void* vp = &x;

Void pointer Hard
A. int value = *static_cast<int*>(vp);
B. double* q = vp;
C. int value = *vp;
D. 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);

Pointer arithmetic Hard
A. 22
B. 12
C. 21
D. 20

43 Given int** pp, which implicit initialization is valid in standard C++?

Pointer to pointer Hard
A. const int** q = pp;
B. double** q = pp;
C. const int* const* q = pp;
D. void** 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();

Dangling pointer Hard
A. v[1] = 9;
B. v.pop_back();
C. v.reserve(oldCapacity + 1);
D. 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?

Wild pointer Hard
A. All three pointer members are null pointers.
B. a.p is null, while b.p and c.p are indeterminate.
C. a.p is indeterminate, while b.p and c.p are null.
D. 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);

Null pointer assignment Hard
A. f(nullptr);
B. f(0L);
C. f(false);
D. 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?

Classes containing pointers Hard
A. Default copy constructor and default copy assignment
B. Deleted copy constructor and deleted copy assignment
C. Deep-copy constructor and default copy assignment
D. Deep-copy constructor and deep-copy assignment

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;

Pointer to objects Hard
A. Cast p to void* before deletion.
B. Declare Base::~Base() as virtual.
C. Declare Derived::~Derived() as virtual.
D. Replace 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;

this pointer Hard
A. 222
B. 343
C. 233
D. 231

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();

Array of objects Hard
A. It is ill-formed because Derived* cannot convert to Base*.
B. It is undefined because base-pointer arithmetic does not traverse a derived array.
C. It is well-defined and prints 2.
D. It is well-defined and prints 1.

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);

Defining and assigning Standard C++ string objects Hard
A. 2, 2, 2
B. 5, 5, 5
C. 5, 5, 3
D. 2, 5, 3

52 What pair is printed by this code?

std::string s = "bananana";
std::cout << s.find("ana", 2) << ','
<< s.rfind("ana", 4);

String class member functions Hard
A. 5,3
B. 1,3
C. 3,3
D. 3,5

53 What is the value of s after the operation below?

std::string s = "abcdef";
s.replace(1, 3, s, 2, 3);

String class modifiers Hard
A. abccef
B. acdecf
C. acdef
D. 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;

Differences between pointer and reference variables Hard
A. 2,2,2
B. 5,5,5
C. 2,5,2
D. 1,5,5

55 Which function template can accept int matrix[2][3] without a cast while retaining both array extents at compile time?

Declaration and processing of multidimensional arrays inside main and classes Hard
A. void process(int matrix[][]);
B. template<std::size_t R, std::size_t C> void process(int (&matrix)[R][C]);
C. void process(int* matrix[3]);
D. void process(int** matrix);

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?

Declaration and processing of multidimensional arrays inside main and classes Hard
A. int (&row(std::size_t i))[2] { return data[i]; }
B. int (*row(std::size_t i))[3] { return &data[i]; }
C. int** row(std::size_t i) { return &data[i]; }
D. int* row(std::size_t i) { 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;

Pointer to data member Hard
A. 27
B. 12
C. 17
D. 72

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;

Pointer arithmetic Hard
A. 4:50
B. 5:50
C. 5:40
D. 4:40

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;
};

Classes containing pointers Hard
A. Owner(Owner&& o) : p(o.p) {} Owner& operator=(Owner&& o) { p = o.p; return *this; }
B. Owner(Owner&& o) : p(std::exchange(o.p, nullptr)) {} Owner& operator=(Owner&& o) { p = std::exchange(o.p, nullptr); return *this; }
C. Owner(Owner&& o) : p(o.p) { o.p = nullptr; } Owner& operator=(Owner&& o) { delete p; p = o.p; return *this; }
D. 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; }

60 What does this code print?

std::string s = "abcdef";
auto it = s.erase(s.begin() + 1, s.begin() + 4);
std::cout << *it << s;

String class modifiers Hard
A. eabef
B. eaef
C. faef
D. daef