Functions (User-defined & Built-in) Interview Questions

    Question 1C++ Function Declaration

    Which of the following is the correct way to declare a function in C++ that returns an integer and takes no arguments?

    Question 2C++ Function Definition

    What is the correct way to define a function named add that takes two integers and returns their sum?

    Question 3C++ Guess the Output – Function Return

    What will be the output of the following C++ code?

    #include <iostream> using namespace std; int square(int x) { return x * x; } int main() { cout << square(4); return 0; }

    Question 4C++ Function Overloading

    Which of the following demonstrates valid function overloading in C++?

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

    Question 5C++ Guess the Output – Pass by Value

    What will be the output of the following code?

    #include <iostream> using namespace std; void change(int x) { x = 20; } int main() { int a = 10; change(a); cout << a; return 0; }

    Question 6C++ Pass by Reference

    Which of the following correctly demonstrates pass by reference in C++?

    Question 7C++ Default Arguments

    Which function declaration is valid with default arguments?

    Question 8C++ Inline Functions

    What is the purpose of an inline function in C++?

    Question 9C++ Guess the Output – Recursion

    What will be the output of the following program?

    #include <iostream> using namespace std; int factorial(int n) { if (n == 0) return 1; return n * factorial(n - 1); } int main() { cout << factorial(4); return 0; }

    Question 10C++ Function Prototypes

    Why are function prototypes used in C++?