C++ OOPs Concepts Test 3

    Question 1C++ OOPS - Object Access

    Which operator is used to access class members via an object?

    Question 2C++ OOPS - Copy Constructor

    Which of the following correctly declares a copy constructor for class `Student`?

    Question 3C++ OOPS - Access Control

    What will happen if you try to directly access a private data member of a class?

    Question 4C++ OOPS - Multilevel Inheritance Code

    What will the following program print?

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

    Question 5C++ OOPS - Virtual Function

    What will this code print?

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

    Question 6C++ OOPS Abstract Class Instantiation

    What happens if you try to instantiate an abstract class in C++?

    Question 7C++ OOPS Friend Function Multiple Classes

    What will be the output?

    #include <iostream> using namespace std; class A { int x = 5; friend class B; }; class B { public: void show(A a) { cout << a.x; } }; int main() { A a; B b; b.show(a); }

    Question 8C++ OOPS Static Member Function Call

    How can a static member function be called?

    Question 9C++ OOPS Friend Function Operator Overloading

    What will be the output?

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

    Question 10C++ OOPS Pure Virtual Function & V-Table

    Which statement is true about pure virtual functions?

    Question 11C++ OOPS - Pointer to Object

    If Student *ptr; is a pointer to a Student object, how can we call its function show()?

    Question 12C++ OOPS - Implicit Copy Constructor

    When does the compiler provide a default copy constructor?

    Question 13C++ OOPS - Encapsulation & Abstraction

    How is encapsulation different from abstraction?

    Question 14C++ OOPS - Diamond Problem

    The "diamond problem" occurs in which type of inheritance?

    Question 15C++ OOPS - Non-virtual Function

    What happens if `virtual` is removed in the above code?

    Question 16C++ OOPS Abstraction with Multiple Derived Classes

    What will be the output of this code?

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

    Question 17C++ OOPS Friend Class Concept

    Which statement is true about friend classes in C++?

    Question 18C++ OOPS Static Member and Object Lifetime

    What happens to static members when all objects of the class are destroyed?

    Question 19C++ OOPS Operator Overloading Limitation

    Which of the following statements is NOT true?

    Question 20C++ OOPS V-Table Concept

    What is the V-Table in C++?