adplus-dvertising

Welcome to the Class Hierarchies,Library and Containers MCQs Page

Dive deep into the fascinating world of Class Hierarchies,Library and Containers with our comprehensive set of Multiple-Choice Questions (MCQs). This page is dedicated to exploring the fundamental concepts and intricacies of Class Hierarchies,Library and Containers, a crucial aspect of Object Oriented Programming Using C++. In this section, you will encounter a diverse range of MCQs that cover various aspects of Class Hierarchies,Library and Containers, from the basic principles to advanced topics. Each question is thoughtfully crafted to challenge your knowledge and deepen your understanding of this critical subcategory within Object Oriented Programming Using C++.

frame-decoration

Check out the MCQs below to embark on an enriching journey through Class Hierarchies,Library and Containers. Test your knowledge, expand your horizons, and solidify your grasp on this vital area of Object Oriented Programming Using C++.

Note: Each MCQ comes with multiple answer choices. Select the most appropriate option and test your understanding of Class Hierarchies,Library and Containers. You can click on an option to test your knowledge before viewing the solution for a MCQ. Happy learning!

Class Hierarchies,Library and Containers MCQs | Page 6 of 15

Discuss
Answer: (a).It combines the first operand and the second operand
Q52.
Which is used to allocate and deallocate storage for objects during the execution?
Discuss
Answer: (c).Freestore
Q53.
Which operators are used in the free store?
Discuss
Answer: (c).both new & delete
Q54.
What type of class member is operator new?
Discuss
Answer: (a).static
Q55.
What is the output of this program?
    #include <new>
    #include <iostream>
    using namespace std;
    struct A 
    {
        virtual ~A() {  };
        void operator delete(void* p) 
        {
            cout << "A :: operator delete" << endl;
        }
    };
    struct B : A 
    {
        void operator delete(void* p) 
        {
            cout << "B :: operator delete" << endl;
        }
    };
    int main() 
    {
        A* ap = new B;
        delete ap;
    }
Discuss
Answer: (b).B::operator delete
Q56.
What is the output of this program?
    #include <iostream>
    using namespace std;
    struct A
    {
        virtual ~A()
        { 
            cout << "~A()" << endl; 
        }
        void operator delete[](void* p, size_t)
        {
            cout << "A :: operator delete[]" << endl;
            delete [] p;
        }
    };
    struct B : A 
    {
        void operator delete[](void* p, size_t) 
        {
            cout << "B :: operator delete[]" << endl;
            delete [] p;
        }
    };
    int main() 
    {
        A* bp = new B[3];
        delete[] bp;
    };
Discuss
Answer: (d).Warning
Q57.
What is the output of this program?
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    class X 
    {
        public:
        void* operator new(size_t sz) throw (const char*)
        {
            void* p = malloc(sz);
            if (p == 0) 
                throw "malloc() failed";
            return p;
        }
        void operator delete(void* p) 
        {
            cout << "X :: operator delete(void*)" << endl;
            free(p);
        } 
    };
    class Y 
    {
        int filler[100];
        public:
        void operator delete(void* p, size_t sz) throw (const char*)
        {
            cout << "Freeing " << sz << " bytes" << endl;
            free(p);
        };
    };
    int main() 
    {
        X* ptr = new X;
        delete ptr;
        Y* yptr = new Y;
        delete yptr;
    }
Discuss
Answer: (d).Both X::operator delete(void*) & Depends on the compiler
Q58.
What is the output of this program?
    #include <new>
    #include<cstdlib>
    #include <iostream>
    using namespace std;
    class X;
    struct Node 
    {
        X* data;
        bool filled;
        Node() : filled(false) { }
    };
    class X 
    {
        static Node buffer[];
        public:
        int number;
        enum { size = 3};
        void* operator new(size_t sz) throw (const char*)
        {
            void* p = malloc(sz);
            if (sz == 0)
                throw "Error: malloc() failed";
            cout << "X :: operator new(size_t)" << endl;
            return p;
        }
        void *operator new(size_t sz, int location) throw (const char*) 
        {
            cout << "X :: operator new(size_t, " << location << ")" << endl;
            void* p = 0;
            if (location < 0 || location >= size || buffer[location].filled == true)
            {
                throw "Error: buffer location occupied";
            }
            else 
            {
                p = malloc(sizeof(X));
                if (p == 0) 
                    throw "Error: Creating X object failed";
                buffer[location].filled = true;
                buffer[location].data = (X*) p;
            }
            return p;
        }
        static void printbuffer() 
        {
            for (int i = 0; i < size; i++) 
            {
                cout << buffer[i].data->number << endl;
            }
        } 
    };
    Node X::buffer[size];
    int main()
    {
        try 
        {
            X* ptr1 = new X;
            X* ptr2 = new(0) X;
            X* ptr3 = new(1) X;
            X* ptr4 = new(2) X;
            ptr2->number = 10000;
            ptr3->number = 10001;
            ptr4->number = 10002;
            X :: printbuffer();
            X* ptr5 = new(0) X;
        }
        catch (const char* message) 
        {
            cout << message << endl;
        }
    }
Discuss
Answer: (c).Runtime error
Q59.
What is the output of this program?
    #include <iostream>
    #include <new>
    #include <cstdlib>
    using namespace std;
    const int bsize = 512;
    int *pa;
    bool allocate = true;
    void get_memory() 
    {
        cerr << "free store exhausted" << endl;
        delete [] pa;
        allocate = false;
    }
    void eat_memory(int size) 
    {
        int *p = new int[size];
        if (allocate)
            eat_memory(size);
        else
            cerr << "free store addr = " << p << endl;
    }
    int main()
    {
        set_new_handler(get_memory);
        pa = new int[bsize];
        cerr << "free store addr = " << pa << endl;
        eat_memory(bsize);
        return 0;
    }
Discuss
Answer: (c).Segmentation fault
Q60.
What must be an operand of operator delete?
Discuss
Answer: (a).Pointer
Page 6 of 15

Suggested Topics

Are you eager to expand your knowledge beyond Object Oriented Programming Using C++? We've curated a selection of related categories that you might find intriguing.

Click on the categories below to discover a wealth of MCQs and enrich your understanding of Computer Science. Happy exploring!