Latest C++ Functions MCQs

1 min read

1. What is a function in C++?

a) A block of code that performs a specific task
b) A keyword to define variables
c) A memory allocation tool
d) A data type
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?

a) yield
b) return
c) give
d) send
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?

a) It defaults to void
b) It defaults to int
c) It causes a compilation error
d) It returns a null pointer
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?

a) The actual body of the function
b) A declaration specifying its name, return type, and parameters
c) A built-in system function
d) A template parameter
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?

a) start()
b) main()
c) init()
d) execute()
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?

a) The function returns an integer 0
b) The function does not return any value
c) The function accepts no parameters
d) The function is a template
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?

a) Variables declared inside the function's parameter list
b) Values passed during the function call
c) Variables declared globally
d) Variables returned by the 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?

a) Variables declared in the function definition
b) The real values passed to a function when it is called
c) Global variables
d) Return values
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?

a) Modifying the parameter affects the original argument
b) A copy of the actual argument is passed to the function
c) It passes the memory address
d) It is faster for large objects
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++?

a) Using the * operator
b) Using the & operator
c) Using the @ operator
d) Using the ref keyword
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?

a) The entire array is copied by value
b) A pointer to its first element is passed
c) The size is automatically known
d) It causes an error
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?

a) A value assigned by the compiler automatically
b) A value assigned to a parameter to be used if no argument is provided
c) An argument that cannot be changed
d) An argument passed by reference
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?

a) They must be assigned from right to left
b) They must be assigned from left to right
c) They must be assigned randomly
d) Only the first argument can be default
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?

a) The parameter is passed by reference
b) The parameter value cannot be modified inside the function
c) The function returns a constant
d) The function is thread-safe
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?

a) Defining multiple functions with the same name but different parameters
b) Calling a function multiple times
c) Using a function in a loop
d) Returning multiple values
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?

a) Yes
b) No
c) Only if one returns void
d) Only for class members
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++?

a) Encrypting code
b) Modifying function names internally to include parameter types for overloading
c) Changing variable names dynamically
d) A debugging technique
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?

a) To include C source code
b) To prevent name mangling for C compatibility
c) To convert C++ code to C
d) To define external variables
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?

a) A function that executes automatically
b) A request to the compiler to insert the function body at the place of its call
c) A function with no parameters
d) A function that returns void
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?

a) The function contains just one statement
b) The function has recursive calls
c) The function returns void
d) The function takes no arguments
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?

a) Yes
b) No
c) Only with void return types
d) Only in namespaces
Correct Answer: b) No
Explanation:
Standard C++ rules explicitly forbid overloading the main() function.

22. What are the common parameters of the main() function?

a) void argc, void argv
b) int argc, char argv
c) char argc, int argv
d) string argc[]
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()?

a) The array of arguments
b) The name of the program
c) The number of command-line arguments passed
d) The exit code
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?

a) A loop that never terminates
b) A function calling itself directly or indirectly
c) A function returning multiple values
d) A function calling main()
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?

a) A base condition
b) A global variable
c) A switch statement
d) A void return type
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?

a) It runs exactly once
b) It causes a stack overflow at runtime
c) It causes a compilation error
d) It defaults to zero
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?

a) When the recursive call is the last operation in the function
b) When recursion is done at the beginning
c) When a function calls two different functions
d) When recursion only happens in if-statements
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?

a) A function calling itself twice
b) Two or more functions calling each other cyclically
c) A function returning a reference to itself
d) A recursive function with multiple base cases
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?

a) Yes
b) No
c) Only if it is an integer array
d) Only if it returns void
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?

a) It creates a dangling reference because the variable is destroyed on exit
b) It causes a syntax error
c) It makes the variable global
d) There is no danger
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?

a) private
b) static
c) extern
d) internal
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?

a) Left to right
b) Right to left
c) Middle first
d) Simultaneously
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?

a) A pointer to the return value
b) A variable that stores the memory address of a function
c) A function returning a pointer
d) A smart 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?

a) void *ptr();
b) void (*ptr)();
c) void ptr*();
d) void (&ptr)();
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?

a) A function that calls main()
b) A function passed as an argument to another function to be executed later
c) A recursive function
d) An exception handler
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?

a) std::invoke
b) std::callable
c) std::function
d) std::ptr
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++?

a) A function taking no arguments
b) An object of a class that overloads the function call operator ()
c) A function pointer alias
d) A built-in mathematical function
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++?

a) A macro definition
b) An anonymous, inline function object
c) A generic template
d) A global variable
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?

a) Parameter list
b) Return type
c) Capture clause
d) Execution block
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?

a) Capture all variables by value
b) Capture all variables by reference
c) Capture no variables
d) Capture only pointers
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?

a) Capture all variables by value
b) Capture all variables by reference
c) Assign new values
d) Compare variables
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:

a) Mutable
b) Volatile
c) Const
d) Static
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?

a) dynamic
b) mutable
c) changeable
d) volatile
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?

a) Inside the capture clause
b) Using a trailing return type with ->
c) Before the parameter list
d) Inside the parameter list
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)?

a) A lambda using auto in its parameter list
b) A lambda without a capture clause
c) A lambda called from C
d) A lambda that captures globals
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?

a) nothrow
b) noexcept
c) safe
d) throw()
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?

a) It can be evaluated at compile time given constant expressions
b) It never takes parameters
c) It runs in O(1) time
d) It cannot have a return value
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)?

a) They are the same
b) consteval forces strict compile-time evaluation, constexpr allows runtime too
c) constexpr is strictly for compile time
d) consteval is for variables only
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?

a) Deletes the function from memory
b) Explicitly forbids its use, causing a compile error if called
c) Marks it deprecated
d) Frees its variables
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++?

a) A function that has a variable memory footprint
b) A function that can accept a variable number of arguments
c) A function that changes its return type dynamically
d) A function without parameters
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?

a) <variadic.h>
b) <stdarg.h> or
c)
d)
Correct Answer: b) <stdarg.h> or
Explanation:
The header defines macros needed to handle C-style variadic arguments.

52. What is a function template?

a) A blueprint for creating functions that can operate on different data types
b) A function that only accepts string parameters
c) A design pattern
d) A function returning a void pointer
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?

a) generic
b) template
c) auto
d) typename
Correct Answer: b) template
Explanation:
The template keyword is required before the template parameter list.

54. What is template argument deduction?

a) When the compiler automatically determines the data type of the template parameter
b) When the compiler removes unused templates
c) When the programmer explicitly forces a type
d) When the program casts a variable
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?

a) Making a template run faster
b) Providing a specific implementation of a template for a particular data type
c) Restricting templates to classes
d) Defining multiple parameters
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++?

a) A function imported from another project
b) A non-member function with access to private and protected members of a class
c) A function with no return type
d) A static function
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?

a) Yes, always
b) No, because it is not a member of the class
c) Only if passed explicitly
d) Only when called statically
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?

a) Only in the private section
b) Only in the public section
c) Anywhere; access specifiers do not affect friend declarations
d) Only in the protected section
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?

a) A function defined inside a namespace
b) A function declared within a class definition
c) A function returning an object
d) A function accepting class objects
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?

a) Using the dot (.) operator
b) Using the scope resolution operator (::)
c) Using the arrow (->) operator
d) Using the colon (:) operator
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++?

a) A pointer to the next instruction
b) An implicit pointer that holds the address of the current object
c) A pointer to the global scope
d) A pointer to the base class
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?

a) A function that can only be called once
b) A member function that belongs to the class itself, not to any specific object
c) A function that cannot change local variables
d) An inline 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?

a) They can access non-static class members directly
b) They have a 'this' pointer
c) They can only access static data members and other static member functions
d) They must be virtual
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?

a) The function returns a constant value
b) The function cannot modify any non-static data members of the class
c) The function takes constant parameters
d) The function is static
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?

a) A function with no body
b) A member function expected to be redefined in derived classes for dynamic polymorphism
c) A function executed in a virtual machine
d) A friend 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?

a) A virtual function without a return type
b) A virtual function assigned the value of 0, forcing derived classes to implement it
c) A function returning 0
d) A function marked pure
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?

a) It becomes a sealed class
b) It becomes an abstract class and objects of it cannot be created
c) It causes a compilation error
d) It becomes a struct
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?

a) Yes
b) No
c) Only in abstract classes
d) Only default constructors
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?

a) Yes, and it is highly recommended when using inheritance
b) No, it causes undefined behavior
c) Only if the constructor is virtual
d) Yes, but they must be pure virtual
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?

a) Allows overriding private functions
b) Explicitly states that a function is intended to override a virtual function in a base class
c) Overloads a function dynamically
d) Stops a function from being overridden
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?

a) Makes the function return a constant value
b) Prevents any further overriding of this virtual function in derived classes
c) Terminates the program after execution
d) Optimizes the function for speed
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?

a) The compiler guesses the function name
b) The compiler automatically figures out the return type based on the return statement
c) The function returns an auto pointer
d) The function doesn't need to return anything
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?

a) Only the first one matters
b) They must all deduce to the exact same type
c) It automatically returns a variant
d) It causes a runtime error
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?

a) Prevents returning memory addresses
b) Generates a compiler warning if the return value of the function is ignored by the caller
c) Prevents discarding the function from optimization
d) Removes the function from the global namespace
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?

a) It deletes the function from the binary
b) It warns developers that the function is outdated and its use is discouraged
c) It makes the function run slower
d) It changes the access specifier to private
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?

a) auto func() -> int;
b) int -> func();
c) func() : int;
d) func auto -> int;
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?

a) The exception is caught automatically
b) The program calls std::terminate and crashes immediately
c) The exception is ignored
d) It causes a memory leak
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?

a) Yes
b) No, it causes an ambiguity error when calling the function
c) Only if they are template functions
d) Yes, but only in classes
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?

a) They overload each other successfully
b) They are treated as exactly the same signature by the compiler at the declaration level
c) One is faster than the other
d) The first one is illegal
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?

a) int func();
b) int& func();
c) int&& func();
d) int* func();
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?

a) void func(int arr[][]);
b) void func(int arr[][10]);
c) void func(int** arr);
d) Multidimensional arrays cannot be passed
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?

a) It causes a compiler error
b) It is valid, but the caller must remember to delete it to avoid memory leaks
c) The memory is automatically freed
d) It results in a dangling pointer
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?

a) std::vector
b) std::function
c) std::tuple
d) std::any
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++?

a) Yes
b) No, C++ does not support standard nested functions
c) Only if it is void
d) Only inside main
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++?

a) A macro
b) A lambda expression that is defined and immediately executed in the same statement
c) A recursive function
d) A separate thread
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?

a) It executes twice
b) It can suspend its execution and be resumed later
c) It bypasses the CPU
d) It only accepts string arguments
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?

a) Macros are evaluated at runtime
b) Inline functions perform type checking; macros perform simple text substitution
c) Macros can only define variables
d) There is no difference
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?

a) typedef void(*FuncPtr)();
b) using FuncPtr = void(*)();
c) alias FuncPtr = void();
d) Both a and b are valid
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?

a) void func(int arr[])
b) void func(const int arr[])
c) void func(int* const arr)
d) void func(const int& arr)
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?

a) Yes, it unrolls infinitely
b) It is syntactically allowed, but the compiler generally ignores the inline request
c) No, syntax error
d) Only if depth is 1
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 ?

a) To attach a variable to a port
b) To generate a new callable by partially applying arguments to an existing function
c) To combine two classes
d) To force a constant
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?

a) Only in the function definition
b) Only in the function prototype (declaration)
c) In both places simultaneously
d) In the main function
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?

a) Overload Resolution
b) Type Erasure
c) Name Mangling
d) Stack Unwinding
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?

a) A log of all function calls on disk
b) The data structure on the call stack containing parameters, locals, and return address
c) A database record
d) A C++20 module feature
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?

a) Yes, but it is frowned upon as it hides memory management
b) No, it will not compile
c) Yes, and memory frees automatically
d) No, new only returns ints
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?

a) static void func();
b) void const func();
c) void func() const;
d) const void func();
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?

a) Pass by value
b) Pass by reference
c) Static variables
d) Const parameters
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?

a) Value is printed
b) Compiler ignores the value
c) A compile-time error occurs
d) Program crashes
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?

a) No
b) Yes, commonly used to implement state machines or dispatch tables
c) Only if they take no arguments
d) Only if they return void
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?

a) Using = delete on a template specialization
b) Using #pragma disable
c) Using a void return type
d) It cannot be prevented
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?

a) std::vector
b) std::tuple
c) Multiple return keywords
d) Global references
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?

a) Binding network sockets
b) Easily unpacking multiple returned values into local variables
c) Linking C code
d) Managing thread locks
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++?

a) An error message
b) A helper function generated by the compiler to manage virtual function calls or multiple inheritance
c) A thread variable
d) A C++ keyword
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?

a) Function name and return type only
b) Function name, parameter types, and constness, EXCLUDING return type
c) Return type, body, and variables
d) Only the parameter list
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?

a) 40 bytes
b) The size of a pointer
c) 10
d) 0
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?

a) void pointers
b) Macros
c) Variadic templates
d) Global arrays
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?

a) Using a for loop
b) Using a fold expression (C++17) or recursive unpacking
c) Using the expand keyword
d) Using a switch statement
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?

a) To provide a calculation result to the user
b) To signify the execution status (success or failure) to the operating system
c) To assign a memory block size
d) Because void main is required
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++?

a) Yes
b) No, standard C++ requires main to return int
c) Yes, on Windows
d) Yes, in embedded systems
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?

a) Code fails to compile
b) Program crashes
c) The compiler implicitly inserts return 0;
d) It returns garbage
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?

a) The Heap
b) The Call Stack
c) The Data Segment
d) The Text Segment
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?

a) Yes, this is how inline functions and standard definitions work
b) No, they must be separated into .h and .cpp files
c) Only for void functions
d) Only for class members
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.
← Previous: Latest C++ Basics MCQs
NewC++ Pointers and References - MCQs

C++ Pointers and References - MCQs

In C++, pointers and references are powerful features that provide direct access to memory and allow for highly efficient data…

By MCQs Generator
New
No Image

C++ Loops (For, While)

1. What is a function in C++? a) A block of code that performs a specific task b) A keyword…

By MCQs Generator
NewC++ Variables & Data Types MCQs

C++ Variables & Data Types MCQs

Variables and data types form the core building blocks of any C++ application, defining how data is stored, represented, and…

By MCQs Generator