C++ Programming Questions and Answers – Argument Passing

C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ programming questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

C++ programming questions on “Argument Passing” along with answers, explanations and/or solutions:

Q 1. How many ways of passing a parameter are there in c++?
A. 1
B. 2
C. 3
D. 4

Show Answer Answer:-C. 3
Explanation There are three ways of passing a parameter. They are pass by value,pass by reference and pass by pointer.

Q 2. Which is used to keep the call by reference value as intact?
A. static
B. absolute
C. const
D. virtual

Show Answer Answer:-C. const
Explanation Because const will not change the value of the variables during the execution.

Q 3. By default how the value are passed in c++?
A. call by pointer
B. call by reference
C. call by value
D. call by object

Show Answer Answer:-C. call by value
Explanation None.

Q 4. What will be the output of the following C++ code?

  1. #include <iostream>
  2. using namespace std;
  3. void copy (int& a, int& b, int& c)
  4. {
  5. a *= 2;
  6. b *= 2;
  7. c *= 2;
  8. }
  9. int main ()
  10. {
  11. int x = 1, y = 3, z = 7;
  12. copy (x, y, z);
  13. cout << “x =” << x << “, y =” << y << “, z =” << z;
  14. return 0;
  15. }

A. 2 5 10
B. 2 4 5
C. 2 4 9
D. 2 6 14

Show Answer Answer:-D. 2 6 14
Explanation Because we multiplied the values by 2 in the copy function. Output: $ g++ arg6.cpp $ a.out x = 2,y = 6,z = 14

Q 5. What will be the new value of x in the following C++ code?

  1. #include <iostream>
  2. using namespace std;
  3. void fun(int &x)
  4. {
  5. x = 20;
  6. }
  7. int main()
  8. {
  9. int x = 10;
  10. fun(x);
  11. cout << “New value of x is ” << x;
  12. return 0;
  13. }

A. 10
B. 15
C. 20
D. 36

Show Answer Answer:-C. 20
Explanation As the parameter is passed by reference, the value in the original memory of x is changed hence the output is printed as 20. Output: $ g++ arg5.cpp $ a.out 20

Q 6. What will be the output of the following C++ code?

  1. #include <iostream>
  2. using namespace std;
  3. long factorial (long a)
  4. {
  5. if (a > 1)
  6. return (a * factorial (a + 1));
  7. else
  8. return (1);
  9. }
  10. int main ()
  11. {
  12. long num = 3;
  13. cout << num << “! = ” << factorial ( num );
  14. return 0;
  15. }

A. 6
B. 24
C. compile time error
D. segmentation fault

Show Answer Answer:-D. segmentation fault
Explanation As we have given in the function as a+1, it will exceed the size and so it arises the segmentation fault. Output: $ g++ arg3.cpp $ a.out segmentation fault

Q 7. What will be the output of the following C++ code?

  1. #include <iostream>
  2. using namespace std;
  3. void square (int *x)
  4. {
  5. *x = (*x + 1) * (*x);
  6. }
  7. int main ( )
  8. {
  9. int num = 10;
  10. square(&num);
  11. cout << num;
  12. return 0;
  13. }

A. 100
B. compile time error
C. 144
D. 110

Show Answer Answer:-D. 110
Explanation We have increased the x value in operand as x+1, so it will return as 110. Output: $ g++ arg2.cpp $ a.out 110

Q 8. What will be the output of the following C++ code?

  1. #include <iostream>
  2. using namespace std;
  3. int add(int a, int b);
  4. int main()
  5. {
  6. int i = 5, j = 6;
  7. cout << add(i, j) << endl;
  8. return 0;
  9. }
  10. int add(int a, int b )
  11. {
  12. int sum = a + b;
  13. a = 7;
  14. return a + b;
  15. }

A. 11
B. 12
C. 13
D. compile time error

Show Answer Answer:-C. 13
Explanation The value of a has been changed to 7, So it returns as 13. Output: $ g++ arg1.cpp $ a.out 13

Q 9. What will happen when we use void in argument passing?
A. It will return value to its caller
B. It will not return value to its caller
C. Maybe or may not be return any value to its caller
D. It will return value with help of object

Show Answer Answer:-B. It will not return value to its caller
Explanation As void is not having any return value, it will not return the value to the caller.

Q 10. What will be the output of the following C++ code?

  1. #include <iostream>
  2. using namespace std;
  3. void Sum(int a, int b, int & c)
  4. {
  5. a = b + c;
  6. b = a + c;
  7. c = a + b;
  8. }
  9. int main()
  10. {
  11. int x = 2, y =3;
  12. Sum(x, y, y);
  13. cout << x << ” ” << y;
  14. return 0;
  15. }

A. 2 3
B. 6 9
C. 2 15
D. compile time error

Show Answer Answer:-C. 2 15
Explanation We have passed three values and it will manipulate according to the given condition and yield the result as 2 15 Output: $ g++ arg.cpp $ a.out 2 15

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page