This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1What is virtual inheritance in C++?
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: 2Which 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;}};
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: 3In Object Oriented Programming (OOP), when a new class created from an existing class using principle of inheritance, then the new class is known as ______.
Option D
In Inheritance, one class acquires the properties and functions of another class.
The existing class whose properties are inherited is called:
The new class created from the existing class is called:
The derived class can use the features of the base class and can also add its own additional features.
#include<iostream>
using namespace std;
class Person // Base class
{
public:
void displayName()
{
cout<<"Name : Suresh Kulahry"<<endl;
}
};
class Student : public Person // Derived class
{
public:
void displayCourse()
{
cout<<"Course : MCA";
}
};
int main()
{
Student s1;
s1.displayName();
s1.displayCourse();
return 0;
}
Q: 4Which of the following can be considered as the members that can be inherited but not accessible in any class?
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: 5Which concept of C++ is used to reuse the written code?
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: 6Which is correct syntax of inheritance?
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: 7Inheritance in OOP allows a class to:
Option A
Inheritance allows a class to inherit properties and behavior from another class, enabling code reuse through the "is-a" relationship.
Q: 8The number of access specifiers in class of C++ is
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 |
Q: 9In object-oriented programming, private methods of a class ___________.
Option A
In Object-Oriented Programming (Java or C++), a private method is accessible only inside the class in which it is declared. It cannot be accessed directly from outside the class, even by subclasses.
Private Methods are not inherited in a way that allows access, and they cannot be overridden because they are not visible in derived classes.
You have reached the end of this topic. Continue learning with the next topic below.
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.