Functions and OOP Features - user-defined functions, parameter passing, virtual functions, constructors, destructors, overloading, templates - Question Bank

1. What is the primary goal of encapsulation in OOP?
A) To allow objects to change their form.
B) To bundle data (attributes) and methods (functions) that operate on the data into a single unit and restrict direct access to some components.
C) To enable runtime polymorphism.
D) To create generic code that works with multiple data types.
2. A template function is defined using which keyword prefix?
A) class
B) struct
C) template
D) virtual
3. Which of the following is a key benefit of using virtual functions?
A) Enforcing compile-time type safety.
B) Allowing dynamic dispatch of function calls based on the actual type of the object at runtime.
C) Reducing the memory footprint of objects.
D) Simplifying operator syntax.
4. If a base class has a virtual destructor, what is the implication for derived class objects?
A) The derived class destructor is never called.
B) The derived class destructor is called, followed by the base class destructor, ensuring proper cleanup.
C) The base class destructor is called instead of the derived class destructor.
D) It prevents the derived class from having its own destructor.
5. What does a constructor achieve that a regular function cannot regarding object initialization?
A) It can be called multiple times after object creation.
B) It is automatically invoked when an object is created, ensuring proper initial state.
C) It can be used to destroy an object.
D) It can be declared as virtual.
6. Consider a function `void display(int a, int b)`. If you create another function `void display(double x, double y)`, this is an example of:
A) Function overriding
B) Function overloading
C) Operator overloading
D) Template specialization
7. Which OOP feature allows an object to take on many forms?
A) Encapsulation
B) Inheritance
C) Polymorphism
D) Abstraction
8. A function template can be specialized for a specific data type. This means:
A) The compiler generates a unique version of the template function for that specific type.
B) The template definition is ignored for that specific type.
C) The template can only be used with that specific type.
D) The template becomes a regular function.
9. What is the scope resolution operator (`::`) used for?
A) To define a virtual function.
B) To access members of a class or namespace from outside the class/namespace.
C) To overload an operator.
D) To declare a template.
10. When is it generally recommended to use pass by reference rather than pass by pointer?
A) When you need to guarantee that the parameter is not null.
B) When you want to modify the original variable.
C) When the object being passed is large.
D) All of the above
11. What is the purpose of the `this` pointer in C++?
A) To point to the next object in memory.
B) To point to the current object within a member function.
C) To point to a static member of the class.
D) To point to the base class constructor.
12. In the context of virtual functions, what is an abstract class?
A) A class with no members.
B) A class that cannot be instantiated and typically contains one or more pure virtual functions.
C) A class that can only be inherited by one other class.
D) A class that has all its members declared as private.
13. If a function is declared as `void func(int *ptr)`, the parameter `ptr` is passed by:
A) Value
B) Reference
C) Pointer
D) Constant reference
14. What is the primary benefit of using templates?
A) To reduce the number of lines of code by writing generic code.
B) To improve runtime performance by optimizing function calls.
C) To enforce strict type checking at compile time.
D) To allow dynamic memory allocation.
15. Which concept allows a single operator symbol (like `+` or `-`) to perform different operations based on the operands it is applied to?
A) Function overloading
B) Operator overloading
C) Virtual functions
D) Templates
16. A destructor can be overloaded.
A) True, like constructors
B) False, a class can have only one destructor
C) True, but only with different return types
D) True, but only with different parameter lists
17. What happens if a class has multiple constructors?
A) Only the default constructor can be used.
B) The compiler selects the appropriate constructor based on the arguments provided during object creation.
C) All constructors are called automatically for every object.
D) It leads to a compile-time error.
18. A virtual function in a base class allows derived classes to provide their own specific implementation, which is known as:
A) Encapsulation
B) Abstraction
C) Polymorphism
D) Overloading
19. Which of the following is NOT a common reason to use user-defined functions?
A) Code reusability
B) Modularity and organization
C) Reducing program complexity
D) Automatic memory management
20. In C++, `std::vector<int>` is an example of using:
A) A function template
B) A class template
C) A virtual function
D) An overloaded operator
21. What is the purpose of the `typename` keyword when declaring a template parameter for a dependent type?
A) To indicate that the parameter is a pointer.
B) To specify that the parameter is a reference.
C) To inform the compiler that the identifier is a type name, not a variable or function name.
D) To define a constant value.
22. What is the main difference between function overloading and function overriding?
A) Overloading occurs in the same class, while overriding occurs in derived classes.
B) Overloading requires virtual functions, while overriding does not.
C) Overloading involves different function signatures, while overriding involves the same signature in a derived class.
D) Overloading is compile-time polymorphism, while overriding is runtime polymorphism.
23. If a class has a destructor, when is it guaranteed to be called?
A) Only when `delete` is explicitly called on an object.
B) When the object goes out of scope or is explicitly deleted.
C) When the program terminates.
D) When a copy constructor is called.
24. What is the term for a constructor that can be called with a single argument?
A) Default constructor
B) Copy constructor
C) Parameterized constructor
D) Implicitly converting constructor
25. A derived class can access public and protected members of its base class. What is the purpose of `protected` access specifier?
A) To allow access only within the class itself.
B) To allow access within the class and by friend functions/classes.
C) To allow access within the class and by derived classes.
D) To allow access from anywhere.
26. Pass by pointer is similar to pass by reference in that:
A) Both can modify the original variable.
B) Both avoid copying the object.
C) Both use dereferencing syntax.
D) Both are automatically managed by the compiler.
27. When using templates, the compiler generates actual code for each specific data type used. This process is known as:
A) Inheritance
B) Virtualization
C) Instantiation
D) Encapsulation
28. Which of the following is an example of compile-time polymorphism?
A) Virtual functions
B) Function overloading
C) Abstract classes
D) Inheritance
29. What does `~` signify when used before a class name in C++?
A) It indicates a static member.
B) It indicates a virtual function.
C) It indicates a destructor.
D) It indicates a friend function.
30. A copy constructor is called when:
A) An object is destroyed.
B) An object is initialized from another object of the same type.
C) A function returns an object by value.
D) Both B and C
31. A pure virtual function is a virtual function that:
A) Must be implemented by all derived classes.
B) Has a default implementation in the base class.
C) Has no implementation in the base class and is assigned 0.
D) Can only be called from the base class.
32. What is the primary advantage of pass by reference over pass by value for large objects?
A) It is always faster.
B) It avoids the overhead of copying the entire object.
C) It allows modification of the original object.
D) It makes the code more readable.
33. A class template allows you to define:
A) A single class for a specific data type.
B) A blueprint for classes that can operate on different data types.
C) A class with only private members.
D) A class that can be inherited by any other class.
34. Which of the following is NOT a valid way to achieve function overloading?
A) Changing the number of parameters.
B) Changing the types of parameters.
C) Changing the return type of the function.
D) Changing the order of parameters.
35. When does a constructor typically get called?
A) When an object is destroyed.
B) When an object is declared or created.
C) When a function is called.
D) When a program finishes execution.
36. What is the keyword used in C++ to declare a virtual function?
A) static
B) friend
C) virtual
D) abstract
37. If a function parameter is declared as `int& var`, it means the parameter is passed by:
A) Value
B) Reference
C) Pointer
D) Constant value
38. A function template in C++ is used to create:
A) A single function that works only for one specific data type.
B) A blueprint for functions that can operate on different data types.
C) A function that can be called from any other program.
D) A function that is automatically called at program startup.
39. Operator overloading is a form of:
A) Runtime polymorphism
B) Compile-time polymorphism
C) Inheritance
D) Encapsulation
40. What is the syntax for a destructor in C++?
A) ~ClassName()
B) ClassName()
C) delete ClassName()
D) static ~ClassName()
41. A constructor that takes no arguments or has default values for all its arguments is known as a:
A) Parameterized constructor
B) Default constructor
C) Copy constructor
D) Friend constructor
42. Consider a base class `Animal` and a derived class `Dog`. If `speak()` is a virtual function in `Animal` and is overridden in `Dog`, calling `speak()` on a `Dog` object through a pointer to `Animal` will invoke:
A) The `speak()` function of the `Animal` class.
B) The `speak()` function of the `Dog` class.
C) A compile-time error.
D) A default system function.
43. When a variable is passed by value to a function, any changes made to the parameter inside the function:
A) Are reflected in the original variable.
B) Are local to the function and do not affect the original variable.
C) Cause a compile-time error.
D) Are stored in a temporary global variable.
44. What is the purpose of a template in C++?
A) To define abstract classes.
B) To enable generic programming, allowing code to work with different data types without rewriting.
C) To manage exception handling.
D) To create multithreaded applications.
45. Function overloading allows multiple functions with the same name to exist if they differ in:
A) Return type only
B) Number and/or types of parameters
C) Access specifier (public, private, protected)
D) Scope (local, global)
46. Which member function is automatically called when an object of a class is destroyed?
A) Constructor
B) Destructor
C) Copy constructor
D) Assignment operator
47. What is the main role of a constructor in object-oriented programming?
A) To destroy an object when it goes out of scope.
B) To initialize the data members of an object when it is created.
C) To provide access to private members of a class.
D) To perform memory deallocation for an object.
48. In C++, what is a virtual function primarily used for?
A) To define static members of a class.
B) To enable runtime polymorphism through inheritance.
C) To overload operators for a class.
D) To create template functions.
49. Which parameter passing mechanism allows a function to modify the original variable passed to it?
A) Pass by value
B) Pass by reference
C) Pass by pointer
D) Pass by constant reference
50. What is the primary purpose of a user-defined function in programming?
A) To handle all input/output operations automatically.
B) To encapsulate a specific task or set of operations into a reusable block of code.
C) To manage memory allocation and deallocation for the entire program.
D) To define the graphical user interface of an application.