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

25
Score: 0
Attempted: 0/25
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; }




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




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



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




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




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



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




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



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




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




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




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




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



14. Which of the following will NOT compile?



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



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




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



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



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




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




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




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



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




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




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




Contents Copyrights Reserved By T4Tutorials