Breaking

BCA 2nd Sem -Object Oriented Programming Using C++ UNIT-IV MCQ

 

BCA 2nd Sem -Object Oriented Programming Using C++ UNIT-IV MCQ


  • UNIT-I 
   -Introducing Object  Oriented Approach                -Relating to other paradigms {Functional, Data decomposition}                         - Basic terms and ideas Abstraction, Encapsulation, Inheritance, Polymorphism  - - Review of C                            -Difference between C and C++ - cin, cout, new, delete, operators
   .
 Unit-1 MCQ's
  • UNIT-II 
  • - Classes and Objects Encapsulation                                   -information hiding,                 - abstract data types,                  -Object & classes                   -attributes                                       -methods                                -C++ class declaration               -State idendity and behaviour of an object                                  -Constructors and destructors,                                -instantiation of objects,            -Default parameter value,         -object types,                             -C++ garbage collection,        -dynamic memory allocation,                -abstract classes.
    Unit-2 MCQ's
  • UNIT-III 
  •  - Inheritance and Polymorphism Inheritance,     -Class hierarchy,                     -derivation – public, private & protected,                              -Aggregation,                            - composition vs classification hierarchies,                               -Polymorphism,                       -Categorization of polymorphism techniques,      -Method polymorphism,         - Operator overloading                 
    Unit-3 MCQ's
  • UNIT-IV 
  • -Generic function Template function,                                    -function name overloading,    -Overriding inheritance methods,                                    -Run time polymorphism,       -Multiple Inheritance

  • UNIT-V                 
  • - Files and Exception Handling Streams and files,                                                                 - Exception handling

    Unit-5 MCQ's

    Object Oriented Programming Using C++ 

    1. What is a template function in C++?

    a) A function that can accept only integer arguments
    b) A function that can work with different data types
    c) A function that cannot return any value
    d) A function with a fixed return type
    Answer: b) A function that can work with different data types


    2. Which keyword is used to define a template function in C++?

    a) template
    b) generic
    c) typename
    d) class
    Answer: a) template


    3. What is the correct syntax for defining a function template?

    a) template <typename T> T func(T a, T b);
    b) generic <class T> T func(T a, T b);
    c) class <typename T> T func(T a, T b);
    d) template generic <T> T func(T a, T b);
    Answer: a) template <typename T> T func(T a, T b);


    4. What is the primary purpose of function templates?

    a) To write multiple functions with different names
    b) To allow a single function to handle multiple data types
    c) To improve memory allocation
    d) To replace pointers in C++
    Answer: b) To allow a single function to handle multiple data types


    5. How do we define multiple template parameters in a function template?

    a) template <class T, class U>
    b) template (class T, class U)
    c) template {class T, class U}
    d) template [class T, class U]
    Answer: a) template <class T, class U>


    6. What is the difference between typename and class in templates?

    a) class is used only for classes, while typename is for primitive types
    b) There is no difference; both can be used interchangeably
    c) typename is required for non-type template parameters
    d) class is preferred in C++17, while typename is outdated
    Answer: b) There is no difference; both can be used interchangeably


    7. Which of the following is TRUE about function templates?

    a) Function templates must have at least one parameter
    b) Function templates can only return void
    c) Function templates allow writing generic code for multiple types
    d) Function templates cannot be overloaded
    Answer: c) Function templates allow writing generic code for multiple types


    8. How is a function template instantiated?

    a) Automatically when it is called with a specific data type
    b) Manually using the new keyword
    c) By defining it inside a class
    d) Using the class keyword
    Answer: a) Automatically when it is called with a specific data type


    9. What happens if a function template is called with a data type that it does not support?

    a) The compiler generates an error
    b) The function executes with a default type
    c) The program crashes at runtime
    d) The function ignores the unsupported data type
    Answer: a) The compiler generates an error


    10. Can function templates be overloaded?

    a) No, function templates cannot be overloaded
    b) Yes, but only with non-template functions
    c) Yes, function templates can be overloaded
    d) Only in C++17 and later versions
    Answer: c) Yes, function templates can be overloaded


    11. What is the correct way to call a function template explicitly?

    a) func<int>(10, 20);
    b) func<int, 10, 20>;
    c) func(10, 20) <int>;
    d) func<int> {10, 20};
    Answer: a) func<int>(10, 20);


    12. What is a non-type template parameter?

    a) A template parameter that is a constant value instead of a type
    b) A template parameter that only supports integer types
    c) A template parameter that stores function pointers
    d) A template parameter used in virtual functions
    Answer: a) A template parameter that is a constant value instead of a type


    13. Can function templates return a value?

    a) Yes, they can return any data type
    b) No, they always return void
    c) Yes, but only integer values
    d) Only if they use static keyword
    Answer: a) Yes, they can return any data type


    14. What is template specialization?

    a) A technique to provide a specific implementation for a particular data type
    b) A way to declare multiple templates in one function
    c) A feature used for operator overloading
    d) A method for dynamic memory allocation
    Answer: a) A technique to provide a specific implementation for a particular data type


    15. Can a function template have a default parameter?

    a) Yes, it can have default parameters
    b) No, default parameters are not allowed in templates
    c) Only if the function is inline
    d) Only if it is a non-member function
    Answer: a) Yes, it can have default parameters


    16. What is the correct way to define a function template for swapping two values?

    a)


    template <typename T> void swapValues(T &a, T &b) { T temp = a; a = b; b = temp; }

    b)


    void swapValues(T a, T b) { T temp = a; a = b; b = temp; }

    c)


    template swapValues(T a, T b) { T temp = a; a = b; b = temp; }

    d)


    class T swapValues(T a, T b) { T temp = a; a = b; b = temp; }

    Answer: a)


    17. What is function template instantiation?

    a) Creating a specific version of the template function for a given data type
    b) Deleting a function template from memory
    c) Overloading a function template
    d) Using a function template inside a class
    Answer: a) Creating a specific version of the template function for a given data type


    18. Can a function template have multiple parameters of different types?

    a) Yes
    b) No
    Answer: a) Yes


    19. What is the advantage of using function templates?

    a) Reduces code duplication
    b) Increases memory usage
    c) Slows down program execution
    d) Complicates debugging
    Answer: a) Reduces code duplication


    20. What happens if a function template is not used in a program?

    a) The compiler does not generate any code for it
    b) The compiler generates a warning
    c) The program crashes
    d) The function template gets deleted automatically
    Answer: a) The compiler does not generate any code for it

    21. Which of the following statements is true about function templates?

    a) They can only be defined in the main() function.
    b) They can have multiple template parameters.
    c) They cannot be overloaded.
    d) They must always return void.
    Answer: b) They can have multiple template parameters.


    22. What happens if a function template is defined but never used in a program?

    a) The compiler will generate an error.
    b) The function will still be compiled and added to the executable.
    c) The function template will not be instantiated.
    d) The linker will remove it during optimization.
    Answer: c) The function template will not be instantiated.


    23. What is the correct syntax for using a template function explicitly for double type?

    a) func<double>(10.5, 20.5);
    b) func(10.5, 20.5)<double>;
    c) func<double>{10.5, 20.5};
    d) func(double 10.5, double 20.5);
    Answer: a) func<double>(10.5, 20.5);


    24. Can a function template accept both template and non-template parameters?

    a) Yes
    b) No
    Answer: a) Yes


    25. What does the following function template do?

    template <typename T>
    T square(T x) { return x * x; }

    a) Squares the given value of any data type
    b) Returns the square root of the input
    c) Multiplies x by itself only if x is an integer
    d) Causes a compilation error
    Answer: a) Squares the given value of any data type


    26. Which of the following is true about function templates?

    a) They reduce code duplication.
    b) They increase compilation time.
    c) They cannot be used for user-defined types.
    d) They require explicit specialization for all types.
    Answer: a) They reduce code duplication.


    27. Can function templates be used with user-defined types like struct or class?

    a) Yes
    b) No
    Answer: a) Yes


    28. What is an explicit specialization of a function template?

    a) Providing a specific implementation for a certain data type.
    b) Using explicit keyword inside a template function.
    c) Making a function template private.
    d) Preventing a function template from being instantiated.
    Answer: a) Providing a specific implementation for a certain data type.


    29. What is an advantage of using function templates?

    a) They allow generic programming.
    b) They eliminate the need for function overloading.
    c) They improve code reusability.
    d) All of the above.
    Answer: d) All of the above.


    30. What does this code do?

    template <class T>
    T add(T a, T b) { return a + b; } int main() { cout << add(10, 20) << endl; }

    a) Prints 30
    b) Causes a runtime error
    c) Prints 1020
    d) Causes a compilation error
    Answer: a) Prints 30


    31. What happens when a function template is instantiated with different data types?

    a) The compiler generates a separate function for each type.
    b) The same function instance is used for all data types.
    c) The template cannot be instantiated with different types.
    d) It results in an error.
    Answer: a) The compiler generates a separate function for each type.


    32. What is function template overloading?

    a) Defining multiple function templates with different parameter lists.
    b) Using multiple templates inside a single function.
    c) Calling a function template multiple times.
    d) Using #define macros instead of templates.
    Answer: a) Defining multiple function templates with different parameter lists.


    33. What is the main difference between function overloading and function templates?

    a) Function overloading requires multiple definitions, whereas templates generate them automatically.
    b) Function templates are faster than function overloading.
    c) Function overloading works only for primitive data types.
    d) Function templates always return void.
    Answer: a) Function overloading requires multiple definitions, whereas templates generate them automatically.


    34. Which of the following can be a non-type template parameter?

    a) int
    b) char
    c) bool
    d) All of the above
    Answer: d) All of the above


    35. Which of the following is an invalid function template declaration?

    a) template <class T> T func(T a, T b);
    b) template <typename T> void print(T a);
    c) template (class T) T sum(T a, T b);
    d) template <class T, class U> T add(T a, U b);
    Answer: c) template (class T) T sum(T a, T b);


    36. Can we create a template function inside a class?

    a) Yes
    b) No
    Answer: a) Yes


    37. How can we restrict a function template to work with only integer types?

    a) Using enable_if or explicit specialization
    b) Using the restrict keyword
    c) It is not possible
    d) Using const keyword
    Answer: a) Using enable_if or explicit specialization


    38. Can function templates be recursive?

    a) Yes
    b) No
    Answer: a) Yes


    39. What happens if a function template has a static variable?

    a) Each instantiation gets its own copy of the variable.
    b) The same static variable is shared among all instances.
    c) The function cannot be instantiated.
    d) Causes a compilation error.
    Answer: a) Each instantiation gets its own copy of the variable.


    40. What is a variadic template function?

    a) A template function that can accept a variable number of arguments.
    b) A template function that returns a void type.
    c) A template function that only works with integer types.
    d) A template function that must be explicitly specialized.
    Answer: a) A template function that can accept a variable number of arguments.

    41. What is function overloading in C++?

    a) Defining multiple functions with the same name but different parameters.
    b) Defining multiple functions with the same name and same parameters.
    c) Using virtual functions in derived classes.
    d) Defining a function more than once in the same scope.
    Answer: a) Defining multiple functions with the same name but different parameters.


    42. What must be different in overloaded functions?

    a) Return type
    b) Function name
    c) Number or type of parameters
    d) All of the above
    Answer: c) Number or type of parameters


    43. Can function overloading be based only on return type?

    a) Yes
    b) No
    Answer: b) No


    44. What is the correct way to overload functions?

    a)

    int add(int a, int b);
    float add(float a, float b);

    b)

    int add(int a, int b);
    int add(int x, int y);

    c)

    int add(int a, int b);
    int add(int a, float b);

    d) Both (a) and (c)
    Answer: d) Both (a) and (c)


    45. What will happen if two functions have the same name and same parameters?

    a) Compile-time error
    b) Runtime error
    c) Overloading will work normally
    d) The program will not compile
    Answer: a) Compile-time error


    46. Which of the following is an example of valid function overloading?

    a)

    void show(int a);
    void show(double b);

    b)

    int show(int a);
    void show(int a);

    c)

    void show(int a);
    void show(int a, int b);

    d) Both (a) and (c)
    Answer: d) Both (a) and (c)


    47. Function overloading is an example of:

    a) Dynamic polymorphism
    b) Static polymorphism
    c) Encapsulation
    d) None of the above
    Answer: b) Static polymorphism


    48. What is the main advantage of function overloading?

    a) Improves readability
    b) Reduces execution time
    c) Reduces memory consumption
    d) None of the above
    Answer: a) Improves readability


    49. Can constructors be overloaded in C++?

    a) Yes
    b) No
    Answer: a) Yes


    50. Can destructors be overloaded in C++?

    a) Yes
    b) No
    Answer: b) No


    51. What happens if overloaded functions have default arguments?

    a) Compilation error
    b) The compiler may face ambiguity
    c) The program executes normally
    d) Function overloading is not allowed with default arguments
    Answer: b) The compiler may face ambiguity


    52. Function overloading in C++ is resolved at:

    a) Compile time
    b) Runtime
    c) Linking time
    d) Execution time
    Answer: a) Compile time


    53. Can overloaded functions be defined in different classes?

    a) Yes
    b) No
    Answer: a) Yes


    54. Which function is selected when multiple overloaded functions match a call?

    a) The most specific match
    b) The first function in the code
    c) The function with the largest parameter size
    d) The function with the smallest parameter size
    Answer: a) The most specific match


    55. Can a base class and derived class have overloaded functions with the same name?

    a) Yes
    b) No
    Answer: a) Yes


    56. How does function overloading relate to polymorphism?

    a) It is an example of runtime polymorphism
    b) It is an example of compile-time polymorphism
    c) It is unrelated to polymorphism
    d) It depends on the implementation
    Answer: b) It is an example of compile-time polymorphism


    57. Which of the following cannot be overloaded?

    a) Constructors
    b) Destructors
    c) Member functions
    d) Operators
    Answer: b) Destructors


    58. Can function overloading be used with friend functions?

    a) Yes
    b) No
    Answer: a) Yes


    59. Function overloading depends on:

    a) The number of parameters
    b) The type of parameters
    c) The sequence of parameters
    d) All of the above
    Answer: d) All of the above


    60. Can overloaded functions have different return types?

    a) Yes, if the function signatures are different
    b) No, return type alone cannot differentiate overloaded functions
    Answer: b) No, return type alone cannot differentiate overloaded functions


    61. What is the role of function overloading in OOP?

    a) It allows the same function name to be used for different tasks
    b) It helps in runtime binding
    c) It makes the code less readable
    d) It does not affect code quality
    Answer: a) It allows the same function name to be used for different tasks


    62. In C++, which function overloading is incorrect?

    a)

    void test(int);
    void test(double);

    b)

    int test(int);
    void test(int);

    c)

    void test(int, double);
    void test(double, int);

    d) None of the above
    Answer: b)

    int test(int);
    void test(int);

    (Overloading cannot be based only on return type)


    63. Can we overload a function using a pointer as a parameter?

    a) Yes
    b) No
    Answer: a) Yes


    64. Which of the following statements is correct?

    a) Function overloading decreases performance
    b) Function overloading increases program complexity
    c) Function overloading makes a program easier to read and maintain
    d) Function overloading has no benefits
    Answer: c) Function overloading makes a program easier to read and maintain


    65. What happens when an overloaded function is called with different argument types?

    a) The compiler selects the best match
    b) It causes a runtime error
    c) It causes a syntax error
    d) It results in undefined behavior
    Answer: a) The compiler selects the best match

    66. What is method overriding in C++?

    a) Declaring a function with the same name in the same class
    b) Declaring a function in a derived class that has the same signature as in the base class
    c) Declaring multiple functions with the same name in the same scope
    d) Using different return types for overloaded functions
    Answer: b) Declaring a function in a derived class that has the same signature as in the base class


    67. When does method overriding occur?

    a) When a derived class redefines a base class function with the same signature
    b) When two functions have the same name but different parameters
    c) When a function is redefined in the same class
    d) When a function is declared static
    Answer: a) When a derived class redefines a base class function with the same signature


    68. Which keyword is used to prevent a method from being overridden in C++?

    a) override
    b) static
    c) final
    d) const
    Answer: c) final


    69. Which access specifier is required for a base class function to be overridden in a derived class?

    a) private
    b) public or protected
    c) static
    d) global
    Answer: b) public or protected


    70. Can a derived class override a private method of the base class?

    a) Yes, always
    b) No, because private methods are not accessible to derived classes
    c) Yes, but only if the derived class is in the same namespace
    d) Yes, if the derived class uses the friend keyword
    Answer: b) No, because private methods are not accessible to derived classes


    71. What happens if a function in a derived class has the same name as in the base class but with different parameters?

    a) It overrides the base class function
    b) It overloads the base class function instead of overriding
    c) It generates a compilation error
    d) It hides the base class function
    Answer: b) It overloads the base class function instead of overriding


    72. What is the role of the virtual keyword in method overriding?

    a) It allows a method to be overridden in the derived class
    b) It prevents a method from being overridden
    c) It makes a method static
    d) It forces early binding
    Answer: a) It allows a method to be overridden in the derived class


    73. What happens if a base class function is not declared as virtual but is overridden in a derived class?

    a) Runtime polymorphism is achieved
    b) The base class function is hidden but not overridden
    c) A compilation error occurs
    d) The derived class function cannot be called
    Answer: b) The base class function is hidden but not overridden


    74. What is the correct way to override a function in C++?

    a) By using the virtual keyword in the base class function declaration
    b) By using the override keyword in the derived class
    c) By using the static keyword in the derived class
    d) By using the final keyword in the base class
    Answer: a) By using the virtual keyword in the base class function declaration


    75. What is the purpose of the override keyword in C++11?

    a) It forces a function to be overridden in the derived class
    b) It explicitly marks a function as overriding a base class function
    c) It prevents method overriding
    d) It is used to call a function from the base class
    Answer: b) It explicitly marks a function as overriding a base class function


    76. Which of the following is NOT true about method overriding?

    a) Overriding occurs in inheritance
    b) The base class function should be virtual
    c) The derived class function should have a different name
    d) The function signatures must be the same
    Answer: c) The derived class function should have a different name


    77. How can you call an overridden method of the base class from the derived class?

    a) Using super keyword
    b) Using base keyword
    c) Using BaseClass::methodName()
    d) Using override keyword
    Answer: c) Using BaseClass::methodName()


    78. What is the output of the following code?

    #include <iostream>
    using namespace std; class Base { public: virtual void show() { cout << "Base Class" << endl; } }; class Derived : public Base { public: void show() override { cout << "Derived Class" << endl; } }; int main() { Base* b = new Derived(); b->show(); return 0; }

    a) Base Class
    b) Derived Class
    c) Compilation error
    d) Runtime error
    Answer: b) Derived Class


    79. What will happen if a base class function is declared virtual but is not overridden in the derived class?

    a) The base class function will be called
    b) A compilation error occurs
    c) The derived class will generate an exception
    d) The function cannot be accessed
    Answer: a) The base class function will be called


    80. Can a constructor be overridden in C++?

    a) Yes
    b) No
    Answer: b) No


    81. What happens if a derived class function has a different return type than the base class function?

    a) It overrides the base class function
    b) It results in a compilation error
    c) It creates an overloaded function
    d) It hides the base class function
    Answer: b) It results in a compilation error


    82. What happens when a function is overridden but the base class function is not virtual?

    a) The base class function will be hidden
    b) The base class function will be overridden
    c) A runtime error occurs
    d) The derived class function will not be called
    Answer: a) The base class function will be hidden


    83. Which of the following can be overridden?

    a) Static functions
    b) Virtual functions
    c) Constructors
    d) Private functions
    Answer: b) Virtual functions


    84. What is pure virtual function in C++?

    a) A function that has no implementation and must be overridden in derived classes
    b) A function that is only accessible in the base class
    c) A function that cannot be inherited
    d) A function that is not allowed in a base class
    Answer: a) A function that has no implementation and must be overridden in derived classes


    85. How can you prevent a function from being overridden in C++?

    a) By declaring it as private
    b) By using final keyword
    c) By declaring it as static
    d) By using override keyword
    Answer: b) By using final keyword

    86. What is run-time polymorphism in C++?

    a) Function overloading
    b) Operator overloading
    c) Method overriding using virtual functions
    d) Inline functions
    Answer: c) Method overriding using virtual functions


    87. Which of the following concepts allows run-time polymorphism in C++?

    a) Function overloading
    b) Virtual functions
    c) Friend functions
    d) Static functions
    Answer: b) Virtual functions


    88. Which of the following keywords is required to achieve run-time polymorphism?

    a) static
    b) final
    c) virtual
    d) const
    Answer: c) virtual


    89. Which of the following statements is true about run-time polymorphism?

    a) It is achieved using function overloading
    b) It is resolved at compile-time
    c) It is achieved using virtual functions and function overriding
    d) It does not use pointers
    Answer: c) It is achieved using virtual functions and function overriding


    90. What is required for a function to support run-time polymorphism?

    a) It must be a global function
    b) It must be a static function
    c) It must be declared virtual in the base class
    d) It must be declared as inline
    Answer: c) It must be declared virtual in the base class


    91. When is the function that is overridden in a derived class executed?

    a) At compile-time
    b) At run-time
    c) At link-time
    d) At design-time
    Answer: b) At run-time


    92. What does the following code print?

    #include <iostream>
    using namespace std; class Base { public: virtual void show() { cout << "Base class" << endl; } }; class Derived : public Base { public: void show() override { cout << "Derived class" << endl; } }; int main() { Base* b = new Derived(); b->show(); return 0; }

    a) Base class
    b) Derived class
    c) Compilation error
    d) Runtime error
    Answer: b) Derived class


    93. How is run-time polymorphism achieved in C++?

    a) Function overloading
    b) Operator overloading
    c) Virtual functions and pointers
    d) Friend functions
    Answer: c) Virtual functions and pointers


    94. What is the output of the following code?

    #include <iostream>
    using namespace std; class A { public: virtual void display() { cout << "A"; } }; class B : public A { public: void display() { cout << "B"; } }; int main() { A* obj = new B(); obj->display(); return 0; }

    a) A
    b) B
    c) Compilation error
    d) Runtime error
    Answer: b) B


    95. What is dynamic binding in C++?

    a) Binding function calls at compile-time
    b) Binding function calls at run-time
    c) Using const keyword to prevent changes
    d) Using friend functions
    Answer: b) Binding function calls at run-time


    96. Which function type does NOT support run-time polymorphism?

    a) Virtual functions
    b) Static functions
    c) Overridden functions
    d) Base class functions
    Answer: b) Static functions


    97. Can a static function be virtual in C++?

    a) Yes
    b) No
    Answer: b) No


    98. How does C++ determine which function to execute in run-time polymorphism?

    a) Using function overloading
    b) Using early binding
    c) Using vtable (virtual table) mechanism
    d) Using default arguments
    Answer: c) Using vtable (virtual table) mechanism


    99. Which of the following is NOT necessary for run-time polymorphism?

    a) Virtual function in base class
    b) Function with same signature in derived class
    c) Object slicing
    d) Base class pointer or reference
    Answer: c) Object slicing


    100. What happens when a virtual function is not overridden in the derived class?

    a) The base class function is called
    b) A compilation error occurs
    c) A runtime error occurs
    d) The function cannot be accessed
    Answer: a) The base class function is called


    101. What is a pure virtual function?

    a) A function with no implementation in the base class
    b) A function that is overridden in the same class
    c) A function that cannot be inherited
    d) A function that cannot be overridden
    Answer: a) A function with no implementation in the base class


    102. What is the syntax of a pure virtual function?

    a) virtual void show();
    b) virtual void show() = 0;
    c) void show() = 0;
    d) void virtual show() {}
    Answer: b) virtual void show() = 0;


    103. Which of the following statements is true?

    a) Virtual functions can be called using object names
    b) Virtual functions are used for early binding
    c) Virtual functions allow dynamic method dispatch
    d) Virtual functions cannot be overridden
    Answer: c) Virtual functions allow dynamic method dispatch


    104. What will happen if a pure virtual function is not implemented in a derived class?

    a) The derived class becomes an abstract class
    b) The base class function is called
    c) A runtime error occurs
    d) Compilation fails
    Answer: a) The derived class becomes an abstract class


    105. What is the default return type of a virtual function?

    a) void
    b) int
    c) Same as the overridden function
    d) No default type
    Answer: c) Same as the overridden function


    106. Can a destructor be virtual?

    a) Yes
    b) No
    Answer: a) Yes


    107. What will happen if a base class destructor is not virtual?

    a) Memory leaks may occur
    b) The derived class destructor is called first
    c) The base class destructor is called first
    d) The program crashes
    Answer: a) Memory leaks may occur


    108. What is the vtable in C++?

    a) A table storing virtual functions of a class
    b) A table storing function overloading information
    c) A table storing constructor definitions
    d) A table storing friend functions
    Answer: a) A table storing virtual functions of a class


    109. How can you prevent a class from being inherited in C++?

    a) By declaring it private
    b) By using final keyword
    c) By using static keyword
    d) By using virtual keyword
    Answer: b) By using final keyword


    110. What is function overriding?

    a) Defining multiple functions with the same name in the same scope
    b) Redefining a base class function in a derived class with the same signature
    c) Using a function with different parameter types
    d) Using a function with different return types
    Answer: b) Redefining a base class function in a derived class with the same signature

    111. What is multiple inheritance in C++?

    a) A class inheriting from multiple base classes
    b) A class inheriting from a single base class
    c) A function overloading mechanism
    d) A template class inheritance
    Answer: a) A class inheriting from multiple base classes


    112. How do you declare multiple inheritance in C++?

    a) class Derived : public Base1, Base2
    b) class Derived : Base1, public Base2
    c) class Derived : public Base1, public Base2
    d) class Derived (Base1, Base2)
    Answer: c) class Derived : public Base1, public Base2


    113. What is the main drawback of multiple inheritance?

    a) It increases code reusability
    b) It increases the execution speed
    c) It can lead to the diamond problem
    d) It makes class structure simpler
    Answer: c) It can lead to the diamond problem


    114. Which keyword helps solve the diamond problem in multiple inheritance?

    a) virtual
    b) static
    c) protected
    d) friend
    Answer: a) virtual


    115. In multiple inheritance, which constructor is executed first?

    a) Derived class constructor
    b) The constructor of the first base class specified
    c) The constructor of the last base class specified
    d) The constructor of the derived class only
    Answer: b) The constructor of the first base class specified


    116. What happens when a function with the same name exists in both base classes?

    a) Compilation error
    b) Ambiguity error unless resolved in the derived class
    c) The function in the first base class is called
    d) The function in the last base class is called
    Answer: b) Ambiguity error unless resolved in the derived class


    117. How can function ambiguity be resolved in multiple inheritance?

    a) By using scope resolution operator (::)
    b) By using friend functions
    c) By declaring the function static
    d) By overloading the function
    Answer: a) By using scope resolution operator (::)


    118. What is the correct way to call a function from a specific base class?

    Derived obj;
    obj.function();

    a) obj.function();
    b) Base1::function();
    c) obj.Base1::function();
    d) Base1 obj.function();
    Answer: c) obj.Base1::function();


    119. What is the syntax to declare a virtual base class in C++?

    a) class A : virtual B
    b) class A : public virtual B
    c) class A { virtual B; }
    d) class A : B virtual
    Answer: b) class A : public virtual B


    120. What is the role of a virtual base class?

    a) To allow the derived class to access private members of the base class
    b) To allow multiple instances of the base class in the inheritance hierarchy
    c) To prevent multiple copies of the base class in the derived class
    d) To prevent function overriding
    Answer: c) To prevent multiple copies of the base class in the derived class


    121. What will happen if the diamond problem is not handled properly?

    a) Compilation error
    b) Ambiguous base class reference
    c) Function overloading error
    d) Infinite loop
    Answer: b) Ambiguous base class reference


    122. Which of the following is true about multiple inheritance?

    a) It always leads to the diamond problem
    b) It cannot be used in C++
    c) It allows a derived class to inherit from multiple base classes
    d) It requires all functions to be virtual
    Answer: c) It allows a derived class to inherit from multiple base classes


    123. How does C++ handle multiple inheritance internally?

    a) Using vtable and vptr
    b) Using inline expansion
    c) Using function overloading
    d) Using preprocessor macros
    Answer: a) Using vtable and vptr


    124. Can a derived class have multiple virtual base classes?

    a) Yes
    b) No
    Answer: a) Yes


    125. What is the output of the following code?

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

    a) A
    b) B
    c) Compilation error
    d) Runtime error
    Answer: c) Compilation error (Ambiguity error)


    126. How do you resolve ambiguity in multiple inheritance?

    a) Using scope resolution operator
    b) Using static functions
    c) Using friend functions
    d) Using private access specifier
    Answer: a) Using scope resolution operator


    127. What happens if a derived class does not override a virtual function from multiple base classes?

    a) The base class function with the highest priority is called
    b) The function from the first base class is called
    c) The function from the last base class is called
    d) Ambiguity error
    Answer: d) Ambiguity error


    128. Can a destructor be virtual in multiple inheritance?

    a) Yes
    b) No
    Answer: a) Yes


    129. Which of the following is a valid example of multiple inheritance?

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


    130. What is the output of the following code?

    #include <iostream>
    using namespace std; class A { public: virtual void show() { cout << "A"; } }; class B : public virtual A { public: void show() { cout << "B"; } }; class C : public virtual A { public: void show() { cout << "C"; } }; class D : public B, public C {}; int main() { D obj; obj.show(); return 0; }

    a) A
    b) B
    c) C
    d) Compilation error
    Answer: c) C (Method resolution follows the rightmost base class)


    131. What is hybrid inheritance?

    a) A mix of multiple and single inheritance
    b) Inheriting multiple base classes and a single derived class
    c) Overloading operators in multiple classes
    d) Using friend functions to access private members
    Answer: a) A mix of multiple and single inheritance


    132. In multiple inheritance, which constructor is called first?

    a) Derived class constructor
    b) First base class constructor
    c) Last base class constructor
    d) None
    Answer: b) First base class constructor

    Next Topic

    ← prevnext →




    No comments:

    Post a Comment