C++ OOPs Concepts Test 2

    Question 1C++ OOPS - Access Specifiers

    By default, members of a C++ class are:

    Question 2C++ OOPS - Parameterized Constructor

    If `class Box` has a constructor `Box(int h, int w)`, how do we correctly create an object?

    Question 3C++ OOPS - Example of Encapsulation

    If age is a private member of class Person, how should it be accessed?

    Question 4C++ OOPS - Access Specifier in Inheritance

    If a base class is inherited as private, what happens to its public members in the derived class?

    Question 5C++ OOPS - Function Overloading

    What will the following code output?

    #include<iostream> using namespace std; void show(int x) { cout << "Int"; } void show(double y) { cout << "Double"; } int main() { show(5.5); }

    Question 6C++ OOPS Interface through Abstract Class

    How can abstraction be achieved in C++?

    Question 7C++ OOPS Friend Function Example

    What will be the output of this code?

    #include <iostream> using namespace std; class Box { int length; public: Box() { length = 10; } friend void printLength(Box); }; void printLength(Box b) { cout << b.length; } int main() { Box b; printLength(b); }

    Question 8C++ OOPS Static Variable in Class

    What will be the output?

    #include <iostream> using namespace std; class Test { static int count; public: Test() { count++; } static int getCount() { return count; } }; int Test::count = 0; int main() { Test a, b, c; cout << Test::getCount(); }

    Question 9C++ OOPS Unary Operator Overloading Example

    What will be the output?

    #include <iostream> using namespace std; class Counter { int value; public: Counter(int v) : value(v) {} void operator++() { value++; } void display() { cout << value; } }; int main() { Counter c(5); ++c; c.display(); }

    Question 10C++ OOPS Virtual Function Example

    What will be the output?

    #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 11C++ OOPS - Member Function Declaration

    Where can member functions of a class be defined?

    Question 12C++ OOPS - Constructor Overloading

    What does constructor overloading in C++ mean?

    Question 13C++ OOPS - Benefit of Encapsulation

    Which of the following is NOT a benefit of encapsulation?

    Question 14C++ OOPS - Code Example Single Inheritance

    What will be the output of the following code?

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

    Question 15C++ OOPS - Operator Overloading

    Which operator cannot be overloaded in C++?

    Question 16C++ OOPS Pure Virtual Function Example

    What will be the output of the following code?

    #include <iostream> using namespace std; class Shape { public: virtual void draw() = 0; }; class Circle : public Shape { public: void draw() { cout << "Drawing Circle"; } }; int main() { Circle c; c.draw(); }

    Question 17C++ OOPS Friend Function and Encapsulation

    Does declaring a function as a friend violate encapsulation?

    Question 18C++ OOPS Static Member Initialization

    Where must static data members of a class be defined?

    Question 19C++ OOPS Binary Operator Overloading

    Which function signature correctly overloads the binary + operator inside a class Complex?

    Question 20C++ OOPS Non-Virtual Function Call

    What happens if virtual is removed in the above code?