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. Increment it by exactly one byte
C. Assign it to the nullptr value
D. Convert it into a reference variable

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

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

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

Pointer to pointer Easy
A. int *p;
B. int p[2];
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. An uninitialized pointer
B. A pointer to a constant
C. A pointer assigned nullptr
D. A pointer to a pointer

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 = new int;
B. int *p = void;
C. int *p = &p;
D. int *p = nullptr;

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

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

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 current object
B. The first data member
C. The next object
D. The parent class

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. size()
B. find()
C. clear()
D. append()

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

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

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

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

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[12, 1];
B. int a[3][4];
C. int a[3, 4];
D. int a(3)(4);

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[2][3]
B. matrix[1][2]
C. matrix[1][3]
D. matrix[2][2]

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 *p = Student::marks;
B. int Student::p* = &Student::marks;
C. int Student::*p = &Student::marks;
D. int *Student::p = &Student::marks;

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

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

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

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

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. An undefined value
B. A compilation error
C. 25
D. The address of 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. 30
B. 20
C. 10
D. 40

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. 4
B. 7
C. The address of x
D. 3

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 always points to the first element
C. It automatically changes to nullptr
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. Cast p to a void* first
C. Declare int* p = nullptr; only
D. Declare int x; int* p = &x;

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. select(int)
B. Neither overload is valid
C. Both overloads are ambiguous
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. 5
B. A compilation error
C. A null pointer value
D. 9

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. 50
C. 10
D. 15

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. 7
B. 5
C. 4
D. 9

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. 4
C. 5
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 xAxx
B. xxxx xxxx
C. xAxx xxxx
D. xxxx 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. 0
C. 4
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. abcdXY
C. XYabef
D. abXYcd

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. 3 7 3 7
B. 7 7 7 7
C. 7 3 3 7
D. 3 7 7 3

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. 4
B. 5
C. 3
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. 10
B. 5
C. 8
D. 7

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. The address of x
B. 11
C. A compilation error
D. 8

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, int rows);
B. void process(int a[][4], 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. 3
B. 6
C. 4
D. 5

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. int value = *vp;
C. double* q = 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. 20
C. 21
D. 12

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

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

Dangling pointer Hard
A. v.pop_back();
B. v[1] = 9;
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(0L);
B. f(nullptr);
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. Deep-copy constructor and default copy assignment
B. Deleted copy constructor and deleted copy assignment
C. Default 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. Declare Base::~Base() as virtual.
B. Cast p to void* before deletion.
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. 231
C. 343
D. 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();

Array of objects Hard
A. It is well-defined and prints 2.
B. It is well-defined and prints 1.
C. It is undefined because base-pointer arithmetic does not traverse a derived array.
D. It is ill-formed because 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);

Defining and assigning Standard C++ string objects Hard
A. 2, 2, 2
B. 5, 5, 3
C. 2, 5, 3
D. 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);

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

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. acdef
C. acdecf
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. 2,5,2
C. 5,5,5
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. void process(int matrix[][]);
C. template<std::size_t R, std::size_t C> void process(int (&matrix)[R][C]);
D. 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?

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

Pointer to data member Hard
A. 17
B. 27
C. 72
D. 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;

Pointer arithmetic Hard
A. 4:50
B. 5:40
C. 4:40
D. 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;
};

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

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