Q: 1 What is virtual inheritance in C++?
C++ technique to enhance multiple inheritance.
C++ technique to ensure that a private member of the base class can be accessed somehow.
C++ technique to avoid multiple copies of the base class into children/derived class.
None of the above.
[ Option C ]
Virtual inheritance in C++ is used to solve the Diamond Problem that occurs in multiple inheritance. Without virtual inheritance, a derived class may receive multiple copies of the same base class due to different inheritance paths. To prevent this duplication, C++ allows a class to be inherited virtually.
This ensures that only one shared copy of the base class is passed to all derived classes, avoiding ambiguity and memory waste.
Diamond Problem:
When classes B and C both inherit from A, and D inherits from both B and C, D gets two copies of A's members.
A
/
B C
/
D
Q: 2 Which feature of OOP is indicated by the following code?
class student {int marks;);
class topper: public student{int age; topper (int age) {this.age=age;}};
Encapsulation and inheritance
Inheritance and polymorphism
Polymorphism
None of the above
[ Option A ]
The given code demonstrates two core features of Object-Oriented Programming: encapsulation and inheritance.
Encapsulation means binding data and functions together inside a class. In the given code, class student { int marks; };, the variable marks is inside the class student. This shows encapsulation, because the data is protected inside the class structure.
Inheritance means creating a new class from an existing class.
class topper : public student {
int age;
topper(int age) { this.age = age; }
};
The toppers is inheriting from the student class using public inheritance. So, the class topper automatically gets the properties of the student class.
Since the code does not use virtual functions or method overriding, it does not show polymorphism.
Q: 3 Which of the following can be considered as the members that can be inherited but not accessible in any class?
pubic
protected
private
More than one of the above
[ Option C ]
In C++, private members of a class cannot be accessed directly by derived classes, even though they are technically inherited. They exist in memory but remain inaccessible to ensure encapsulation.
Q: 4 Which concept of C++ is used to reuse the written code?
Polymorphism
Inheritance
Encapsulation
More than one of the above
[ Option B ]
One of the main goals of C++ is to make programming easier by allowing developers to reuse code instead of writing everything from scratch. The concept in C++ that supports this is inheritance.
Inheritance allows a new class (derived class or child class or sub class) to acquire the properties and functions of an existing class (base class or parent class or super class).
Q: 5 Which is correct syntax of inheritance?
class base_classname : access derived_classname { /* define class body */ };
class derived_classname : access base_classname { /* define class body */ };
class derived_classname : base_classname { /* define class body */ };
More than one of the above
[ Option D ]
In C++, inheritance means creating a new class (derived class) from an existing class (base class). The correct syntax places the derived class first, followed by a colon, then the access specifier (public, private, protected), and then the base class name. Option (b) correctly follows this format.
However, if you do not specify any access specifier, the compiler does not produce an error, instead it uses a default access mode based on the type of class. In C++, the default inheritance for a class is private. This means class Derived : Base { }; is valid and treated as private inheritance. Therefore, option (c) is also a correct syntax.
Q: 6 Inheritance in OOP allows a class to:
Inherit properties and behavior from another class.
Create instances of another class.
Override methods of another class.
None of the above
[ Option A ]
Inheritance allows a class to inherit properties and behavior from another class, enabling code reuse through the "is-a" relationship.
Q: 7 The number of access specifiers in class of C++ is
2
3
4
None of the above
[ Option B ]
In C++, access specifiers are keywords used inside a class to control how the members (data and functions) can be accessed from outside the class. They help in implementing the OOP concept called Data Hiding.
C++ PROVIDES THREE ACCESS SPECIFIERS:
| ACCESS SPECIFIER | ACCESSIBILITY |
|---|---|
| public | Fully accessible, from anywhere in the program. |
| private | Most restricted, only inside the same class. |
| protected | Limited access, inside the class and its derived classes. |
| Access Specifier | Same Class | Derived Class | Outside Class |
|---|---|---|---|
| public | Yes | Yes | Yes |
| private | Yes | No | No |
| protected | Yes | Yes | No |
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.