1. What is a function in C++?
Correct Answer: a) A block of code that performs a specific task
Explanation:
A function is a reusable block of code designed to perform a specific task.
2. Which keyword is used to return a value from a function?
Correct Answer: b) return
Explanation:
The return keyword is used to exit a function and send a value back to the caller.
3. What happens if a C++ function does not specify a return type?
Correct Answer: c) It causes a compilation error
Explanation:
Standard C++ requires every function to have an explicitly declared return type.
4. What is a function prototype?
Correct Answer: b) A declaration specifying its name, return type, and parameters
Explanation:
A function prototype tells the compiler about the function's signature before its actual definition.
5. From which function does the execution of a C++ program begin?
Correct Answer: b) main()
Explanation:
Every standard C++ program must have a main() function, which serves as the entry point.
6. What does a void return type signify?
Correct Answer: b) The function does not return any value
Explanation:
The void keyword indicates that the function performs an action but yields no output value.
7. What are formal parameters in a function?
Correct Answer: a) Variables declared inside the function's parameter list
Explanation:
Formal parameters act as local placeholders for the values passed to the function when called.
8. What are actual parameters?
Correct Answer: b) The real values passed to a function when it is called
Explanation:
Actual parameters are the real values or variables supplied during a function call.
9. Which of the following is true about pass-by-value?
Correct Answer: b) A copy of the actual argument is passed to the function
Explanation:
In pass-by-value, the function works on a separate copy, so original variables are unaffected.
10. How do you pass a variable by reference in C++?
Correct Answer: b) Using the & operator
Explanation:
The ampersand symbol in the parameter list designates it as a reference, allowing direct modification.
11. What happens when an array is passed to a function?
Correct Answer: b) A pointer to its first element is passed
Explanation:
Arrays decay into pointers when passed to functions, transferring the address of the first element.
12. What is a default argument?
Correct Answer: b) A value assigned to a parameter to be used if no argument is provided
Explanation:
Default arguments allow a function to be called with fewer parameters than declared.
13. What is the rule for assigning default arguments?
Correct Answer: a) They must be assigned from right to left
Explanation:
Once a parameter has a default value, all subsequent parameters to its right must also have default values.
14. What does a const parameter indicate?
Correct Answer: b) The parameter value cannot be modified inside the function
Explanation:
Using const ensures that the function treats the parameter as read-only.
15. What is function overloading?
Correct Answer: a) Defining multiple functions with the same name but different parameters
Explanation:
Function overloading allows functions sharing a name to coexist, provided their parameter lists differ.
16. Can two functions be overloaded solely by changing their return types?
Correct Answer: b) No
Explanation:
The C++ compiler determines the function to call based on arguments. It cannot differentiate based solely on return type.
17. What is name mangling in C++?
Correct Answer: b) Modifying function names internally to include parameter types for overloading
Explanation:
To support overloading, the compiler alters internal function names by appending parameter type information.
18. What is the purpose of the extern C block?
Correct Answer: b) To prevent name mangling for C compatibility
Explanation:
extern C instructs the compiler not to mangle names, allowing proper linking with C libraries.
19. What is an inline function?
Correct Answer: b) A request to the compiler to insert the function body at the place of its call
Explanation:
Inline functions suggest embedding code directly to avoid function call overhead.
20. Which scenario makes the compiler most likely to ignore an inline request?
Correct Answer: b) The function has recursive calls
Explanation:
Complexities like recursion or large loops typically cause the compiler to treat inline requests as normal functions.
21. Can the main() function in standard C++ be overloaded?
Correct Answer: b) No
Explanation:
Standard C++ rules explicitly forbid overloading the main() function.
22. What are the common parameters of the main() function?
Correct Answer: b) int argc, char argv
Explanation:
The standard parameters for main are int argc (argument count) and char argv (argument vector).
23. What does argc represent in main()?
Correct Answer: c) The number of command-line arguments passed
Explanation:
argc stands for argument count, indicating how many strings are in the argv array.
24. What is recursion?
Correct Answer: b) A function calling itself directly or indirectly
Explanation:
Recursion occurs when a function involves a call to itself to solve smaller instances of a problem.
25. What is essential for a recursive function to prevent an infinite loop?
Correct Answer: a) A base condition
Explanation:
A base case or terminating condition stops recursive calls, preventing stack overflow.
26. What happens if a recursive function does not have a base case?
Correct Answer: b) It causes a stack overflow at runtime
Explanation:
Without a base case, recursion continues infinitely until the program consumes all available stack memory.
27. What is tail recursion?
Correct Answer: a) When the recursive call is the last operation in the function
Explanation:
Tail recursion occurs when the recursive call is the absolute final action, allowing memory optimization.
28. Which of the following describes mutual recursion?
Correct Answer: b) Two or more functions calling each other cyclically
Explanation:
Mutual recursion happens when function A calls function B, and function B calls function A.
29. Is it legal for a standard C++ function to return a C-style array directly?
Correct Answer: b) No
Explanation:
Raw arrays cannot be returned directly. You must return a pointer, struct, or container like std::vector.
30. What is the danger of returning a reference to a local variable?
Correct Answer: a) It creates a dangling reference because the variable is destroyed on exit
Explanation:
Local variables are destroyed when scope ends. Returning a reference points to invalid memory.
31. Which keyword makes a free-standing function visible only within its translation unit?
Correct Answer: b) static
Explanation:
Applying static to a free function gives it internal linkage, making it invisible outside its source file.
32. How are function arguments pushed onto the call stack typically in cdecl?
Correct Answer: b) Right to left
Explanation:
While parameter evaluation order is unspecified in C++, traditional calling conventions like cdecl push right to left.
33. What is a function pointer?
Correct Answer: b) A variable that stores the memory address of a function
Explanation:
A function pointer holds the memory address of executable code, allowing dynamic calls.
34. What is the correct syntax for a pointer to a function returning void and taking no arguments?
Correct Answer: b) void (*ptr)();
Explanation:
The parentheses around *ptr indicate it is a pointer to a function, not a function returning a pointer.
35. What is a callback function?
Correct Answer: b) A function passed as an argument to another function to be executed later
Explanation:
A callback is executable code passed as an argument to be invoked at a specific time.
36. In C++11, what represents generic callable objects including function pointers and lambdas?
Correct Answer: c) std::function
Explanation:
std::function is a polymorphic wrapper that can store, copy, and invoke any callable target.
37. What is a functor in C++?
Correct Answer: b) An object of a class that overloads the function call operator ()
Explanation:
A functor acts like a function because its class defines operator(), allowing internal state maintenance.
38. What is a lambda expression in C++?
Correct Answer: b) An anonymous, inline function object
Explanation:
Lambdas are convenient inline anonymous functions that can capture variables from their scope.
39. What do the square brackets [] represent in a lambda expression?
Correct Answer: c) Capture clause
Explanation:
The capture clause [] defines which variables from the surrounding scope are accessible inside the lambda.
40. What does a capture clause of [&] signify in a lambda?
Correct Answer: b) Capture all variables by reference
Explanation:
[&] captures all local variables by reference, allowing the lambda to modify them.
41. What does a capture clause of [=] signify in a lambda?
Correct Answer: a) Capture all variables by value
Explanation:
[=] captures all local variables used within the lambda by making a read-only copy.
42. By default, variables captured by value in a lambda are:
Correct Answer: c) Const
Explanation:
Variables captured by value are implicitly read-only unless the mutable keyword is specified.
43. Which keyword makes variables captured by value modifiable inside a lambda?
Correct Answer: b) mutable
Explanation:
The mutable keyword removes the implicit const qualification from variables captured by value.
44. How do you specify the return type of a lambda explicitly?
Correct Answer: b) Using a trailing return type with ->
Explanation:
Lambdas use trailing return types, e.g., [](https://www.google.com/search?q=) -> int { return 5; }.
45. What is a generic lambda (introduced in C++14)?
Correct Answer: a) A lambda using auto in its parameter list
Explanation:
C++14 allows using auto for lambda parameters, making the lambda a template function object.
46. Which keyword marks a function to explicitly not throw any exceptions?
Correct Answer: b) noexcept
Explanation:
The noexcept specifier indicates that the function is guaranteed not to throw exceptions.
47. What does a constexpr function guarantee?
Correct Answer: a) It can be evaluated at compile time given constant expressions
Explanation:
constexpr indicates the return value can be computed at compile-time if inputs are compile-time constants.
48. What is the difference between constexpr and consteval (C++20)?
Correct Answer: b) consteval forces strict compile-time evaluation, constexpr allows runtime too
Explanation:
A consteval function MUST produce a compile-time constant, whereas constexpr can run at runtime if needed.
49. What does the = delete syntax do to a function?
Correct Answer: b) Explicitly forbids its use, causing a compile error if called
Explanation:
Defining a function as = delete intentionally prevents its usage to stop unwanted conversions or copies.
50. What is a variadic function in C++?
Correct Answer: b) A function that can accept a variable number of arguments
Explanation:
Variadic functions can accept an indefinite number of arguments.
51. Which header is required for using old C-style variadic functions like va_list?
Correct Answer: b) <stdarg.h> or
Explanation:
The header defines macros needed to handle C-style variadic arguments.
52. What is a function template?
Correct Answer: a) A blueprint for creating functions that can operate on different data types
Explanation:
Templates allow creating generic functions that work with any data type, avoiding code duplication.
53. What keyword is used to declare a function template?
Correct Answer: b) template
Explanation:
The template keyword is required before the template parameter list.
54. What is template argument deduction?
Correct Answer: a) When the compiler automatically determines the data type of the template parameter
Explanation:
C++ compilers can often deduce the type parameter from the arguments provided during the call.
55. What is template specialization?
Correct Answer: b) Providing a specific implementation of a template for a particular data type
Explanation:
Specialization allows overriding the generic template code with a specific implementation for a certain type.
56. What is a friend function in C++?
Correct Answer: b) A non-member function with access to private and protected members of a class
Explanation:
Declaring a function as a friend gives it full access to the class's private data.
57. Does a friend function have a 'this' pointer?
Correct Answer: b) No, because it is not a member of the class
Explanation:
Since friend functions are not class members, they lack an implicit 'this' pointer.
58. Where can a friend function be declared inside a class?
Correct Answer: c) Anywhere; access specifiers do not affect friend declarations
Explanation:
The location of a friend declaration does not change its meaning or access level.
59. What is a member function?
Correct Answer: b) A function declared within a class definition
Explanation:
Member functions are associated with a class and operate on the objects of that class.
60. How is a class member function defined outside the class?
Correct Answer: b) Using the scope resolution operator (::)
Explanation:
The :: operator is used to define a function outside the class, specifying which class it belongs to.
61. What is the 'this' pointer in C++?
Correct Answer: b) An implicit pointer that holds the address of the current object
Explanation:
Inside non-static member functions, 'this' is a hidden pointer to the object calling the function.
62. What is a static member function?
Correct Answer: b) A member function that belongs to the class itself, not to any specific object
Explanation:
Static member functions are class-level functions and can be called without instantiating an object.
63. Which of the following is true about static member functions?
Correct Answer: c) They can only access static data members and other static member functions
Explanation:
Because they lack a 'this' pointer, static functions can only interact with static class data.
64. What does a const keyword at the end of a member function declaration mean?
Correct Answer: b) The function cannot modify any non-static data members of the class
Explanation:
A const member function promises not to alter the state of the object it is called on.
65. What is a virtual function?
Correct Answer: b) A member function expected to be redefined in derived classes for dynamic polymorphism
Explanation:
Virtual functions allow C++ to support runtime polymorphism through base class pointers.
66. What is a pure virtual function?
Correct Answer: b) A virtual function assigned the value of 0, forcing derived classes to implement it
Explanation:
Declaring a function as = 0 makes it pure virtual, turning its class into an abstract base class.
67. What happens if a class has at least one pure virtual function?
Correct Answer: b) It becomes an abstract class and objects of it cannot be created
Explanation:
Abstract classes serve as interfaces. Because the function is undefined, instantiation is prohibited.
68. Can constructors be virtual functions?
Correct Answer: b) No
Explanation:
Constructors cannot be virtual because the virtual table mechanism relies on the object being constructed first.
69. Can destructors be virtual functions?
Correct Answer: a) Yes, and it is highly recommended when using inheritance
Explanation:
Virtual destructors ensure that when a derived object is deleted via a base pointer, the derived destructor is invoked.
70. What does the override keyword do in C++11?
Correct Answer: b) Explicitly states that a function is intended to override a virtual function in a base class
Explanation:
The override specifier helps prevent bugs by making the compiler check if a corresponding virtual function exists.
71. What does the final keyword do when applied to a member function?
Correct Answer: b) Prevents any further overriding of this virtual function in derived classes
Explanation:
Marking a virtual function as final restricts derived classes from providing their own implementation.
72. What is the C++14 auto return type deduction?
Correct Answer: b) The compiler automatically figures out the return type based on the return statement
Explanation:
Using auto as a return type lets the compiler deduce the type from the expression in the return statement.
73. If a function with an auto return type has multiple return statements, what must be true?
Correct Answer: b) They must all deduce to the exact same type
Explanation:
For return type deduction to work, all return paths within the function must evaluate to the same data type.
74. What does the [[nodiscard]] attribute do?
Correct Answer: b) Generates a compiler warning if the return value of the function is ignored by the caller
Explanation:
[[nodiscard]] is used to enforce that the caller handles the return value, like checking an error code.
75. What is the use of the [[deprecated]] attribute on a function?
Correct Answer: b) It warns developers that the function is outdated and its use is discouraged
Explanation:
The compiler will issue a warning if a function marked as [[deprecated]] is called.
76. What is a trailing return type syntax?
Correct Answer: a) auto func() -> int;
Explanation:
Trailing return types place the return type after the parameter list following an arrow.
77. In the context of exceptions, what happens if a function marked noexcept throws an exception?
Correct Answer: b) The program calls std::terminate and crashes immediately
Explanation:
If an exception escapes a function marked noexcept, the C++ runtime forcefully terminates the application.
78. Can a function be overloaded based strictly on whether a parameter is passed by value or passed by reference?
Correct Answer: b) No, it causes an ambiguity error when calling the function
Explanation:
The compiler cannot determine whether to copy the variable or pass its reference, leading to ambiguity.
79. What is the difference between void func(const int x) and void func(int x) in declarations?
Correct Answer: b) They are treated as exactly the same signature by the compiler at the declaration level
Explanation:
Top-level const qualifiers on pass-by-value parameters are ignored in function signatures.
80. What does a function returning an rvalue reference look like?
Correct Answer: c) int&& func();
Explanation:
The double ampersand (&&) denotes an rvalue reference, often used in move semantics.
81. How do you pass a multidimensional array to a function?
Correct Answer: b) void func(int arr[][10]);
Explanation:
When passing multidimensional C-style arrays, all dimensions except the first must be explicitly specified.
82. What is the consequence of returning a pointer to a dynamically allocated variable (using new) from a function?
Correct Answer: b) It is valid, but the caller must remember to delete it to avoid memory leaks
Explanation:
Dynamically allocated memory survives function exit, shifting the responsibility of deallocation to the caller.
83. Which standard library template safely passes functions and lambdas as arguments?
Correct Answer: b) std::function
Explanation:
std::function is a robust, type-safe wrapper for storing and passing any callable object.
84. Can you declare a standard C-style nested function inside another function in standard C++?
Correct Answer: b) No, C++ does not support standard nested functions
Explanation:
Standard C++ does not allow traditional nested functions, though lambda expressions can be defined locally.
85. What is an Immediately Invoked Function Expression (IIFE) in C++?
Correct Answer: b) A lambda expression that is defined and immediately executed in the same statement
Explanation:
By appending () to the end of a lambda definition, it executes instantly.
86. How is a C++ coroutine (C++20) different from a regular function?
Correct Answer: b) It can suspend its execution and be resumed later
Explanation:
Coroutines introduce keywords like co_await to pause and resume execution state.
87. What distinguishes a macro from an inline function?
Correct Answer: b) Inline functions perform type checking; macros perform simple text substitution
Explanation:
Inline functions are type-safe and integrated into the C++ compiler, whereas macros are preprocessor text replacements.
88. Which of the following creates an alias for a function pointer in C++11?
Correct Answer: d) Both a and b are valid
Explanation:
Both typedef and the modern using keyword can define an alias for function pointers.
89. If you want a function parameter to act as an input-only array that the function cannot alter, what is the best signature?
Correct Answer: b) void func(const int arr[])
Explanation:
Declaring the array parameter as const prevents the function from modifying its contents.
90. Can an inline function be recursive?
Correct Answer: b) It is syntactically allowed, but the compiler generally ignores the inline request
Explanation:
Compilers cannot inline infinite recursion and will compile it as a standard function.
91. What is the purpose of std::bind in ?
Correct Answer: b) To generate a new callable by partially applying arguments to an existing function
Explanation:
std::bind creates a new function object binding specific values to parameters of the original function.
92. When defining default arguments in a function, where should they be specified?
Correct Answer: b) Only in the function prototype (declaration)
Explanation:
Default arguments must be specified in the declaration visible to the caller.
93. Which principle relates to finding the correct overloaded function to execute?
Correct Answer: a) Overload Resolution
Explanation:
Overload resolution is the process where the compiler decides which overloaded function to invoke based on arguments.
94. What is an activation record?
Correct Answer: b) The data structure on the call stack containing parameters, locals, and return address
Explanation:
An activation record (stack frame) is pushed onto the stack every time a function is called.
95. Can standard C++ functions return a reference to a dynamically allocated object?
Correct Answer: a) Yes, but it is frowned upon as it hides memory management
Explanation:
It compiles, but makes it unintuitive for the caller to delete the underlying pointer, risking leaks.
96. How do you specify that a class method does not modify the object?
Correct Answer: c) void func() const;
Explanation:
Placing const after the parameter list signifies that the 'this' pointer is a pointer to const.
97. Which of the following allows a function to modify an outside variable without using pointers?
Correct Answer: b) Pass by reference
Explanation:
Passing by reference (using &) gives the function direct access to the original variable.
98. What happens if a function tries to return a value but its return type is void?
Correct Answer: c) A compile-time error occurs
Explanation:
Returning a specific value inside a void function violates its signature, causing a compilation error.
99. Can you have an array of function pointers?
Correct Answer: b) Yes, commonly used to implement state machines or dispatch tables
Explanation:
Arrays of function pointers map indices to behaviors, replacing complex switch statements.
100. What is the standard convention for preventing a function template from being instantiated with a specific type?
Correct Answer: a) Using = delete on a template specialization
Explanation:
Providing a specialization and marking it = delete explicitly forbids that type from being used.
101. Which modern C++ feature allows returning multiple values elegantly without defining a struct?
Correct Answer: b) std::tuple
Explanation:
std::tuple allows packing multiple disparate data types into a single returnable object.
102. What is structured binding (C++17) used for in the context of functions?
Correct Answer: b) Easily unpacking multiple returned values into local variables
Explanation:
Structured binding syntax easily unpacks pairs, tuples, or structs returned by a function.
103. What is a thunk in C++?
Correct Answer: b) A helper function generated by the compiler to manage virtual function calls or multiple inheritance
Explanation:
Thunks are injected by the compiler to adjust the 'this' pointer during complex method dispatch.
104. What does a function signature consist of?
Correct Answer: b) Function name, parameter types, and constness, EXCLUDING return type
Explanation:
The signature identifies the function for overloading. It excludes the return type.
105. If you pass an array int arr[10] to a function, what is sizeof(arr) inside the function?
Correct Answer: b) The size of a pointer
Explanation:
Because the array decays to a pointer, sizeof returns the pointer's size, not the array's.
106. What does standard C++ recommend instead of C-style variadic functions (...) for type safety?
Correct Answer: c) Variadic templates
Explanation:
Variadic templates offer a type-safe way to handle an arbitrary number of arguments.
107. How are template parameters expanded in a variadic template function?
Correct Answer: b) Using a fold expression (C++17) or recursive unpacking
Explanation:
Variadic parameter packs are typically unpacked via recursion or fold expressions.
108. What is the purpose of the main function returning an integer?
Correct Answer: b) To signify the execution status (success or failure) to the operating system
Explanation:
Returning 0 indicates successful execution; non-zero values represent error codes.
109. Is void main() standard C++?
Correct Answer: b) No, standard C++ requires main to return int
Explanation:
The C++ standard strictly mandates that the main function must have a return type of int.
110. If you do not explicitly write return 0; at the end of the main function, what happens?
Correct Answer: c) The compiler implicitly inserts return 0;
Explanation:
If main() reaches the end without a return statement, it is treated as if return 0; was executed.
111. Which component manages the execution and memory allocation for local function variables?
Correct Answer: b) The Call Stack
Explanation:
The call stack automatically handles local variable memory and control flow for function calls.
112. Can a function be declared and defined in the same statement?
Correct Answer: a) Yes, this is how inline functions and standard definitions work
Explanation:
A function can be fully defined (body included) at its point of declaration.









