Site icon T4Tutorials.com

Function arguments (pass by value/reference) — C++ MCQs

1. What will be the output of the following program?
void change(int x) { x = 10; }
int main() { int a = 5; change(a); cout << a; }

(A) 10


(B) 5


(C) 0


(D) Error



2. What is the output of this code?
void change(int &x) { x = 10; }
int main() { int a = 5; change(a); cout << a; }

(A) 10


(B) 5


(C) 0


(D) Garbage value



3. In C++, when arguments are passed by value, changes made inside the function:

(A) Affect the original variable


(B) Are reflected automatically


(C) Do not affect the original variable


(D) Cause a runtime error



4. What is the output of this code?
void swap(int a, int b) { int temp = a; a = b; b = temp; }
int main() { int x = 2, y = 3; swap(x, y); cout << x << " " << y; }

(A) 3 2


(B) 3 3


(C) 2 2


(D) 2 3



5. What will be printed?
void swap(int &a, int &b) { int temp = a; a = b; b = temp; }
int main() { int x = 2, y = 3; swap(x, y); cout << x << " " << y; }

(A) 2 3


(B) 2 2


(C) 3 3


(D) 3 2



6. When passing arguments by reference, the function receives:

(A) A copy of the value


(B) The address of the variable


(C) A constant copy


(D) A pointer to a new object



7. What is the output of this program?
void test(int a, int &b) { a = 10; b = 20; }
int main() { int x = 1, y = 2; test(x, y); cout << x << " " << y; }

(A) 1 20


(B) 10 20


(C) 10 2


(D) 1 2



8. Which of the following statements is TRUE about pass-by-value?

(A) It saves memory by avoiding copies


(B) It changes the original value


(C) It passes a copy of the variable


(D) It requires pointers



9. What is the output of this code?
void modify(int &x) { x = x * 2; }
int main() { int a = 5; modify(a); cout << a; }

(A) 5


(B) 10


(C) 25


(D) Error



10. What will happen in this program?
void func(int &a, int b) { a = a + b; }
int main() { int x = 3, y = 2; func(x, y); cout << x; }

(A) 3


(B) Error


(C) 2


(D) 5



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

(A) 10


(B) 15


(C) Error


(D) Garbage



12. What is the output of this code?
void func(int &x) { x = 15; }
int main() { int a = 10; func(a); cout << a; }

(A) 10


(B) Error


(C) 15


(D) Undefined



13. In C++, a reference variable must be:

(A) Declared using * symbol


(B) Declared using & symbol


(C) Declared using @ symbol


(D) Declared without any symbol



14. Which of the following will NOT compile?

(A) void test(int x);


(B) void test(int &x);


(C) void test(&x);


(D) void test(int* x);



15. Which of the following statements about references is TRUE?

(A) A reference can be NULL


(B) A reference must be initialized when declared


(C) A reference can refer to multiple variables


(D) References are slower than pointers



16. What will the code output?
void change(int *p) { *p = 9; }
int main() { int x = 3; change(&x); cout << x; }

(A) 3


(B) Error


(C) 0


(D) 9



17. What type of parameter passing uses the & operator in function declaration?

(A) Pass by pointer


(B) Pass by constant


(C) Pass by reference


(D) Pass by value



18. Which of the following allows modification of the actual parameter value?

(A) Pass by reference


(B) Pass by value


(C) Both A and B


(D) None of these



19. What will the output be?
void test(int *p) { *p = *p + 1; }
int main() { int x = 10; test(&x); cout << x; }

(A) 10


(B) 11


(C) 9


(D) Error



20. What will be the output of this code?
void change(int x, int &y) { x = 10; y = 20; }
int main() { int a = 1, b = 2; change(a, b); cout << a << " " << b; }

(A) 10 20


(B) 1 2


(C) 1 20


(D) 10 2



21. What is the output?
void add(int &x, int y) { x = x + y; }
int main() { int a = 5, b = 3; add(a, b); cout << a; }

(A) 8


(B) 5


(C) 3


(D) Error



22. Which statement is TRUE for pass-by-reference?

(A) Function operates on a copy of the variable


(B) Function operates on the original variable


(C) Function ignores parameters


(D) Function always returns void



23. Which of the following function calls is invalid if the function expects pass by reference?
void fun(int &x);

(A) fun(5);


(B) int a = 5; fun(a);


(C) int b = 2; fun(b);


(D) Both B and C



24. What is the output?
void test(int a, int &b) { a++; b++; }
int main() { int x = 1, y = 2; test(x, y); cout << x << " " << y; }

(A) 1 2


(B) 2 2


(C) 1 3


(D) 2 3



25. What will be the result of this code?
void func(int &x) { x = x + 5; }
int main() { int a = 10; func(a); cout << a; }

(A) 5


(B) 10


(C) 20


(D) 15



Exit mobile version