0% found this document useful (0 votes)
23 views69 pages

BSCS 7 A Final MCQS

The document outlines the concepts of inheritance in C++, detailing types such as public, private, and protected inheritance, along with their effects on member accessibility. It also covers function overloading and overriding, explaining their definitions, differences, and rules in languages like C++ and Java. Additionally, it includes multiple-choice questions and answers to assess understanding of these concepts.

Uploaded by

Sheikh Ahmed
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)
23 views69 pages

BSCS 7 A Final MCQS

The document outlines the concepts of inheritance in C++, detailing types such as public, private, and protected inheritance, along with their effects on member accessibility. It also covers function overloading and overriding, explaining their definitions, differences, and rules in languages like C++ and Java. Additionally, it includes multiple-choice questions and answers to assess understanding of these concepts.

Uploaded by

Sheikh Ahmed
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/ 69

Topic: Types Of Inheritance:

1. What is public inheritance in C++?

a) All members of the base class are inherited as private


b) Public and protected members of the base class retain their original levels
c) All members of the base class become protected
d) Private members are inherited as public

2. What does private inheritance do in C++?

a) Makes all members of the base class private in the derived class
b) Keeps all members of the base class public in the derived class
c) Converts private members to public in the derived class
d) Does not affect the access specifiers of the base class

3. In protected inheritance, public members of the base class become:

a) Private in the derived class


b) Protected in the derived class
c) Public in the derived class
d) Inaccessible in the derived class

4. Which type of inheritance hides the base class members from


outside access?

a) Public
b) Private
c) Protected
d) Default

5. What happens to private members of the base class in inheritance?

a) They are inherited as private members


b) They are inherited as protected members
c) They are inherited as public members
d) They are not inherited

6. What is the default type of inheritance in C++?

a) Public
b) Private
c) Protected
d) None

7. In public inheritance, how are protected members of the base class


treated?
a) Public in the derived class
b) Protected in the derived class
c) Private in the derived class
d) Inaccessible in the derived class

8. How does protected inheritance differ from public inheritance?

a) Public members become protected in the derived class


b) Protected members become public in the derived class
c) Private members are inherited in protected inheritance
d) Protected inheritance is only used for static members

9. Which of the following is true for private inheritance?

a) All base class members become public in the derived class


b) All base class members become private in the derived class
c) Protected members become public in the derived class
d) Base class members are not inherited

10. In C++, what is the correct syntax for public inheritance?

a) class Derived : public Base { };


b) class Derived Base { };
c) class Derived public Base { };
d) public class Derived : Base { };

11. Can private members of a base class be directly accessed by the


derived class?

a) Yes
b) No
c) Only in public inheritance
d) Only in protected inheritance

12. What does public inheritance ensure?

a) Derived class cannot modify the base class members


b) Derived class provides direct access to base class public members
c) Derived class hides the base class members from other classes
d) Base class functions cannot be overridden

13. Which access specifier is used when a derived class wants to


expose all public members of the base class?

a) Protected
b) Private
c) Public
d) None
14. Which inheritance type allows members of the base class to be
accessed only within the derived class hierarchy?

a) Public
b) Private
c) Protected
d) Static

15. What happens to the protected members of a base class in private


inheritance?

a) They become private in the derived class


b) They become protected in the derived class
c) They remain protected
d) They are not inherited

16. Which of the following is true about inheritance in C++?

a) All types of inheritance inherit private members


b) Only public inheritance retains original access levels
c) Protected inheritance is the default inheritance type
d) Private inheritance is always preferred over public inheritance

17. If a base class has public members and the derived class inherits it
privately, how are these members treated in the derived class?

a) Public
b) Protected
c) Private
d) Inaccessible

18. What kind of inheritance allows the derived class to directly access
base class protected members?

a) Public inheritance
b) Private inheritance
c) Protected inheritance
d) Multiple inheritance

19. Which of the following is a characteristic of private inheritance?

a) Base class public members remain public


b) Base class public members become private
c) Base class public members become protected
d) Only private members of the base class are inherited

20. What is multiple inheritance in C++?


a) A derived class inherits from one base class
b) A derived class inherits from two or more base classes
c) A base class inherits from another base class
d) A base class has multiple derived classes

21. Which inheritance type is most restrictive in C++?

a) Public
b) Private
c) Protected
d) Default

22. Which of the following members are accessible in a derived class


through protected inheritance?

a) Private members
b) Public and protected members
c) Public members only
d) None of the above

23. In public inheritance, who can access the public members of the
base class?

a) Only the derived class


b) Only friends of the base class
c) Anyone with access to the derived class
d) Only private members of the derived class

24. In which type of inheritance does the derived class restrict access
to base class members?

a) Public
b) Private
c) Protected
d) Static

25. Can a protected member of a base class be accessed by an object


of the derived class?

a) Yes
b) No
c) Only in public inheritance
d) Only in protected inheritance

26. In C++, which inheritance type keeps base class members hidden
from the outside but accessible within the derived class?

a) Public
b) Protected
c) Private
d) Static

27. What happens to public members of a base class when the derived
class inherits privately?

a) They remain public


b) They become private in the derived class
c) They become protected in the derived class
d) They are not inherited

28. Which inheritance type ensures maximum access to base class


members for the derived class?

a) Public inheritance
b) Private inheritance
c) Protected inheritance
d) None of the above

29. In private inheritance, can the derived class object access public
members of the base class directly?

a) Yes
b) No
c) Only if they are friend functions
d) Only through public inheritance

30. In protected inheritance, can public members of the base class be


accessed directly by the derived class?

a) Yes
b) No
c) Only through a friend function
d) Only if they are static members

31. Which of the following is a valid syntax for private inheritance?

a) class Derived private Base { };


b) class Derived : private Base { };
c) private class Derived : Base { };
d) class Derived : Base private { };

32. Can a derived class override a public function of the base class in
C++?

a) Yes, always
b) No, never
c) Only if it is inherited privately
d) Only if the function is virtual
33. In C++, which of the following inheritance types allows further
inheritance of base class members?

a) Public
b) Protected
c) Private
d) Both a and b

34. In multiple inheritance, what happens if two base classes have


members with the same name?

a) Causes a compilation error


b) The derived class must explicitly resolve ambiguity
c) The compiler automatically selects the first base class member
d) The derived class cannot inherit from both classes

35. Which access specifier is used in the following inheritance?

class Derived : Base { };

a) Public
b) Protected
c) Private
d) Default

36. What is the default access level of a class’s members in C++?

a) Public
b) Private
c) Protected
d) None

37. Can protected members of a base class be accessed by a derived


class’s object?

a) Yes, always
b) No, never
c) Only through public inheritance
d) Only if the base class grants friend access

38. When a base class is inherited publicly, what happens to its private
members?

a) They become public


b) They become protected
c) They remain private and are not inherited
d) They become accessible through objects of the derived class
39. Which type of inheritance is used if the following syntax is written?

class Derived : protected Base { };

a) Public
b) Private
c) Protected
d) Multiple

40. What is "hierarchical inheritance"?

a) A single base class is inherited by multiple derived classes


b) Multiple base classes are inherited by one derived class
c) A derived class acts as a base class for another derived class
d) No inheritance is involved

41. What is "multi-level inheritance"?

a) A single base class is inherited by multiple derived classes


b) A derived class acts as a base class for another derived class
c) Two or more base classes are inherited by a single derived class
d) None of the above

42. Which of the following best describes protected inheritance?

a) Only public members of the base class are inherited as private


b) Both public and protected members of the base class become private
c) Public members of the base class become protected
d) Private members of the base class are inherited

43. What does "single inheritance" mean?

a) A derived class inherits from one base class


b) A derived class inherits from multiple base classes
c) A base class is inherited by multiple derived classes
d) None of the above

44. What is the scope of private members of the base class in


inheritance?

a) Accessible only through friend functions in the derived class


b) Accessible only within the base class
c) Accessible by objects of the derived class
d) Accessible within both the base and derived classes

45. How does the compiler treat ambiguous base class members in
multiple inheritance?
a) Selects the first base class’s member
b) Causes a compilation error
c) Resolves ambiguity automatically
d) Allows the programmer to explicitly specify the base class

46. In protected inheritance, who can access the inherited protected


members?

a) Only the derived class


b) Only the base class
c) Derived class and further derived classes
d) All classes

47. What does private inheritance restrict?

a) Access to private members of the base class


b) Access to public members of the base class
c) Access to protected members of the base class
d) Both b and c

48. What type of inheritance is required for a "diamond problem"?

a) Public inheritance
b) Private inheritance
c) Multiple inheritance
d) Virtual inheritance

49. What does the "virtual" keyword do in inheritance?

a) Prevents inheritance
b) Resolves ambiguity in multiple inheritance
c) Converts base class members to public
d) Allows access to private members of the base class

50. In which inheritance type are base class members not accessible
outside the derived class?

a) Public inheritance
b) Protected inheritance
c) Private inheritance

d) Virtual inheritance

MCQ No. Answer MCQ No. Answer


1 b 2 a
3 b 4 b
5 d 6 d
7 b 8 a
9 b 10 a
11 b 12 b
13 c 14 c
15 a 16 b
17 c 18 a
19 b 20 b
21 b 22 b
23 c 24 b
25 b 26 b
27 b 28 a
29 b 30 a
31 b 32 d
33 d 34 b
35 c 36 b
37 b 38 c
39 c 40 a
41 b 42 c
43 a 44 b
45 d 46 c
47 d 48 c
49 b 50 c
Topic: Overloading Vs Overriding

1. What is function overloading?


a) Defining multiple functions with the same name but different parameters
b) Using functions from another class
c) Defining multiple functions with the same name and same parameters
d) None of the above

2. Function overloading is an example of:


a) Runtime polymorphism
b) Compile-time polymorphism
c) Abstraction
d) Encapsulation

3. Which of these is necessary for function overloading?


a) Same name but different return types
b) Different number or types of arguments
c) Different scope of functions
d) All of the above

4. In function overloading, can two functions differ only by their return type?
a) Yes
b) No

5. Which of the following overloads a function correctly in C++?

cpp
CopyEdit
void func(int a);
void func(float b);

a) Correct
b) Incorrect

6. In Java, function overloading is implemented by:


a) Using @Override annotation
b) Defining multiple methods with the same name but different parameters
c) Using generics
d) None of the above

7. Which of the following scenarios results in a compile-time error during overloading?


a) Same name, different number of parameters
b) Same name, different types of parameters
c) Same name, same parameters but different return types
d) Same name, parameters with different default values

8. Can overloaded functions have different access modifiers?


a) Yes
b) No

9. In Python, which concept is similar to function overloading?


a) Decorators
b) Method overloading using default arguments
c) Abstract methods
d) None of the above

10. What determines which overloaded function is called?


a) The number of arguments
b) The type of arguments
c) Both a and b
d) Return type

11. What is function overriding?


a) Reusing a function in the same class
b) Redefining a base class function in a derived class
c) Using default arguments
d) None of the above

12. Function overriding is an example of:


a) Compile-time polymorphism
b) Runtime polymorphism
c) Data hiding
d) Encapsulation

13. In C++, which keyword ensures a function is overridden?


a) override
b) virtual
c) static
d) final

14. In Java, which annotation is used for overriding?


a) @Override
b) @Overload
c) @Virtual
d) @Implement

15. Which of the following statements is correct for overriding in C++?


a) Overridden methods must have the same name and parameters.
b) Overridden methods can have different return types.
c) Both a and b
d) None of the above

16. What happens if a derived class function hides a base class function without overriding
it?
a) Base class function is inaccessible
b) Derived class function is always called
c) Base class function is still accessible using scope resolution
d) Both b and c

17. Which access specifier must be used for a base class function to allow overriding?
a) private
b) protected
c) public or protected
d) None of the above

18. In Java, overridden methods:


a) Must have the same name, parameters, and return type
b) Can differ in return type
c) Can differ in access specifier
d) None of the above

19. What is method overriding in Python?


a) Defining multiple methods with the same name
b) Redefining a parent class method in a child class
c) Using @staticmethod
d) None of the above

20. In function overriding, which method is called?


a) Base class method
b) Derived class method
c) Depends on the object type
d) Both a and b

21. Which concept allows overloading and overriding to coexist?


a) Inheritance
b) Polymorphism
c) Encapsulation
d) Composition

22. Which of the following is true?


a) Overloading happens at runtime; overriding happens at compile time.
b) Overloading happens at compile time; overriding happens at runtime.
c) Both occur at runtime.
d) Both occur at compile time.

23. Function overloading is possible in:


a) Only statically typed languages
b) Only dynamically typed languages
c) Both
d) None

24. Can constructors be overloaded?


a) Yes
b) No
25. Which of these is invalid for function overloading?
a) Default arguments
b) Variable arguments
c) Same name, same parameters
d) None of the above

26. Overridden functions in a derived class must have:


a) Same name and parameters
b) Different name and parameters
c) Same return type but different name
d) None of the above

27. Which keyword in C++ prevents a function from being overridden?


a) static
b) final
c) override
d) None of the above

28. Which access modifier in Java allows overriding?


a) private
b) protected
c) public or protected
d) None of the above

29. What is the role of the super keyword in Java?


a) Call the superclass constructor
b) Access superclass methods
c) Both a and b
d) None of the above

30. Which of these is true?


a) Overloading requires inheritance.
b) Overriding requires inheritance.
c) Both require inheritance.
d) None require inheritance.

31. Which of the following is true for function overloading?


a) Functions must have the same name and signature.
b) Functions can have the same name but different parameters.
c) Functions can have different names and same parameters.
d) None of the above

32. Function overloading does not work with:


a) Different parameter types
b) Different number of parameters
c) Different return types only
d) None of the above
33. In Python, how can you simulate function overloading?
a) Using @staticmethod
b) Using default arguments
c) Using multiple functions with the same name
d) It is not possible

34. Overloading works if:


a) Functions differ in parameter count
b) Functions differ in parameter type
c) Both a and b
d) None of the above

35. Which of the following code snippets demonstrates function overloading in C++?

cpp

CopyEdit

void display(int a);

void display(float b);

a) Correct implementation
b) Incorrect implementation

36. Which of these languages does not support function overloading?


a) C++
b) Java
c) Python
d) C

37. Which type of polymorphism does function overloading achieve?


a) Dynamic
b) Static
c) Hybrid
d) None of the above

38. In Java, when does function overloading occur?


a) At runtime
b) At compile time

39. Can overloaded functions be static in C++?


a) Yes
b) No
40. Which of the following is an invalid function overloading example in C++?
a) Same name, same parameters, different return types
b) Same name, different number of parameters
c) Same name, different parameter types
d) None of the above

41. Function overriding works on:


a) Static functions
b) Non-static functions
c) Both

42. In C++, overriding a function requires the base class function to be declared as:
a) private
b) final
c) virtual
d) static

43. In Java, the super keyword is used to:


a) Override a method
b) Call a superclass method
c) Call a superclass constructor
d) Both b and c

44. Which of these is true for overriding in Java?


a) The overridden method can have a stricter access modifier.
b) The overridden method can have a less strict access modifier.
c) Both are true.
d) Neither is true.

45. Which of the following is necessary for overriding?


a) Same name and return type
b) Different number of parameters
c) Different data types
d) All of the above

46. If you want to prevent a method from being overridden in Java, use the keyword:
a) override
b) static
c) final
d) sealed

47. In Python, overriding is possible in:


a) Single inheritance
b) Multiple inheritance
c) Both
d) None of the above

48. When overriding a method, which method is called at runtime?


a) Base class method
b) Derived class method
c) Both
d) None

49. Which C++ feature allows overriding but ensures the base class method is accessible?
a) protected keyword
b) Scope resolution operator ::
c) final keyword
d) private keyword

50. Which principle is illustrated by method overriding?


a) Encapsulation
b) Polymorphism
c) Abstraction
d) Inheritance

MCQ’s Key

1. a 2. b 3. b 4. b 5. a
6. b 7. c 8. a 9. b 10. c
11. b 12. b 13. b 14. a 15. a
16. d 17. c 18. a 19. b 20. c
21. b 22. b 23. c 24. c 25. b
26. a 27. b 28. c 29. c 30. b
31. b 32. c 33. b 34. c 35. a
36. d 37. b 38. b 39. a 40. a
41. b 42. c 43. d 44. b 45. a
46. c 47. c 48. b 49. b 50. b
Topic: Static and Dynamic Binding

1. What is static binding in C++?


a) Binding of a function at runtime
b) Binding of a function at compile-time
c) Dynamic linking of libraries
d) Linking two variables at runtime
2. Static binding is also known as:
a) Dynamic linking
b) Early binding
c) Late binding
d) Overriding
3. Which keyword prevents static binding in C++?
a) final
b) virtual
c) static
d) override
4. What determines static binding in C++?
a) Object type
b) Pointer type
c) Function prototype
d) Compiler analysis
5. Which of the following is NOT associated with static binding?
a) Operator overloading
b) Function templates
c) Virtual functions
d) Inline functions
6. Static binding improves:
a) Runtime performance
b) Code readability
c) Compile-time errors
d) Code reusability
7. Which of these is true for static binding?
a) It delays function resolution until runtime
b) It resolves function calls at compile-time
c) It supports polymorphism
d) It works only with pointers
8. Static binding is used in:
a) Function overriding
b) Abstract base classes
c) Friend functions
d) Inline functions
9. Static binding is NOT used in which of the following scenarios?
a) Function overriding
b) Function overloading
c) Operator overloading
d) Default arguments
10. In C++, static binding can be observed with:
a) Dynamic_cast operator
b) Static_cast operator
c) Virtual base class
d) Function pointers
11. Which of the following supports static binding?
a) Function templates
b) Dynamic dispatch
c) Virtual table
d) Pure virtual functions
12. Static binding avoids:
a) Polymorphism
b) Code reusability
c) Performance overhead
d) Compile-time type safety
13. Which of the following statements is true?
a) Static binding uses a virtual table
b) Static binding occurs at runtime
c) Static binding is faster than dynamic binding
d) Static binding cannot be used with templates
14. What is the main advantage of static binding?
a) Flexibility at runtime
b) Enhanced code readability
c) Better runtime performance
d) Simplified inheritance hierarchy
15. Which of the following is a consequence of using static binding?
a) Increased flexibility
b) Reduced code complexity
c) Limited polymorphism
d) None of the above
16. What is dynamic binding in C++?
a) Binding of a function at compile-time
b) Linking two variables at runtime
c) Binding of a function at runtime
d) Linking libraries at compile-time
17. Which keyword is used to enable dynamic binding in C++?
a) static
b) final
c) virtual
d) override
18. Dynamic binding is also known as:
a) Early binding
b) Late binding
c) Static binding
d) Compile-time binding
19. Which of the following features in C++ relies on dynamic binding?
a) Function overloading
b) Operator overloading
c) Polymorphism
d) Friend functions
20. Dynamic binding occurs when:
a) A function is non-virtual
b) A function is declared as virtual
c) A function is overloaded
d) A static member function is called
21. Dynamic binding works through the use of:
a) Static_cast
b) Virtual table (vtable)
c) Function templates
d) Inline functions
22. Which of the following is NOT a characteristic of dynamic binding?
a) Function resolution at runtime
b) Use of vtable and vpointer
c) Achieved through the virtual keyword
d) Faster than static binding
23. What happens if a virtual function is not overridden in a derived class?
a) The program throws an error
b) The base class version is called
c) The derived class version is called
d) The function becomes abstract
24. Dynamic binding can be implemented in C++ by:
a) Overloading operators
b) Overriding virtual functions
c) Declaring inline functions
d) Using friend functions
25. Which operator helps achieve dynamic binding for virtual functions in C++?
a) ->
b) ::
c) .
d) ->*
26. When does dynamic binding occur in a program?
a) During compilation
b) During linking
c) During runtime
d) During pre-processing
27. What happens when a pure virtual function is not implemented in a derived class?
a) The derived class becomes abstract
b) The program runs normally
c) The base class version is executed
d) A runtime error occurs
28. Dynamic binding improves:
a) Compile-time performance
b) Code readability
c) Runtime flexibility
d) Static analysis
29. Which of the following uses dynamic binding?
a) Inline functions
b) Friend functions
c) Virtual destructors
d) Function templates
30. Dynamic binding is used when:
a) The base class function is called directly
b) A derived class function is invoked through a base class pointer
c) The function is declared as static
d) Inline functions are used
31. Which of the following can lead to a performance overhead in dynamic binding?
a) Function overloading
b) Virtual function calls
c) Static function calls
d) None of the above
32. What is the effect of using the final specifier in C++?
a) It prevents a class from being inherited
b) It prevents a virtual function from being overridden
c) It allows a function to be called without an object
d) None of the above
33. What is the purpose of the override keyword in C++11?
a) To indicate that a function is static
b) To indicate that a function is virtual
c) To indicate that a function is overriding a base class function
d) To prevent function overriding
34. What happens when a derived class overrides a base class virtual function?
a) The base class function is called
b) The derived class function is called
c) Both functions are called
d) None of the above
35. What is the main advantage of dynamic binding?
a) Faster execution
b) More memory usage
c) Flexibility and extensibility
d) Simplicity
36. Which type of function is associated with static binding?
a) Virtual functions
b) Overloaded functions
c) Overridden functions
d) Pure virtual functions
37. Static binding can be applied to:
a) Virtual functions
b) Non-virtual functions
c) Pure virtual functions
d) Functions with the override keyword
38. What is the performance impact of dynamic binding compared to static binding?
a) Dynamic binding is faster than static binding
b) Static binding is slower than dynamic binding
c) Dynamic binding has a slight runtime overhead
d) Both have the same performance
39. Static binding determines the function to be executed based on:
a) The type of the object at runtime
b) The type of the object at compile-time
c) Virtual table lookups
d) Function overrides
40. Which of the following supports both static and dynamic binding?
a) Friend functions
b) Overloaded functions
c) Member functions
d) Static member functions
41. Dynamic binding relies on which of the following mechanisms?
a) Static_cast
b) Virtual table and vpointer
c) Overloaded operator resolution
d) Compile-time template resolution
42. Overridden functions are:
a) Statically bounded
b) Dynamically bounded
c) Inline functions
d) Non-Virtual functions
43. Redefined functions are:
a) Overloaded
b) Statically bounded
c) Dynamically bounded
d) Overridden
44. What happens when a derived class redefines a base class function without using the
virtual keyword?
a) The derived class function is dynamically bound
b) The base class function is hidden, and static binding is used
c) The base class function is overridden
d) A compiler error occurs
45. You have a base class Animal with a function speak(). A derived class Dog redefines the
speak() function without using the virtual keyword. When a base class pointer points to a Dog
object and calls speak(), what happens?
a) The Dog version of speak() is called
b) The Animal version of speak() is called
c) A compiler error occurs
d) The program crashes at runtime
46. A function printDetails() is declared in both base and derived classes without the virtual
keyword. If a base class object is created, which version of the function is executed when
called?
a) Base class version
b) Derived class version
c) It depends on the runtime type of the object
d) A runtime error occurs
47. In a base class Person, a virtual destructor is defined. A derived class Student overrides it.
If a Person* pointer points to a Student object and is deleted, what happens?
a) Only the Person destructor is called
b) Only the Student destructor is called
c) Both destructors are called (Student first, then Person)
d) Undefined behavior occurs
48. A base class Parent has a virtual function introduce(), and a derived class Child overrides
it. If a Parent object is directly created and introduce() is called, what happens?
a) The Parent version is called
b) The Child version is called
c) A compile-time error occurs
d) The virtual function is skipped because Parent is instantiated directly
49. What will the following code output?
class Base {
public:
void display() { cout << "Base Display\n"; }
};
class Derived : public Base {
public:
void display() { cout << "Derived Display\n"; }
};
int main() {
Base* b = new Derived();
b->display();
return 0;
}
a) Base Display
b) Derived Display
c) Compilation error
d) Undefined behavior
50. What will happen when the following code is executed?
class Parent {
public:
virtual void introduce() = 0; // Pure virtual function
};
class Child : public Parent {
public:
void introduce() override { cout << "Child Introduce\n"; }
};
int main() {
Parent* p = new Child();
p->introduce();
return 0;
}
a) Child Introduce
b) Runtime error
c) Compilation error
d) Parent Introduce
51. Which of the following statements is true about redefining and overriding in C++?
a) Redefining and overriding always use dynamic binding.
b) Redefining functions require the virtual keyword, while overriding does not.
c) Redefining is resolved at compile-time, and overriding is resolved at runtime.
d) Both are resolved at runtime but behave differently.

52. In which of the following cases will a compiler error occur?


a) A derived class redefines a non-virtual function from the base class.
b) A derived class overrides a virtual function but with a different return type.
c) A base class pointer calls a redefined function in the derived class.
d) A derived class does not implement a pure virtual function from the base class.
53. What happens if a base class function is declared as virtual, and a derived class defines a
function with the same name but different parameters?
a) The derived class function overrides the base class function.
b) The base class function is hidden in the derived class.
c) The derived class function is dynamically bound.
d) A compile-time error occurs.
54. Which of the following statements is false about static and dynamic binding?
a) Static binding occurs when the function call is resolved at compile-time.
b) Dynamic binding requires the function to be declared virtual in the base class.
c) Static binding can occur even if the function is virtual.
d) Dynamic binding is only applicable to overloaded functions.
55. What is true about the following code snippet?
class A {
public:
virtual void func() = 0;
};
class B : public A {
public:
void func() override {}
};

class C : public B {
public:
void func() override {}
};
a) Class B cannot be instantiated, but class C can be instantiated.
b) Both B and C can be instantiated.
c) Neither B nor C can be instantiated.
d) Class B can be instantiated, but class C cannot be instantiated.
56. What will the following code produce?
class Base {
public:
virtual void func1() { cout << "Base func1\n"; }
virtual void func2() { cout << "Base func2\n"; }
};

class Derived : public Base {


public:
void func1() override { cout << "Derived func1\n"; }
void func2(int x) { cout << "Derived func2 with " << x << "\n"; }
};

int main() {
Base* b = new Derived();
b->func1();
b->func2();
return 0;
}
a) Derived func1
Base func2
b) Base func1
Derived func2
c) Compilation error
d) Derived func1
Derived func2
57. What happens when the following code is executed?
class A {
public:
void show() { cout << "A Show\n"; }
};

class B : public A {
public:
void show() { cout << "B Show\n"; }
};

int main() {
A* a = new B();
a->show();
return 0;
}
a) A Show
b) B Show
c) Undefined behavior
d) Compilation error
58. What happens when you redefine a virtual function without using the override
keyword in a derived class?
a) A compile-time error occurs.
b) The function will still be dynamically bound, but warnings may be issued.
c) The function will be treated as statically bound, ignoring the virtual nature.
d) The base class function remains pure virtual.
59. Which of the following is an incorrect statement about overriding and redefining in
C++?
a) Redefining a function in the derived class hides the base class function.
b) Overriding requires the base class function to be declared as virtual.
c) Overridden functions can have different function signatures between base and derived
classes.
d) Redefining is resolved based on the static type of the object.
60. What is a potential downside of using dynamic binding in large systems?
a) It complicates the implementation of inheritance hierarchies.
b) It reduces the reusability of code.
c) It can make debugging more difficult because the exact function called is determined at
runtime.
d) It forces the use of static functions.

MCQ’s Key

1 b 7 b 13 c 19 c 25 a
2 b 8 d 14 c 20 b 26 c
3 b 9 a 15 c 21 b 27 a
4 d 10 b 16 c 22 d 28 c
5 c 11 a 17 c 23 b 29 c
6 a 12 c 18 b 24 b 30 b
31 b 37 b 43 b 49 a 55 a
32 b 38 c 44 b 50 a 56 a
33 c 39 b 45 b 51 c 57 a
34 b 40 c 46 a 52 b 58 b
35 c 41 b 47 c 53 b 59 c
36 b 42 b 48 a 54 d 60 c

Topic: Abstract Classes

1. What is an abstract class in C++?


a) A class that cannot have member functions
b) A class with at least one pure virtual function
c) A class that cannot be inherited
d) A class that must have constructors

2. What is a pure virtual function?


a) A function that is always defined inside the base class
b) A function that has no implementation in the base class
c) A function that must be static
d) A function that cannot be overridden
3. How do you declare a pure virtual function in C++?
a) virtual void show() = 0;
b) void show() = 0;
c) virtual void show();
d) void show();

4. What happens if a class has at least one pure virtual function?


a) It cannot have constructors
b) It cannot have member variables
c) It cannot be instantiated
d) It must not have destructors

5. What keyword is used to define an abstract class in C++?


a) abstract
b) virtual
c) interface
d) No specific keyword

6. Can an abstract class have a constructor?


a) Yes
b) No

7. If a derived class does not override a pure virtual function of the base class, what
happens?
a) It becomes an abstract class itself
b) It gets an error at runtime
c) It runs without any issue
d) It throws a compiler warning

8. What is true about an abstract base class?


a) It cannot have normal member functions
b) It can only contain virtual functions
c) It cannot have member variables
d) It can have both pure virtual functions and normal member functions

9. Can a derived class of an abstract class be instantiated?


a) Yes, always
b) Yes, only if it overrides all pure virtual functions
c) No, never
d) No, unless it is declared as final

10. What is required to make an abstract class concrete?


a) Remove all pure virtual functions
b) Define a constructor
c) Provide implementation for all pure virtual functions in a derived class
d) Declare all methods as static

11. Can an abstract class have a destructor?


a) Yes, and it should be virtual
b) No, it is not allowed
c) Only if it is static
d) Only if it is protected

12. If an abstract class has multiple pure virtual functions, what must a derived class
do to become concrete?
a) Override at least one function
b) Override all pure virtual functions
c) Provide a constructor
d) Inherit another abstract class

13. What happens if an abstract class has a constructor?


a) It cannot be instantiated
b) It must be overridden in derived classes
c) It will cause a compilation error
d) It allows object creation
14. If a function is declared as virtual void show() = 0;, what does = 0 indicate?
a) The function is a pure virtual function
b) The function returns zero
c) The function is optional
d) The function is private

15. Can an abstract class have private pure virtual functions?


a) Yes
b) No

16. Which of the following correctly implements an abstract class?

class Base {
virtual void display() = 0;
};

a) Correct
b) Incorrect

17. If a pure virtual function is overridden in a derived class, what must be done for
proper object destruction?
a) The base class destructor should be virtual
b) The derived class destructor should be virtual
c) Nothing special is required
d) The function must be declared as final

18. Can an abstract class be a friend class?


a) Yes
b) No

19. What is the purpose of an abstract class?


a) To force derived classes to implement specific functionality
b) To prevent inheritance
c) To allow object slicing
d) To optimize memory allocation

20. How can a class be made abstract without defining any pure virtual function?
a) Declare the destructor as pure virtual
b) Declare all functions as private
c) Use the abstract keyword
d) Declare at least one function as static

21. Can an abstract class contain normal (non-virtual) functions?


a) Yes
b) No

22. What happens if a derived class does not implement all pure virtual functions of
the base class?
a) The derived class becomes abstract
b) The program runs with warnings
c) Compilation error
d) It causes undefined behavior

23. If a derived class provides its own version of a pure virtual function, what
happens to the base class reference pointing to the derived class object?
a) Calls the base class function
b) Calls the derived class function
c) Compilation error
d) Causes undefined behavior

24. What is the return type of a pure virtual function?


a) Must always be void
b) Can be any data type
c) Must be an int
d) Must be a char

25. If a class has a pure virtual destructor, what must be done?


a) Provide an implementation for it
b) It should not be defined
c) It must be private
d) It must be static

26. How many pure virtual functions must a class have to be considered abstract?
a) At least one
b) At least two
c) Exactly three
d) None
27. What is the advantage of using abstract classes?
a) Provides a common interface for derived classes
b) Increases compilation speed
c) Reduces code size
d) Prevents multiple inheritance

28. What is a correct example of a pure virtual function declaration?


a) virtual void show() = 0;
b) void show() = 0;
c) virtual int show();
d) void show();

29. If a class A is abstract and class B inherits from A, but does not override all
pure virtual functions, what happens?
a) Class B also becomes abstract
b) Class B runs without any issue
c) Class B cannot be instantiated
d) Both a and c

30. Which of the following statements is true?


a) An abstract class can have member variables
b) An abstract class cannot have a constructor
c) An abstract class must have a destructor
d) An abstract class must have a static method

31. Can abstract classes be used in multiple inheritance?


a) Yes
b) No

32. If a class inherits from two abstract classes, what must it do?
a) Implement all pure virtual functions from both classes
b) Implement only one function
c) Cannot be inherited
d) None of the above

33. Can an abstract class implement an interface in C++?


a) Yes, using virtual functions
b) No, C++ does not support interfaces
c) Yes, using static functions
d) No, because it will cause a conflict

34. If an abstract class has an implemented method, can it be called from a derived class?
a) Yes
b) No

35. Can an abstract class be used as a data member in another class?


a) No
b) Yes, using pointers or references

36. What is the correct way to instantiate an abstract class?


a) A obj;
b) A *obj = new A();
c) A *obj = new Derived();
d) A obj = new A();

37. Can a constructor of a derived class call the constructor of an abstract base class?
a) Yes
b) No

38. What happens if a pure virtual function has a definition in the abstract class?
a) The function must be overridden
b) The function can be called by derived classes
c) The function behaves as a normal virtual function
d) All of the above

39. What is the output of the following program?

class
Base
{ pub
lic:
virtual void show() = 0;
};
class Derived : public Base
{ public:
void show() { cout << "Derived"; }
};
int main()
{ Derived
d;
d.show();
}
a) Error
b) Derived
c) Base
d) Undefined

40. What is the main purpose of abstract classes?


a) Prevent object creation
b) Provide a blueprint for derived classes
c) Enforce polymorphism
d) All of the above

41. Can an abstract class have static member functions?


a) Yes
b) No

42. How do you call a pure virtual function inside a base class constructor?
a) It is not possible
b) Using this->function()
c) Using Base::function()
d) Using a derived class object

43. If a function is both virtual and static, what happens?


a) Compilation error
b) Works as a static function
c) Works as a virtual function
d) Undefined behavior

44. Can a non-abstract class inherit from an abstract class without implementing its
pure virtual functions?
a) No
b) Yes

45. How do abstract classes help in designing large applications?


a) By enforcing common interfaces
b) By reducing memory usage
c) By avoiding constructors
d) By preventing inheritance
46. Which of the following correctly defines an abstract class?
a) class A { virtual void show() = 0; };
b) class A { void show() = 0; };
c) class A { virtual void show(); };
d) class A { void show(); };

47. What happens when a base class with a pure virtual destructor is deleted via a
base class pointer?
a) Calls only the base class destructor
b) Calls the derived class destructor first
c) Causes memory leak
d) Results in undefined behavior

48. If a class contains a friend function, can it still be abstract?


a) Yes
b) No

49. Can abstract classes be templated?


a) Yes
b) No

50. What happens if a derived class partially implements an abstract class?


a) It remains abstract
b) It can be instantiated
c) It becomes a static class
d) None of the above

51. A class is made abstract by declaring at least one of its functions as?
a) impure virtual function
b) pure virtual function
c) pure abstract function
d) impure abstract function

52. A pure virtual function is specified by placing?


a)-1
b) 0
c) 1
d) infinite

53. Classes that can be used to instantiate objects are called?


a) concrete classes
b) interface
c) abstract class
d) None of the above

54. Which of the following is true?


a) The C++ interfaces are implemented using abstract classes
b) The purpose of an abstract class is to provide an appropriate base class from which
other classes can inherit.
c) Abstract classes cannot be used to instantiate objects and serves only as an interface.
d) All of the above

55. Where does the abstract class is used?


a) base class only
b) derived class
c) both derived & base class
d) virtual class

56. Which class is used to design the base class?


a) abstract class
b) derived class
c) base class
d) derived & base class

57. We cannot make an instance of an abstract base class


a) TRUE
b) FALSE
c) Can be true and false
d) Can not say

8. We can make an instance of an abstract super class

a) TRUE
b) FALSE
c) Can be true and false
d) Can not say

9. Which is the correct syntax of defining a pure virtual function?

a) pure virtual return_type func();


b) virtual return_type func() pure;
c) virtual return_type func() = 0;
d) virtual return_type func();
60. Which is the correct statement about pure virtual functions?
a) They should be defined inside a base class
b) Pure keyword should be used to declare a pure virtual function
c) Pure virtual function is implemented in derived classes
d) Pure virtual function cannot implemented in derived classes

MCQS KEY
1 B 11 A 21 A
2 B 12 B 22 A
3 A 13 A 23 B
4 C 14 A 24 B
5 D 15 A 25 A
6 A 16 B 26 A
7 A 17 A 27 A
8 D 18 A 28 A
9 B 19 A 29 D
10 C 20 A 30 A
31 A 41 A 51 B
32 A 42 A 52 B
33 A 43 A 53 A
34 A 44 A 54 D
35 B 45 A 55 A
36 C 46 A 56 A
37 A 47 B 57 A
38 D 48 A 58 B
39 B 49 A 59 C
40 D 50 A 60 C

Topic: Inheritance

1. Which of the following is the correct syntax for inheriting a class in C++?

a) class Derived : public Base { };


b) class Derived : Base { };
c) class Derived(Base) { };
d) class Derived(Base) : public { };

2. What is the default access specifier for members of a class inherited using
private inheritance?

a) Private
b)Public
c) Protected
d) Default

3. In C++, a derived class can access which members of its base class?
a) Private
b) Public
c) Protected
d) Both b and c

4. What is the use of the virtual keyword in a base class?

a) To indicate a class is abstract


b)To allow polymorphic behavior in derived classes
c) To prevent the class from being inherited
d) None of the above

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

a) Single Inheritance
b) Multiple Inheritance
c) Multilevel Inheritance
d) Restricted Inheritance

6. If a derived class does not have a constructor, which constructor of the base class
is called?

a)Default constructor
b) Parameterized constructor
c) Copy constructor
d) None of the above

7. Which access specifier will be used by default when inheriting a class in C++?
a) Public
b)Private
c) Protected
d) Default

8. Which keyword is used to create a derived class from a base class in C++?

a) extend
b) inherits
c) class
d) :

9. If a class is derived privately, what type of access will its members have in the
derived class?

a)Private
b) Public
c) Protected
d) None of the above

10. Which of the following is true about inheritance in C++?

a)A derived class can inherit from multiple base classes


b) A base class can inherit from a derived class
c) Inheritance is not supported in C++
d) A derived class cannot access protected members of the base class

11. Which of the following constructors will be called when a derived class object is
created?

a) Only the base class constructor


b)Only the derived class constructor
c) Both the base class constructor and the derived class constructor
d) No constructor is called

12. What is the main advantage of inheritance in C++?

a)It helps avoid code duplication


b) It makes the code slower
c) It is difficult to implement
d) It allows for multithreading

13. What happens if the destructor of the base class is virtual?


a) The derived class destructor is not called
b)The destructor of the derived class is called after the base class
destructor
c) It prevents polymorphism
d) It has no effect

14. Which of the following is a feature of public inheritance?

a)Public members of the base class become private in the derived


class
b) Private members of the base class are accessible in the derived class
c) Public members of the base class are accessible in the derived class
d) Protected members of the base class become public in the derived class

15. What is the result of multiple inheritance in C++?

a)It can lead to ambiguity


b) It causes a runtime error
c) It is not allowed in C++
d) It leads to a reduction in code size

16. Which of the following will allow a derived class to access protected members of
the base class?

a) Private inheritance
b)Public inheritance
c) Protected inheritance
d) All of the above

17. Which of the following correctly describes multilevel inheritance in C++?

a)A derived class inherits from another derived class


b) A derived class inherits from multiple base classes
c) A base class inherits from multiple derived classes
d) A class cannot inherit from any class

18. If a class is inherited privately, what happens to the public members of the base
class?

a)They become private in the derived class


b) They become protected in the derived class
c) They remain public in the derived class
d) They cannot be accessed

19. Which type of inheritance allows a class to inherit the functionality of


multiple base classes?
a) Single Inheritance
b) Multilevel Inheritance
c)Multiple Inheritance
d) Hybrid Inheritance

20. What happens when a class is inherited in C++ using protected inheritance?

a)Public members of the base class become protected in the derived


class
b) Protected members of the base class become private in the derived class
c) Public members of the base class become private
d) Private members of the base class are inherited

21. Can a derived class in C++ modify the behavior of a function defined in the base
class?

a)Yes, by overriding the function


b) No, derived class cannot modify behavior
c) Yes, by defining a new function with the same name
d) Yes, by using pointers

22. What will happen if a constructor is not explicitly called in a derived class?

a)The default constructor of the base class will be called automatically


b) An error will occur because constructors cannot be inherited
c) The base class constructor will never be called
d) The compiler will not compile the program

23. What is the purpose of virtual inheritance in C++?

a)To ensure that only one instance of a base class is


inherited in case of multiple inheritance
b) To increase the efficiency of inheritance
c) To prevent the derived class from accessing base class methods
d) To allow the derived class to override base class methods

24. What is the order in which constructors are called in case of multiple
inheritance in C++?

a) Constructor of the last base class followed by the constructor of the derived class
b)Constructor of the base classes followed by the constructor of the
derived class
c) Constructor of the derived class followed by the constructors of the base classes
d) The constructor of the derived class is never called

25. Which of the following best describes the concept of "is-a" relationship in
inheritance?
a)It means a derived class can be considered a type of its base class
b) It means a derived class has no relation to the base class
c) It represents a "has-a" relationship
d) It does not apply in C++

26. Which keyword is used to access members of a base class inside a derived class
in C++?

a) this
b) super
c) base
d) ::

27. What is the default visibility of members in a class derived using protected
inheritance?
a) Private
b)Protected
c) Public
d) None of the above

28. If you want to prevent a derived class from overriding a function, which
keyword should be used in the base class?

a) final
b) override
c) virtual
d) static

29. Which of the following is true about destructors in C++ inheritance?

a)The destructor of the derived class is called before the destructor of


the base class
b) Destructors in derived classes are always virtual
c) The base class destructor is not called if the derived class destructor is virtual
d) Destructor calls in inheritance depend on the access modifier

30. Which of the following statements is true about inheritance in C++?

a) A base class can access private members of the derived class


b)A derived class can access public and protected members of the
base class
c) A derived class cannot access any members of the base class
d) A derived class can access private members of the base class

31. Which type of inheritance is being used in the following code?


class A { };
class B : public A {
}; class C : public
B { };

a) Multiple Inheritance
b)Multilevel Inheritance
c) Hybrid Inheritance
d) Single Inheritance

32. In C++, what is the output of a program if a base class constructor is not
called explicitly by the derived class?

a)The default constructor of the base class is called automatically


b) An error occurs
c) The base class constructor is skipped
d) The base class constructor must be explicitly called by the derived class

33. Which of the following statements about constructors and destructors in


inheritance is false?

a) A constructor of a derived class can call the constructor of the base class
b)The destructor of the base class is not called after the derived class
destructor
c) The constructor of the derived class does not need to call the base class constructor
d) The base class destructor should be virtual if dynamic memory is allocated

34. Which of the following is true regarding destructors in C++ inheritance?

a) The destructor of the derived class is called before the destructor of the base class
b) Destructors in derived classes are always virtual
c)The base class destructor is called after the derived class destructor
d) Destructor calls in inheritance depend on the access modifier

35. What happens when the same function is defined in both base and derived
classes?

a) The base class function is called


b)The derived class function is called
c) It causes ambiguity
d) The function will not compile

36. If a class contains only private members and methods, what happens if it is
inherited publicly?

a)The private members of the base class remain private in the derived
class
b) The private members of the base class are accessible in the derived class
c) The private members of the base class are inherited as public
d) The inheritance will fail

37. What is the correct syntax to create a derived class that inherits publicly
from two base classes in C++?

a) class Derived : class Base1, class Base2 { };


b) class Derived : public Base1, public Base2 { };
c) class Derived(Base1, Base2) { };
d) class Derived : Base1, Base2 { };
38. Which of the following statements is true about virtual inheritance?

a)It avoids ambiguity when using multiple inheritance


b) It prevents derived classes from accessing base class members
c) It allows derived classes to inherit from multiple base classes
d) It is only used when there is a private base class

39. Which function is used to dynamically allocate memory for a class object in C+
+?

a) new
b) malloc
c) delete
d) alloc

40. Which of the following keywords is used to restrict the overriding of a


function in a derived class?

a) final
b) private
c) static
d) override

41. How do you make a base class constructor private?

a) private class Base { };


b) class Base { private: Base() { }; };
c) class Base { public: Base() { }; private: };
d) private constructor Base() { };

42. Can a constructor be inherited in C++?

a)Yes
b) No
c) Only if it is public
d) Only if it is protected

43. Which of the following is true regarding an abstract class in C++?

a) It has only abstract methods


b)It cannot be instantiated
c) It must have a constructor
d) It is not allowed in C++

44. In C++, the base class constructor is always called first before the derived class
constructor when an object of the derived class is created. True or False?

a)True
b) False

45. Which of the following is correct about inheritance?

a) Derived classes can access private members of the base class


b)Derived classes can access protected members of the base class
c) A base class can access members of its derived class
d) None of the above

46. Which of the following is true when a class is inherited privately?

a)Public members of the base class are private in the derived class
b) The derived class cannot access the private members of the base class
c) The base class constructor is not called
d) The derived class can access protected members of the base class

47. Can a derived class have a constructor that calls the constructor of the base class
explicitly?

a)Yes
b) No

48. What happens when the same function is defined in both base and derived
classes?

a) The base class function is called


b)The derived class function is called
c) It causes ambiguity
d) The function will not compile

49. Which access specifier makes members of the base class accessible to the
derived class and all other classes?

a) Private
b) Protected
c)Public
d) None of the above

50. What is the purpose of using the virtual keyword with a base class method?

a)To allow the method to be overridden in the derived class


b) To prevent the method from being overridden in the derived class
c) To make the method static
d) To enable function overloading

51. Which of the following will allow polymorphic behavior in inheritance?

a) Using static methods


b)Using virtual methods
c) Declaring all methods as final
d) Declaring constructors as private

52. What happens when a function is marked as final in a base class?

a)It cannot be overridden in derived classes


b) It must be overridden in derived classes
c) It becomes a static function
d) It cannot be called by any class

53. What type of inheritance is shown by the following code snippet?


class A { };
class B : public A { };
class C : public A { };

class D : public B, public C { };

a) Single Inheritance
b)Multiple Inheritance
c) Multilevel Inheritance
d) Hybrid Inheritance

54. Which of the following is NOT inherited by default in C++?

a) Functions
b) Constructors
c)Destructors
d) Static members

55. Can a constructor in a derived class explicitly call the constructor of the base
class?

a)Yes
b) No

56. Which of the following ensures that only one instance of a base class is
inherited in multiple inheritance?
a)Virtual inheritance
b) Static inheritance
c) Protected inheritance
d) Private inheritance

57. Which keyword is used to refer to the base class in the derived class?

a) super
b) this
c) base
d) ::

58. Which of the following is a valid access specifier for base class members in
derived classes?

a) protected
b) private
c) public
d) All of the above

59. Which of the following defines the relationship between a base class and
derived class in C++?

a) Base class is a template for derived class


b) Base class and derived class are unrelated
c) Derived class is an instance of the base class
d) Derived class and base class have an inverse relationship

60. In the context of inheritance, which relationship is described by the phrase "is-
a"?

a)Derived class is a type of the base class


b) Derived class has an instance of the base class
c) Base class has a derived class
d) None of the above

MCQ’S Key

1 a 11 b 21 a 31 b 41 b 51 b
2 b 12 a 22 a 32 a 42 a 52 a
3 d 13 b 23 a 33 b 43 b 53 b
4 b 14 a 24 b 34 c 44 a 54 c
5 d 15 a 25 a 35 b 45 b 55 a
6 a 16 b 26 c 36 a 46 a 56 a
7 b 17 a 27 b 37 b 47 a 57 c
8 d 18 a 28 a 38 a 48 b 58 a
9 a 19 c 29 a 39 a 49 c 59 a
10 a 20 a 30 b 40 a 50 a 60 a

Topic: Levels of Inheritance

1. Which of the following statements is true about multiple inheritance in C++?

a) A class can inherit from multiple base classes


b) A class can inherit from only one base class
c) A class can inherit from multiple derived classes
d) Multiple inheritance is not supported in C++

2. Which keyword is used to implement multiple inheritance in C++?

a) public
b) private
c) protected
d) There is no specific keyword for multiple inheritance

3. In multiple inheritance, if two base classes have a member function with the same
name, which function does the derived class call?
a) The function from the first base class
b) The function from the second base class
c) Both functions will be called
d) The compiler will throw an error

4. What is the key issue associated with multiple inheritance in C++?

a) Constructor conflicts
b) Diamond problem
c) Method overloading
d) Memory inefficiency
5. Which of the following is true about multiple inheritance?

a) C++ does not support multiple inheritance


b) C++ allows a derived class to inherit from more than one base class
c) Only one base class can be inherited
d) Multiple inheritance is only possible if all classes are abstract

6. What does the "virtual" keyword solve in multiple inheritance?

a) It allows constructors to be inherited


b) It solves the diamond problem by ensuring only one copy of the base class is inherited
c) It prevents inheritance from multiple classes
d) It increases the speed of the inheritance process

7. Which of the following is the correct syntax for multiple inheritance in C++?

a) class Derived : public Base1, Base2 {}


b) class Derived : public Base1 + Base2 {}
c) class Derived : Base1, Base2 {}
d) class Derived : Base1, private Base2 {}

8. If a class is derived from two base classes in C++, how many times will the constructor
of the base class be called?
a) Once
b) Twice
c) Three times
d) Only when explicitly called

9. In multiple inheritance, if two base classes have a member with the same name,
the derived class must:
a) Rename one of the members
b) Use scope resolution to call the correct member
c) Remove one of the members
d) Inherit both members without any issues

10. What will happen if you try to access a member variable of the base class directly
in multiple inheritance without using scope resolution?
a) It will access the first base class member variable
b) It will access the second base class member variable
c) The compiler will generate an error
d) The program will run without any issues

11. What is multi-level inheritance in C++?

a) A class inherits from more than one base class


b) A class inherits from another derived class
c) A class cannot inherit from multiple classes
d) A class inherits from a single base class

12. In multi-level inheritance, how many levels of inheritance are involved?

a) Two
b) Three
c) Any number of levels
d) Only two levels are allowed

13. In multi-level inheritance, the derived class at the deepest level can access members
of which classes?
a) Only the base class at the top level
b) Only its immediate base class
c) All classes in the inheritance chain
d) Only the last class in the chain
14. Which of the following is true in multi-level inheritance?

a) A derived class can only inherit from one base class


b) A class can inherit from multiple base classes
c) Multi-level inheritance is not allowed in C++
d) The derived class cannot access the base class's private members

15. How is the constructor of a base class called in multi-level inheritance?

a) The base class constructor is called only in the derived class


b) The base class constructor is called automatically when the derived class object is created
c) The base class constructor is never called
d) The constructor needs to be explicitly called using the scope resolution operator

16. In multi-level inheritance, if the base class has a destructor, which class's destructor
is called first when the derived class object goes out of scope?
a) The base class's destructor
b) The immediate base class's destructor
c) The derived class's destructor
d) The compiler chooses which destructor to call

17. In multi-level inheritance, which member function has higher priority?

a) The member function of the immediate base class


b) The member function of the class that is farthest in the inheritance hierarchy
c) The member function of the topmost base class
d) The member function of the derived class

18. Can a class inherit from multiple classes in a multi-level inheritance scenario in C++?

a) Yes, multi-level inheritance allows inheritance from multiple classes


b) No, multi-level inheritance only allows inheritance from one base class
c) Only abstract classes can be inherited in multi-level inheritance
d) Only one base class can be inherited in multi-level inheritance

19. In multi-level inheritance, what happens if a member is not defined in the derived class?

a) The derived class will inherit the member from the parent class
b) The derived class will ignore the member
c) The derived class will cause a compilation error
d) The derived class will access the member from the most distant ancestor class

20. Which of the following is an example of multi-level inheritance in C++?

a) class Derived : public Base1 {}; class Derived2 : public Derived {};
b) class Derived : public Base {}; class Derived2 {};
c) class Derived : public Base1, Base2 {};
d) class Derived {}; class Derived2 : public Derived {};

21. What happens in a program where both multiple and multi-level inheritance are used?

a) Multiple inheritance will override multi-level inheritance


b) Both can be used together without issue
c) It is not allowed to use both at the same time in C++
d) Multi-level inheritance takes precedence over multiple inheritance

22. In multiple and multi-level inheritance, which class's constructor is called first?

a) The immediate base class's constructor


b) The topmost base class's constructor
c) The derived class's constructor
d) The most derived class's constructor

23. When using both multiple and multi-level inheritance, which of the following is
true about accessing inherited members?
a) The member from the most derived class is always accessed
b) The member from the immediate base class is always accessed
c) The compiler can get confused about which base class to use
d) There is no way to access inherited members

24. Which of the following is a potential drawback of combining multiple and multi-level
inheritance in C++?
a) The code becomes simpler and easier to maintain
b) There can be ambiguity when accessing members or functions
c) It always leads to faster execution
d) It makes the program easier to debug

25. In a situation where a derived class uses both multiple and multi-level
inheritance, which constructor is called last?
a) The constructor of the most base class
b) The constructor of the most derived class
c) The constructor of the immediate base class
d) The constructor of the second derived class

26. Which type of inheritance allows a derived class to inherit from more than one
base class?
a) Single Inheritance
b) Multiple Inheritance
c) Multilevel Inheritance
d) Hierarchical Inheritance

27. In C++, how can multiple inheritance be implemented?

a) By using the ‘virtual’ keyword


b) By inheriting from more than one class
c) By using the ‘private’ access modifier
d) By using function overloading

28. Which of the following inheritance types forms a hierarchy of classes where a derived
class inherits from a base class, and then another derived class inherits from that
derived class?
a) Multiple Inheritance
b) Hierarchical Inheritance
c) Multilevel Inheritance
d) Hybrid Inheritance

29. Which inheritance type combines multiple inheritance and multilevel inheritance?

a) Single Inheritance
b) Hybrid Inheritance
c) Multiple Inheritance
d) Multilevel Inheritance

30. What is the main issue with multiple inheritance in C++?

a) Code duplication
b) Ambiguity
c) Slow execution
d) Memory leakage

31. What is used in C++ to resolve ambiguity in multiple inheritance?

a) Constructor
b) Virtual Inheritance
c) Destructor
d) Destructor Overloading
32. Which keyword is used to denote a derived class inheriting from two base classes in
C++?
a) public
b) virtual
c) friend
d) private

33. What is the output of the following code snippet?

c
l
a
s
s
A

{
p
u
b
li
c
:
void show() { cout << "A"; }
};

class B : public A
{}; class C :
public A {};
class D : public B, public
C {}; int main() {
D obj;
obj.show();
}

a) A
b) B
c) C
d) Compilation error due to ambiguity

34. Which type of inheritance involves a derived class being derived from one base
class, and then another class being derived from the first derived class?
a) Multilevel Inheritance
b) Multiple Inheritance
c) Hierarchical Inheritance
d) Hybrid Inheritance

35. Which of the following is NOT a feature of multiple inheritance in C++?

a) It allows a derived class to inherit from multiple base classes.


b) It can cause ambiguity if not handled properly.
c) It provides better code reusability.
d) It only works with virtual functions.

36. In multilevel inheritance, how many levels of inheritance are there?

a) One
b) Two
c) Two or More than two
d) It depends on the programmer

37. Which of the following is true about multilevel inheritance in C++?

a) A class can inherit from two or more base classes.


b) A class can inherit from a single base class.
c) A class can inherit from another derived class.
d) A base class cannot be derived from another class.

38. What is the advantage of using multiple inheritance in C++?

a) It prevents ambiguity.
b) It enables the derived class to combine the functionality of multiple base classes.
c) It makes the code less complex.
d) It improves memory usage.

39. Which of the following statements about inheritance in C++ is FALSE?

a) Inheritance allows a class to acquire properties and behaviors from another class.
b) Multilevel inheritance occurs when a class inherits from more than one class.
c) In C++, multiple inheritance is supported but should be used carefully.
d) Inheritance can help in code reuse and organization.

40. Which of the following is an example of multilevel inheritance in C++?

a) class B : public A {};


b) class C : public B, public A {};
c) class D : public C {};
d) class A : public B, public C {};

41. In C++, how can the ambiguity of multiple inheritance be avoided?

a) Using friend classes


b) Using virtual inheritance
c) Using static binding
d) Using private inheritance

42. What happens when two classes are derived from a common base class, and a third
class is derived from both of them in C++?
a) Virtual inheritance resolves ambiguity automatically.
b) The derived class may face ambiguity issues.
c) The derived class inherits only from one class.
d) Only the first derived class is inherited.

43. Which of the following is an example of multiple inheritance?

a) class B : public A {};


b) class C : public A, public B {};
c) class D : public C {};
d) class E : public D {};
44. What is the key disadvantage of multiple inheritance?

a) It leads to code duplication.


b) It causes ambiguity and complexity.
c) It increases the memory footprint.
d) It is not supported in C++.

45. Which of the following inheritance types would be most appropriate for a
scenario where one class represents a basic entity, and another class builds on that
entity with additional properties or behaviors?
a) Single Inheritance
b) Multiple Inheritance
c) Multilevel Inheritance
d) Hybrid Inheritance

46. What is multiple inheritance in C++?

a) When a derived class inherits from only one base class


b) When a derived class inherits from more than one base class
c) When a base class is inherited by only one derived class
d) When a base class is inherited by more than one derived class

47. Which of the following is an example of multiple inheritance?

a) class A : public B { };
b) class A : public B, public C { };
c) class A : public B, private C { };
d) class A : private B { };

48. Which of the following is an example of multi-level inheritance?

a) class A : public B { }; class C : public A { };


b) class A : public B, public C { };
c) class A : public B { }; class C : public B { };
d) class A : private B { };

49. What is hybrid inheritance in C++?

a) Combination of multiple inheritance and multi-level inheritance


b) Combination of multiple inheritance and hierarchical inheritance
c) Combination of multi-level inheritance and hierarchical inheritance
d) None of the above

50. Which of the following is an example of hybrid inheritance?

a) class A : public B { }; class C : public A { }; class D : public B, public C { };


b) class A : public B, public C { };
c) class A : public B { }; class C : public B { };
d) class A : private B { };

MCQ’s Key

1 a 11 b 21 b 31 b 41 b
2 d 12 c 22 b 32 b 42 b
3 d 13 c 23 c 33 d 43 b
4 b 14 b 24 b 34 a 44 b
5 b 15 b 25 b 35 d 45 c
6 b 16 c 26 b 36 c 46 b
7 a 17 d 27 b 37 c 47 b
8 b 18 b 28 c 38 b 48 a
9 b 19 a 29 b 39 b 49 a
10 c 20 a 30 b 40 c 50 a

Topic: Virtual Functions

1. Which keyword is used to declare a virtual function in C++?


a) virtual
b) override
c) dynamic
d) function

2. What is the main purpose of a virtual function in C++?


a) To improve execution speed
b) To enable function overloading
c) To allow runtime polymorphism
d) To allocate memory dynamically

3. Which of the following is true about virtual functions?


a) They must be defined in the derived class
b) They cannot be overridden
c) They support dynamic binding
d) They cannot be inherited

4. How is a virtual function resolved at runtime?


a) By static binding
b) By function overloading
c) Using a virtual table (vtable)
d) By function overriding

5. What happens if a virtual function is called using a base class pointer that points to
a derived class object?
a) Base class function is always called
b) Derived class function is called if it overrides the base function
c) Compilation error occurs
d) Runtime error occurs

6. What is a pure virtual function?


a) A function with no parameters
b) A function that must be overridden in derived classes
c) A function with a default argument
d) A function that can be called without instantiating a class
7. How do you define a pure virtual function in C++?
a) void show() = 0;
b) virtual void show() {}
c) void show();
d) pure virtual void show();

8. What is an abstract class in C++?


a) A class that contains at least one pure virtual function
b) A class that cannot have a constructor
c) A class that cannot have member functions
d) A class that must have only static members
9. If a class has at least one pure virtual function, then:
a) It must be instantiated
b) It cannot be inherited
c) It cannot have any constructors
d) It cannot be instantiated directly

10. What is a virtual destructor used for?


a) To destroy objects statically
b) To prevent memory leaks in polymorphic base classes
c) To prevent derived class objects from being deleted
d) To override base class constructors

11. Can a constructor be declared as virtual?


a) Yes
b) No
c) Only in derived classes
d) Only if the base class is abstract

12. What happens if a class has a virtual function but no virtual destructor?
a) The program does not compile
b) Derived class objects may not be properly deleted
c) All virtual functions become non-virtual
d) The base class cannot be inherited

13. What is the size of an object of a class that contains only a virtual function
(assuming no other members)?
a) 0 bytes
b) 4 bytes (or 8 bytes in 64-bit systems)
c) 1 byte
d) 2 bytes

14. If a virtual function is redefined in a derived class, which function is executed


when called through a base class reference?
a) Base class version
b) Derived class version
c) It depends on the compiler
d) It results in an error

15. What is the role of a virtual table (vtable) in C++?


a) It stores objects dynamically
b) It keeps track of virtual functions for a class
c) It is used for function overloading
d) It manages memory allocation

16. Which of the following statements about virtual functions is false?


a) Virtual functions can be called from a base class pointer
b) A derived class can override a virtual function
c) Virtual functions are resolved at compile-time
d) A virtual function can be made pure virtual

17. What is the default behavior of a virtual function if it is not overridden in a


derived class?
a) The base class version is used
b) Compilation error occurs
c) A runtime error occurs
d) The function is skipped
18. What happens if a base class declares a virtual function but a derived class does not
override it?
a) The function becomes private
b) The base class version is called
c) A segmentation fault occurs
d) The program does not compile

19. Which of the following is true about virtual functions in multiple inheritance?
a) Each base class maintains its own vtable
b) Only one vtable is created for all base classes
c) Virtual functions cannot be used in multiple inheritance
d) Virtual functions cause compilation errors in multiple inheritance

20. Can a virtual function have a default argument?


a) Yes
b) No
c) Only if declared in the derived class
d) Only if declared outside the class

21. What is the key advantage of using virtual functions in C++?


a) Faster execution
b) Reduced memory usage
c) Achieving runtime polymorphism
d) Enabling function overloading

22. Which of the following statements about virtual functions is correct?


a) Virtual functions are resolved at compile-time
b) Virtual functions enable static binding
c) Virtual functions allow dynamic dispatch
d) Virtual functions cannot be inherited

23. What happens if a derived class does not override a virtual function?
a) Compilation error occurs
b) The base class version is used
c) The derived class cannot be instantiated
d) Runtime error occurs

24. Which of the following must be declared as virtual to achieve runtime polymorphism?
a) Constructor
b) Member function
c) Destructor
d) Static function

25. What is the output of calling a virtual function from a base class pointer pointing to
a derived class object?
a) Base class version is always called
b) Derived class version is called if overridden
c) Compilation error occurs
d) The program crashes

26. What is a virtual pointer (vptr) in C++?


a) A pointer to virtual functions
b) A pointer to store the address of the vtable
c) A special pointer used for object copying
d) A function that manages memory allocation

27. What is the purpose of a virtual function table (vtable)?


a) Stores addresses of all functions in a program
b) Stores addresses of virtual functions for a class
c) Stores memory addresses of class objects
d) Stores addresses of function arguments

28. Can a virtual function be private in C++?


a) Yes, but it cannot be accessed directly
b) No, it must be public or protected
c) Only if declared as static
d) Only in abstract classes

29. What is the effect of making a destructor virtual in C++?


a) Prevents memory leaks in polymorphic base classes
b) Increases the speed of object deletion
c) Prevents objects from being deleted
d) Forces the compiler to inline the destructor

30. Can a virtual function have a default implementation in the base class?
a) Yes
b) No
c) Only if declared as pure virtual
d) Only if the derived class does not override it

31. What is the primary reason for using a virtual destructor?


a) To delete derived class objects correctly when using a base class pointer
b) To improve execution speed
c) To allow function overloading
d) To avoid function overriding

32. Which of the following is true about virtual functions and inheritance?
a) A virtual function in a base class can be overridden in a derived class
b) Virtual functions cannot be inherited
c) Virtual functions must always be overridden
d) A virtual function can only be used in single inheritance

33. What happens if a class has only private virtual functions?


a) The class cannot be instantiated
b) The functions can only be accessed within the class
c) The derived class cannot override the virtual functions
d) The program will not compile

34. Can a static function be virtual?


a) Yes
b) No
c) Only if declared inside an abstract class
d) Only in multiple inheritance

35. What happens if a base class has a virtual function and a derived class redefines it
without using virtual?
a) The function remains virtual in the derived class
b) The function loses its virtual property in the derived class
c) Compilation error occurs
d) The program crashes at runtime

36. Can a pure virtual function have an implementation in the base class?
a) Yes
b) No
c) Only in final classes
d) Only if the derived class does not override it

37. What is the main difference between a virtual function and a non-virtual function?
a) Virtual functions support dynamic binding, while non-virtual functions use static binding
b) Virtual functions cannot be overridden
c) Non-virtual functions are always faster
d) Virtual functions must be static

38. If a derived class does not provide an override for a pure virtual function, what
happens?
a) The program does not compile
b) The base class version is used
c) The function is ignored
d) The function remains uninitialized

39. Which of the following is a correct declaration of a pure virtual function?


a) virtual void show() = 0;
b) void show() = virtual;
c) virtual = 0 void show();
d) virtual show() = 0;

40. What happens if a virtual function is declared as final in a base class?


a) It cannot be overridden in derived classes
b) It becomes static
c) It can only be overridden once
d) It is removed from the vtable

41. Can a derived class override a virtual function and make it non-virtual?
a) No, once declared virtual, it remains virtual in derived classes
b) Yes, by removing the virtual keyword
c) Only if the base class allows it
d) Only if using private inheritance

42. What is the effect of overriding a virtual function in a derived class?


a) The function call is resolved at runtime
b) The function call is resolved at compile-time
c) The function call is always static
d) The function call is ignored

43. Can a constructor call a virtual function?


a) No, because virtual functions are not resolved dynamically in constructors
b) Yes, and it behaves normally
c) Only if explicitly specified
d) Only if the function is pure virtual

44. How does C++ ensure proper destruction of objects with virtual functions?
a) Using virtual destructors
b) Using function pointers
c) Using static functions
d) Using friend functions

45. What happens when an object with virtual functions is copied?


a) The vptr is copied along with the object
b) The vptr is reset to the base class
c) The vtable is copied
d) The object cannot be copied

46. How can a virtual function be prevented from being overridden?


a) By declaring it as final
b) By making it static
c) By using private access
d) By declaring it as inline

47. Which of the following statements is true about virtual functions and inline functions?
a) Virtual functions cannot be inlined
b) Inline functions cannot be virtual
c) Virtual functions can be inlined but are resolved at runtime
d) Inline functions are resolved at runtime

48. What is the return type of a virtual function in C++?


a) It can be any valid return type
b) It must always return void
c) It can only return pointers
d) It must return a base class type

49. Can a virtual function return a different type in a derived class?


a) Yes, if it follows covariance
b) No, it must return the exact same type
c) Only if the base class allows it
d) Only if the function is pure virtual

50. What is the impact of using virtual functions on object size?


a) It increases due to the vptr
b) It decreases due to optimization
c) It remains the same
d) It depends on the compiler
MCQ’s Key

1 a 11 b 21 c 31 a 41 a
2 c 12 b 22 c 32 a 42 a
3 c 13 b 23 b 33 b 43 a
4 c 14 b 24 b 34 b 44 a
5 b 15 b 25 b 35 b 45 b
6 b 16 c 26 b 36 a 46 a
7 a 17 a 27 b 37 a 47 c
8 a 18 b 28 a 38 a 48 a
9 d 19 a 29 a 39 a 49 a
10 b 20 a 30 a 40 a 50 a

You might also like