MCQs on Functions in C++:
1. What is the main purpose of using functions in programming?
       o A) To make the program run faster
       o B) To break down complex problems into smaller, manageable parts
       o C) To handle input and output operations
       o D) To handle errors in the program
     Answer: B) To break down complex problems into smaller, manageable parts
  2. Which of the following is the correct syntax for declaring a function in C++?
       o A) return_type function_name(parameters);
       o B) function_name return_type(parameters);
       o C) return_type function_name(parameters) { // body }
       o D) function_name(parameters) { return_type; }
     Answer: A) return_type function_name(parameters);
  3. What is the purpose of the void return type in a function?
       o A) The function does not return any value.
       o B) The function can return any type of value.
       o C) The function always returns null.
       o D) The function is required to return an integer.
     Answer: A) The function does not return any value.
  4. What is the correct syntax for calling a function named square that takes an integer
     as an argument in C++?
         o A) square(5);
         o B) square(5) = result;
         o C) square(int 5);
         o D) result = square(5);
     Answer: D) result = square(5);
  5. Which statement about function prototypes is true?
       o A) A function prototype defines the body of the function.
       o B) A function prototype informs the compiler about the function’s name, return
           type, and parameters.
       o C) A function prototype is used to specify the values returned by the function.
       o D) A function prototype is only required if the function is defined after main().
     Answer: B) A function prototype informs the compiler about the function’s name, return
     type, and parameters.
6. Which of the following functions is an example of a function that does not return
   any value?
      o A) int square(int);
      o B) double raiseToPow(double, int);
      o C) void printMessage();
      o D) float areaOfCircle(double);
   Answer: C) void printMessage();
7. What happens when a function is declared but not defined?
     o A) The program will compile and run without any issues.
     o B) The program will fail to compile.
     o C) The program will use a default implementation.
     o D) The program will print an error message.
   Answer: B) The program will fail to compile.
8. In C++, what is the correct way to handle a function that calculates the square of a
   number?
      o A) A function that takes an integer and returns an integer.
      o B) A function that returns a string representation of the square.
      o C) A function that prints the square directly without returning it.
      o D) A function that multiplies two numbers and returns the result.
   Answer: A) A function that takes an integer and returns an integer.
9. What is the value of result after executing the following code?
   cpp
   Copy code
   int square(int number) {
       return number * number;
   }
   int result = square(4);
       o   A) 4
       o   B) 16
       o   C) 12
       o   D) 8
   Answer: B) 16
10. What is the concept of "top-down design" in function development?
      o A) Breaking the program into functions that handle smaller sub-tasks.
      o B) Writing all the code for the main program first and functions later.
      o C) Handling only the main task and delegating all other tasks to external libraries.
       o   D) Writing functions only when errors occur in the program.
   Answer: A) Breaking the program into functions that handle smaller sub-tasks.
11. What is the result of calling a function by value?
      o A) The function can modify the actual variable in the calling function.
      o B) The function works with a copy of the variable, and changes do not affect the
          original.
      o C) The function cannot be called with arguments.
      o D) The function does not return any value.
   Answer: B) The function works with a copy of the variable, and changes do not affect
   the original.
12. Which function declaration is correct for a function that takes a double and an
    integer as arguments and returns a double?
        o A) double power(double, int);
        o B) int power(double, int);
        o C) void power(double, int);
        o D) double power(int, double);
   Answer: A) double power(double, int);
13. Which of the following is the correct way to declare and define a function in
    separate files?
       o A) Define the function first, then declare it in the header file.
       o B) Declare the function in the header file, and define it in a separate
           implementation file.
       o C) Declare the function and define it both in the same file.
       o D) It is not possible to declare and define functions separately in C++.
   Answer: B) Declare the function in the header file, and define it in a separate
   implementation file.
14. What would be the output of the following code?
   cpp
   Copy code
   int isEven(int     number) {
       if (number     % 2 == 0)
            return    1;
       else
            return    0;
   }
   int main() {
       int number;
       cin >> number;
          if (isEven(number))
               cout << "Even";
          else
               cout << "Odd";
          return 0;
     }
         o   A) "Odd" if the number is even
         o   B) "Even" if the number is even
         o   C) "Even" if the number is odd
         o   D) The program will give a compile-time error.
     Answer: B) "Even" if the number is even
  15. Which of the following statements is true about the return statement in a function?
        o A) A function with a void return type must always use a return statement.
        o B) A return statement terminates the execution of the function and returns
            control to the calling function.
        o C) A function with a return statement does not need to have a return type.
        o D) The return statement can only return an integer.
     Answer: B) A return statement terminates the execution of the function and returns
     control to the calling function.
  16. What will happen if a function with a return type is missing the return statement?
        o A) The program will compile but may not return a value.
        o B) The program will crash.
        o C) The program will give a compile-time error.
        o D) The program will give a runtime error.
     Answer: C) The program will give a compile-time error
More MCQs on Functions in C++:
  17. What is the purpose of the function prototype?
        o A) To provide the implementation of the function.
        o B) To inform the compiler about the function's name, return type, and parameters
            before it is used in the program.
        o C) To declare variables inside a function.
        o D) To handle the input and output for the program.
     Answer: B) To inform the compiler about the function's name, return type, and
     parameters before it is used in the program.
  18. Which of the following is a correct way to declare a function that takes two integers
      and returns their sum as an integer?
         o A) int addNumbers(int, int);
       o   B) void addNumbers(int, int);
       o   C) int addNumbers();
       o   D) void addNumbers();
   Answer: A) int addNumbers(int, int);
19. Which of the following statements about function calls is true?
      o A) Functions cannot be called from inside other functions.
      o B) The return type of a function must match the type of the calling function.
      o C) The arguments passed to a function must match the type of the function's
          parameters.
      o D) A function call does not need to match the order of parameters.
   Answer: C) The arguments passed to a function must match the type of the function's
   parameters.
20. How would you modify the function raiseToPow to handle negative exponents
    correctly?
       o A) Return the result as a positive number.
       o B) Return 1 divided by the result raised to the positive exponent.
       o C) Throw an error when the exponent is negative.
       o D) Ignore the negative exponent.
   Answer: B) Return 1 divided by the result raised to the positive exponent.
21. What is the correct syntax to define a function that takes no arguments and returns
    a double?
       o A) double function() { // body }
       o B) double function(void) { // body }
       o C) function() -> double { // body }
       o D) void function() -> double { // body }
   Answer: A) double function() { // body }
22. What is the result when you use a function call multiple times with different
    arguments?
       o A) The function is executed only once with the last argument.
       o B) The function executes multiple times with each argument, and returns the
          result of the last execution.
       o C) The function executes multiple times with each argument, and their results are
          combined.
       o D) The function will give an error when called multiple times.
   Answer: B) The function executes multiple times with each argument, and returns the
   result of the last execution.
23. Which of the following is true about a function with a void return type?
      o A) It can only perform calculations and not display any output.
      o B) It can return a value, but the value is ignored by the calling function.
      o C) It cannot take any parameters.
      o D) It performs an action but does not return any value to the calling function.
   Answer: D) It performs an action but does not return any value to the calling function.
24. Which of the following is an example of a recursive function?
      o A) A function that calls itself within its body.
      o B) A function that calculates the sum of two numbers.
      o C) A function that calculates the square of a number.
      o D) A function that prints a message.
   Answer: A) A function that calls itself within its body.
25. In a C++ function definition, which keyword is used to indicate that the function
    does not return any value?
        o A) int
        o B) double
        o C) void
        o D) return
   Answer: C) void
26. What will happen if you try to call a function without declaring its prototype when
    it is defined after the main function?
         o A) The compiler will throw an error because it does not know about the function.
         o B) The program will run normally.
         o C) The compiler will automatically generate the function's prototype.
         o D) The function will be called but will not return a value.
   Answer: A) The compiler will throw an error because it does not know about the
   function.
27. Which of the following is an example of passing a function argument by reference in
    C++?
      o A) int square(int number);
      o B) int square(int& number);
      o C) void square(int number);
      o D) void square(int* number);
   Answer: B) int square(int& number);
28. How can a function return multiple values in C++?
      o A) By using arrays or structures to return multiple values.
       o   B) By declaring the function with multiple return types.
       o   C) By returning the values as a single string.
       o   D) It is not possible for a function to return multiple values in C++.
   Answer: A) By using arrays or structures to return multiple values.
29. Which of the following is true about function overloading?
      o A) Function overloading allows functions with the same name but different return
          types.
      o B) Function overloading allows functions with the same name but different
          parameter lists.
      o C) Function overloading allows functions with different names but the same
          parameters.
      o D) Function overloading does not allow a function to return any value.
   Answer: B) Function overloading allows functions with the same name but different
   parameter lists.
30. Which of the following is correct about call by reference in functions?
      o A) The function operates on a copy of the passed argument.
      o B) The function operates on the actual argument, modifying it directly.
      o C) It is only used for functions with a void return type.
      o D) It makes a copy of the argument and then operates on it.
   Answer: B) The function operates on the actual argument, modifying it directly.
31. What will happen if you forget to include a return statement in a function that has a
    non-void return type?
       o A) The function will return 0 by default.
       o B) The program will throw a compile-time error.
       o C) The program will execute, but the return value will be undefined.
       o D) The function will print an error message.
   Answer: B) The program will throw a compile-time error.
32. Which of the following is correct for the function circleArea that calculates the
    area of a circle given the radius?
       o A) double circleArea(int radius);
       o B) double circleArea(double radius);
       o C) int circleArea(double radius);
       o D) void circleArea(double radius);
   Answer: B) double circleArea(double radius);
33. Which keyword in C++ is used to declare a function that takes parameters but does
    not return any value?
       o   A) void
       o   B) return
       o   C) function
       o   D) int
   Answer: A) void
34. What will be the output of the following code?
   cpp
   Copy code
   int cube(int number) {
       return number * number * number;
   }
   int main() {
       cout << cube(3);
       return 0;
   }
       o   A) 9
       o   B) 6
       o   C) 27
       o   D) 30
   Answer: C) 27