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.

Unit 2 MCQs Pointers, Reference Variables, Arrays and String Concepts

CSE202 — Object Oriented Programming 60 questions. Pick an option to check it.

Read the notes Subjective questions

0 of 60 answered 0 right 0 wrong , 60 left

  1. Question 1 of 60 Void pointer Easy

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

  2. Question 2 of 60 Pointer arithmetic Easy

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

  3. Question 3 of 60 Pointer to pointer Easy

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

  4. Question 4 of 60 Dangling pointer Easy

    When does a pointer commonly become a dangling pointer?

  5. Question 5 of 60 Wild pointer Easy

    What is a wild pointer?

  6. Question 6 of 60 Null pointer assignment Easy

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

  7. Question 7 of 60 Classes containing pointers Easy

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

  8. Question 8 of 60 Pointer to objects Easy

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

  9. Question 9 of 60 this pointer Easy

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

  10. Question 10 of 60 Array of objects Easy

    Which statement declares an array containing five Student objects?

  11. Question 11 of 60 Defining and assigning Standard C++ string objects Easy

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

  12. Question 12 of 60 String class member functions Easy

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

  13. Question 13 of 60 String class modifiers Easy

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

  14. Question 14 of 60 Differences between pointer and reference variables Easy

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

  15. Question 15 of 60 Declaration and processing of multidimensional arrays inside main and classes Easy

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

  16. Question 16 of 60 Declaration and processing of multidimensional arrays inside main and classes Easy

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

  17. Question 17 of 60 Pointer to data member Easy

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

  18. Question 18 of 60 Void pointer Easy

    What kind of address can a void* generally store?

  19. Question 19 of 60 String class modifiers Easy

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

  20. Question 20 of 60 Pointer to objects Easy

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

  21. Question 21 of 60 Void pointer Medium

    What does the following C++ code print?

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

  22. Question 22 of 60 Pointer arithmetic Medium

    What is the output of the following code?

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

  23. Question 23 of 60 Pointer to pointer Medium

    What does the following program fragment display?

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

  24. Question 24 of 60 Dangling pointer Medium

    Consider the following code:

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



    Which statement about p is correct after push_back?

  25. Question 25 of 60 Wild pointer Medium

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

    C++
    int* p;
    *p = 10;

  26. Question 26 of 60 Null pointer assignment Medium

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

    C++
    void select(int);
    void select(int*);
    
    select(nullptr);

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

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

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

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

  31. 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;

  32. Question 32 of 60 String class member functions Medium

    What value is assigned to pos?

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

  33. 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");

  34. 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;

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

  36. 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;

  37. 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;

  38. 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]?

  39. 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;

  40. 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;

  41. 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;

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

  43. Question 43 of 60 Pointer to pointer Hard

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

  44. Question 44 of 60 Dangling pointer Hard

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

  45. 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?

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

  47. 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?

  48. 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;

  49. 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;

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

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

  52. Question 52 of 60 String class member functions Hard

    What pair is printed by this code?

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

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

  54. 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;

  55. 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?

  56. 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?

  57. 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;

  58. 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;

  59. 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?

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

  60. 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;

That's unit 2

Answer a few questions and your score shows up here.