Site icon T4Tutorials.com

Default arguments — C++ MCQs

1. What is a default argument in C++?

(A) An argument that must always be provided


(B) A value automatically assigned if no argument is passed


(C) A type of constructor


(D) A return value of a function



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

(A) 3


(B) 5


(C) 8


(D) Error



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

(A) int sum(int a, int b = 10);


(B) int sum(int a = 10, int b);


(C) int sum(a = 10, int b);


(D) int sum(a, b = 10);



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); }

(A) 566


(B) 123


(C) 561


(D) 563



5. Default arguments must be defined:

(A) Only in function definition


(B) Only in function declaration


(C) Either in declaration or definition, but not both


(D) In both declaration and definition



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

(A) 5


(B) 6


(C) 23


(D) Error



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

(A) Right to left


(B) Left to right


(C) Random order


(D) Center outward



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

(A) 5 20


(B) 10 20


(C) 5 10


(D) 5 5



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

(A) void show(int a = 1, int b = 2);


(B) void show(int a, int b = 2);


(C) void show(int a = 1, int b);


(D) void show(int a = 1);



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); }

(A) 9


(B) 12


(C) 13


(D) 11



11. Which one is a valid function declaration?

(A) void fun(int a = 10, int b);


(B) void fun(int a, int b = 10);


(C) void fun(int a = 10, b = 20);


(D) void fun(a = 10, b = 20);



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

(A) 3


(B) 4


(C) 7


(D) 6



13. Default arguments in C++ are evaluated:

(A) When declared globally


(B) At runtime


(C) After the function executes


(D) At compile time



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); }

(A) 5


(B) 8


(C) 6


(D) 10



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

(A) 10


(B) 5


(C) Error


(D) 15



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

(A) They must appear at the start of the parameter list


(B) They must appear at the end of the parameter list


(C) They can appear anywhere in the list


(D) They are not allowed in C++



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); }

(A) 24


(B) 30


(C) 60


(D) 15



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

(A) Default values are ignored


(B) Default values are still added


(C) Program crashes


(D) Compiler gives an error



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’); }

(A) B


(B) BB


(C) A


(D) AA



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

(A) void func(int a = 1, int b = 2, int c = 3);


(B) void func(int a, int b, int c = 3, int d = 4);


(C) void func(int a = 1, int b, int c);


(D) Both A and B



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

(A) 5


(B) 7


(C) 9


(D) 6



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

(A) They help reduce function overloading


(B) They must be provided from rightmost side


(C) They can appear anywhere in the argument list


(D) They can be used with constructors



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

(A) 11


(B) 10


(C) 12


(D) 9



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

(A) Only a constant expression


(B) Only a variable


(C) Either a constant or variable


(D) Only a literal value



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

(A) 5


(B) 6


(C) 2


(D) Error



Exit mobile version