Site icon T4Tutorials.com

Function overloading — C++ MCQs

1. What is function overloading in C++?

(A) Defining multiple functions with the same name but different parameters


(B) Using one function inside another


(C) Using multiple return types for one function


(D) Redefining a function in derived class



2. Which of the following is an example of function overloading?

(A) void add(int a, int b); void add(double a, double b);


(B) void add(int a, int b); int add(int a, int b);


(C) void add(); int add(int); int add(float, float);


(D) Both A and C



3. What will this code output?
void test(int a) { cout << "int"; }
void test(double a) { cout << "double"; }
int main() { test(3.5); }

(A) int


(B) double


(C) Error


(D) None



4. Which rule must be followed for function overloading?

(A) Functions must differ in number or type of parameters


(B) Functions must differ only by return type


(C) Functions must have different names


(D) Functions must be in separate classes



5. What is the output of this code?
void show() { cout << "No arg"; }
void show(int a) { cout << "Int arg"; }
int main() { show(); }

(A) Int arg


(B) No arg


(C) Error


(D) Both printed



6. What happens if two functions differ only in return type?

(A) It causes ambiguity error


(B) It is allowed


(C) It becomes overloaded


(D) It is treated as inline



7. Which of the following is a valid overloaded function set?

(A) int sum(int a, int b); float sum(float a, float b);


(B) int sum(int a); int sum(int b);


(C) int sum(); void sum();


(D) None



8. What is the output of this code?
void area(int r) { cout << "Circle"; }
void area(int l, int b) { cout << "Rectangle"; }
int main() { area(4,5); }

(A) Circle


(B) Rectangle


(C) Error


(D) Both



9. What will be printed?
void print(int a) { cout << "Int"; }
void print(char c) { cout << "Char"; }
int main() { print('A'); }

(A) Int


(B) Error


(C) Char


(D) None



10. Function overloading is resolved at:

(A) Execution time


(B) Run time


(C) Linking time


(D) Compile time



11. What will this code output?
void calc(int a, int b) { cout << a + b; }
void calc(double a, double b) { cout << a * b; }
int main() { calc(2, 3); }

(A) 5


(B) 6


(C) Error


(D) 2 3



12. What will this program print?
void print(string s) { cout << "String"; }
void print(char c) { cout << "Char"; }
int main() { print("A"); }

(A) Error


(B) Char


(C) String


(D) None



13. Can constructors be overloaded in C++?

(A) Yes


(B) No


(C) Only with default parameters


(D) Only in derived classes



14. What happens when overloaded functions are ambiguous?

(A) The first function is chosen


(B) Compiler gives an error


(C) Program runs normally


(D) The last function is chosen



15. Which of the following causes a compilation error?

(A) void show(int); void show(double);


(B) int show(int); void show(int);


(C) void show(int, double); void show(double, int);


(D) void show(); void show(int);



16. What will be printed?
void fun(int a, double b) { cout << "int double"; }
void fun(double a, int b) { cout << "double int"; }
int main() { fun(2, 3.5); }

(A) int double


(B) double int


(C) Error


(D) None



17. What is the output of this program?
void test(int a) { cout << "Int"; }
void test(float a) { cout << "Float"; }
int main() { test(5.0); }

(A) Int


(B) Float


(C) Error


(D) Ambiguous



18. Can default arguments and overloading be used together?

(A) Only inside classes


(B) No


(C) Only if return types differ


(D) Yes



19. What will the output be?
void sum(int a, int b = 2) { cout << a + b; }
void sum(int a) { cout << a; }
int main() { sum(5); }

(A) 7


(B) 5


(C) Error


(D) 2



20. Which one is NOT allowed in function overloading?

(A) Same number but different types of parameters


(B) Different number of parameters


(C) Same parameters but different return types


(D) Same function name



21. What will be printed?
void display(int a) { cout << "Integer"; }
void display(float a) { cout << "Float"; }
int main() { display(2.0f); }

(A) Integer


(B) Float


(C) Error


(D) None



22. What happens if a call matches two overloaded functions equally?

(A) The compiler gives an ambiguity error


(B) The first declared function executes


(C) The last declared function executes


(D) Random one is chosen



23. Which of the following is TRUE about overloaded functions?

(A) They must return the same type


(B) They must differ by parameter list


(C) They must be defined in separate files


(D) They must have different names



24. What is the output of this code?
void show(int a, int b) { cout << "Two ints"; }
void show(int a) { cout << "One int"; }
int main() { show(10, 20); }

(A) One int


(B) Two ints


(C) Error


(D) None



25. Function overloading helps to:

(A) Use same function name for different purposes


(B) Increase code repetition


(C) Reduce performance


(D) Restrict polymorphism



26. What is the output?
void calc(int a) { cout << "Int"; }
void calc(float a) { cout << "Float"; }
void calc(double a) { cout << "Double"; }
int main() { calc(5); }

(A) Int


(B) Float


(C) Double


(D) Error



27. Can operator overloading and function overloading coexist in C++?

(A) Only in templates


(B) No


(C) Only inside classes


(D) Yes



28. Which of the following is false about function overloading?

(A) It is an example of compile-time polymorphism


(B) It can be done using different parameter types


(C) It can be done by changing return types


(D) It increases program readability



29. What will this code print?
void calc(int a, double b) { cout << "A"; }
void calc(double a, int b) { cout << "B"; }
int main() { calc(2.0, 3); }

(A) A


(B) B


(C) Error


(D) Ambiguous



30. Function overloading is an example of:

(A) Compile-time polymorphism


(B) Runtime polymorphism


(C) Data abstraction


(D) Encapsulation



Exit mobile version