0% found this document useful (0 votes)
3 views9 pages

Polymorphismandinheritance

The document contains a series of multiple-choice questions related to C++ programming concepts, specifically focusing on virtual functions, inheritance, polymorphism, and class behavior. Each question is followed by the correct answer, providing insights into key aspects of C++ programming. The questions cover topics such as function overriding, abstract classes, and the implications of using virtual destructors.

Uploaded by

shaurayavohra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Polymorphismandinheritance

The document contains a series of multiple-choice questions related to C++ programming concepts, specifically focusing on virtual functions, inheritance, polymorphism, and class behavior. Each question is followed by the correct answer, providing insights into key aspects of C++ programming. The questions cover topics such as function overriding, abstract classes, and the implications of using virtual destructors.

Uploaded by

shaurayavohra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1. What is the main purpose of virtual functions in C++?

A. To implement function overloading


B. To support compile-time polymorphism
C. To support runtime polymorphism
D. To allow operator overloading
Answer: C. To support runtime polymorphism

2. What will be the output of the following program?

class A {

public:

virtual void display() { cout << "Class A"; }

};

class B : public A {

public:

void display() override { cout << "Class B"; }

};

int main() {

A *ptr = new B();

ptr->display();

A. Class A
B. Class B
C. Compilation Error
D. Runtime Error
Answer: B. Class B

3. Which of the following is NOT a type of inheritance in C++?


A. Multiple
B. Multilevel
C. Distributive
D. Hierarchical
Answer: C. Distributive

4. If a base class has a virtual destructor, what does it ensure?


A. The object size increases
B. Only base class's destructor is called
C. Derived class's destructor is called properly
D. Constructors become virtual too
Answer: C. Derived class's destructor is called properly

5. What will the following code print?

class A {

public:

A() { cout << "A "; }

};

class B : public A {

public:

B() { cout << "B "; }

};

int main() {

B b;

A. B A
B. A B
C. AB
D. Compilation Error
Answer: B. A B

6. What kind of inheritance leads to the Diamond Problem in C++?


A. Single
B. Multilevel
C. Multiple
D. Hierarchical
Answer: C. Multiple

7. Which keyword is used to prevent a function from being overridden in derived classes?
A. static
B. final
C. const
D. override
Answer: B. final

8. What will happen if a virtual function is not overridden in a derived class?


A. Compiler error
B. Base class version will be invoked
C. It causes undefined behavior
D. It skips that function call
Answer: B. Base class version will be invoked

9. What is true about abstract classes in C++?


A. Can be instantiated directly
B. Must contain at least one static function
C. Have at least one pure virtual function
D. Always use friend functions
Answer: C. Have at least one pure virtual function

10. What does function overriding require?


A. Different parameter list
B. Same class
C. Derived class with same function signature
D. Different function names
Answer: C. Derived class with same function signature

11. Which of the following correctly defines a pure virtual function?


A. virtual void show() = 0;
B. void show() = 0;
C. virtual void show() {};
D. void show(); = 0
Answer: A. virtual void show() = 0;

12. Which concept allows the same function name to behave differently for different classes?
A. Encapsulation
B. Polymorphism
C. Inheritance
D. Abstraction
Answer: B. Polymorphism

13. What will be the output?

class Base {

public:

void print() { cout << "Base"; }

};

class Derived : public Base {

public:
void print() { cout << "Derived"; }

};

int main() {

Base *bptr = new Derived();

bptr->print();

A. Base
B. Derived
C. Compilation Error
D. Runtime Error
Answer: A. Base

14. What will be the output?

class Base {

public:

virtual void print() { cout << "Base"; }

};

class Derived : public Base {

public:

void print() { cout << "Derived"; }

};

int main() {

Base *bptr = new Derived();

bptr->print();

A. Base
B. Derived
C. Compilation Error
D. Segmentation Fault
Answer: B. Derived

15. What happens when a derived class object is assigned to a base class pointer without virtual
functions?
A. Runtime error
B. Derived class method is called
C. Base class method is called
D. Nothing happens
Answer: C. Base class method is called

16. Which inheritance causes ambiguity if two base classes have the same function?
A. Single
B. Hierarchical
C. Multiple
D. Multilevel
Answer: C. Multiple

17. In C++, which type of function supports late binding?


A. Static
B. Virtual
C. Inline
D. Friend
Answer: B. Virtual

18. What will the output be?

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 *ptr = new C();

ptr->f();

}
A. A
B. B
C. C
D. Compilation Error
Answer: C. C

19. Which access modifier makes members accessible only within the class and friends?
A. private
B. protected
C. public
D. internal
Answer: A. private

20. What does the override keyword ensure?


A. A virtual function is overloaded
B. A base function is hidden
C. The function is truly overriding a virtual function
D. Compile-time polymorphism
Answer: C. The function is truly overriding a virtual function

21. Which of the following is true about base class pointer and derived class object?
A. Can't point
B. Only calls base class methods
C. Can access derived class methods if virtual
D. Causes compile-time error
Answer: C. Can access derived class methods if virtual

22. Which inheritance is not supported in Java but supported in C++?


A. Single
B. Multilevel
C. Multiple
D. Hierarchical
Answer: C. Multiple

23. What keyword is used to resolve ambiguity in multiple inheritance?


A. static
B. scope resolution operator
C. friend
D. new
Answer: B. scope resolution operator
24. What is object slicing?
A. Copying derived object to base object
B. Slicing base to derived
C. Deleting object
D. Object corruption
Answer: A. Copying derived object to base object

25. What is the output?

class A {

public:

virtual void fun() { cout << "A"; }

};

class B : public A {

public:

void fun() { cout << "B"; }

};

int main() {

A a;

B b;

A *ptr = &a;

ptr->fun();

ptr = &b;

ptr->fun();

A. AB
B. BA
C. AA
D. BB
Answer: A. AB

26. What is required to make a base class polymorphic?


A. At least one virtual function
B. At least one static function
C. No constructors
D. Multiple inheritance
Answer: A. At least one virtual function
27. What will be the output?

class Base {

public:

virtual void show(int x = 10) {

cout << x;

};

class Derived : public Base {

public:

void show(int x = 20) {

cout << x;

};

int main() {

Base *b = new Derived();

b->show();

A. 10
B. 20
C. Compilation Error
D. Runtime Error
Answer: A. 10

28. In which situation does polymorphism not apply?


A. Static binding
B. Runtime binding
C. Virtual functions
D. Function overriding
Answer: A. Static binding

29. Which of the following can make a class abstract in C++?


A. Pure virtual function
B. Static function
C. Inline function
D. Const member
Answer: A. Pure virtual function

30. What is the result of deleting a base class pointer pointing to a derived object, without virtual
destructor?
A. Memory is reclaimed properly
B. Only base destructor is called
C. Both destructors are called
D. Compile-time error
Answer: B. Only base destructor is called

You might also like