C++ OOPs Concepts Test 5

    Question 1C++ OOPS - Object Initialization

    Which of the following initializes an object car of class Car with a constructor taking two arguments?

    Question 2C++ OOPS - Order of Execution

    What is the order of execution for constructors and destructors in nested objects?

    Question 3C++ OOPS - Encapsulation Security

    Why does encapsulation improve security?

    Question 4C++ OOPS - Protected Members in Inheritance

    If a member is declared protected in the base class, how is it inherited in a public derived class?

    Question 5C++ OOPS - Function Overriding

    What will this print?

    #include<iostream> using namespace std; class A { public: virtual void show(){ cout<<"A"; } }; class B: public A { public: void show(){ cout<<"B"; } }; int main(){ B obj; obj.show(); }

    Question 6C++ OOPS Abstract Class Pointer

    What will be the output?

    #include <iostream> using namespace std; class Base { public: virtual void show() = 0; }; class Derived : public Base { public: void show() { cout << "Derived"; } }; int main() { Base* b = new Derived(); b->show(); }

    Question 7C++ OOPS Operator Overloading with Friend Function

    What will be the output?

    #include <iostream> using namespace std; class Complex { int real; public: Complex(int r) : real(r) {} friend Complex operator+(Complex c1, Complex c2); void show() { cout << real; } }; Complex operator+(Complex c1, Complex c2) { return Complex(c1.real + c2.real); } int main() { Complex a(3), b(4); Complex c = a + b; c.show(); }

    Question 8C++ OOPS Static Member Access

    Which of the following is the correct way to access a static variable of a class `Sample` named `count`?

    Question 9C++ OOPS Overloading [] Operator

    Which statement about overloading [] is correct?

    Question 10C++ OOPS V-Table and Object Size

    How does adding virtual functions affect object size?

    Question 11C++ OOPS - Object Lifetime

    When is a destructor of a class called automatically?

    Question 12C++ OOPS - Destructor Call

    When is a destructor called for an automatic (stack) object?

    Question 13C++ OOPS - Real-life Example of Encapsulation

    Which of the following is a real-world analogy for encapsulation?

    Question 14C++ OOPS - Function Overriding Code

    What will the following code output?

    #include<iostream> using namespace std; class Base { public: void show() { cout << "Base"; } }; class Derived : public Base { public: void show() { cout << "Derived"; } }; int main() { Derived d; d.show(); }

    Question 15C++ OOPS - Runtime Polymorphism Example

    What is the output?

    #include<iostream> using namespace std; class Animal { public: virtual void sound() { cout << "Animal"; } }; class Dog : public Animal { public: void sound() { cout << "Dog"; } }; int main() { Animal* a = new Dog(); a->sound(); }

    Question 16C++ OOPS Real-life Example of Abstraction

    Which is the best real-life example of abstraction in C++?

    Question 17C++ OOPS Limitations of Friend Functions

    Which of the following is NOT true about friend functions in C++?

    Question 18C++ OOPS Static vs Non-Static Member

    What will be the output?

    #include <iostream> using namespace std; class Alpha { static int x; int y; public: Alpha() { y = 5; } static void printX() { cout << x; } void printY() { cout << y; } }; int Alpha::x = 100; int main() { Alpha::printX(); Alpha a; a.printY(); }

    Question 19C++ OOPS Operator Overloading Example (Mix)

    What will be the output?

    #include <iostream> using namespace std; class Point { int x; public: Point(int v) : x(v) {} Point operator-(Point p) { return Point(x - p.x); } void show() { cout << x; } }; int main() { Point p1(10), p2(3); Point p3 = p1 - p2; p3.show(); }

    Question 20C++ OOPS Virtual Function Override Example

    What will be the output?

    #include <iostream> using namespace std; class A { public: virtual void f() { cout << "A"; } }; class B : public A { public: void f() { cout << "B"; } }; class C : public B { public: void f() { cout << "C"; } }; int main() { A* p = new C(); p->f(); }