Default arguments — C++ MCQs

25
Score: 0
Attempted: 0/25
1. What is a default argument in C++?



2. What will be the output of this code?
int add(int a, int b = 5) { return a + b; }
int main() { cout << add(3); }




3. Which of the following function declarations is correct for using a default argument?



4. What will the following code display?
void print(int a = 1, int b = 2, int c = 3) { cout << a << b << c; }
int main() { print(5, 6); }




5. Default arguments must be defined:



6. What will the code print?
void test(int a = 2, int b = 3) { cout << a * b; }
int main() { test(); }




7. In C++, default arguments are assigned from:



8. What will be the output?
void display(int x = 10, int y = 20) { cout << x << ” ” << y; }
int main() { display(5); }




9. Which of the following is invalid in default argument usage?



10. What will this code output?
void func(int a, int b = 4, int c = 6) { cout << a + b + c; }
int main() { func(2, 3); }




11. Which one is a valid function declaration?



12. What happens when this code executes?
void print(int x = 1, int y = 2) { cout << x + y; }
int main() { print(3, 4); }




13. Default arguments in C++ are evaluated:



14. What will the code print?
void sum(int a = 1, int b = 2, int c = 3) { cout << a + b + c; }
int main() { sum(5); }




15. What is the output?
void show(int a = 5) { cout << a; }
int main() { show(10); }




16. Which of the following is TRUE about default arguments?



17. What will this code output?
void calc(int x = 3, int y = 4, int z = 5) { cout << x * y * z; }
int main() { calc(2, 3); }




18. What happens if a function is called with all arguments provided?



19. What is the output of this code?
void print(char c = ‘A’, int n = 2) { for(int i = 0; i < n; i++) cout << c; }
int main() { print(‘B’); }




20. Which of the following uses multiple default parameters correctly?



21. What will this code display?
void test(int a = 2, int b = 3) { cout << a + b; }
int main() { test(4, 5); }




22. Which of the following statements is FALSE about default arguments?



23. What will the program print?
void fun(int a, int b = 4, int c = 6) { cout << a + b + c; }
int main() { fun(1); }




24. Can a default argument be a constant expression or variable?



25. What is the output of the program?
void area(int l = 2, int b = 3) { cout << l * b; }
int main() { area(); }




Contents Copyrights Reserved By T4Tutorials