Function definition and declaration — C++ MCQs

30
Score: 0
Attempted: 0/30
1. Which of the following correctly declares a function in C++?



2. What is the correct way to define a function named display that takes no arguments and returns nothing?



3. What will the following program print?
void show() { cout << "Hello"; }
int main() { show(); return 0; }




4. Identify the function declaration among the following:



5. Which of the following defines a function named square that returns an integer?



6. What is the return type of the following function?
float average(int a, int b) { return (a + b) / 2; }




7. What is the error in this code?
int add(int a, int b) { return a + b }




8. What will the following code display?
int add(int a, int b) { return a + b; }
int main() { cout << add(2, 3); return 0; }




9. Which of the following is a valid function declaration?



10. What is the correct function definition for int max(int x, int y)?
12. What will be printed?
void greet() { cout << "Hi"; }
int main() { greet(); greet(); }




13. What is wrong with this function definition?
void display; { cout << "Hello"; }




14. Which of the following functions returns nothing?



15. How many values can a function return in C++?



16. What is the output?
int getValue() { return 10; }
int main() { cout << getValue(); return 0; }




17. Which of the following is incorrect?



18. What will this code output?
int sum(int a, int b = 5) { return a + b; }
int main() { cout << sum(10); }




19. What happens if a function is declared but not defined?



20. What is true about the main() function in C++?



21. Identify the correct syntax to call a function named display:



22. What will this code print?
int add(int a, int b) { return a + b; }
int main() { cout << add(2, add(1, 2)); }




23. Which statement about function overloading is true?



24. What is the output of this program?
void show(int a) { cout << "Int"; }
void show(double a) { cout << "Double"; }
int main() { show(5.5); }




25. What will this function return?
int test() { int x = 5; return x + 2; }




26. Which of the following is NOT allowed in C++ functions?



27. What happens if a function does not have a return statement but its return type is int?



28. What will be the output?
int test() { return 3; cout << "Hello"; }
int main() { cout << test(); }




29. Which of the following is true about function parameters?



30. What is the correct order?



Contents Copyrights Reserved By T4Tutorials