Here is a listing of tough C++ programming questions on “Argument Passing” along with answers, explanations and/or solutions:
1. How many ways of passing a parameter are there in c++?
a) 1
b) 2
c) 3
d) 4
Answer: c
Clarification: There are three ways of passing a parameter. They are pass by value,pass by reference and pass by pointer.
2. Which is used to keep the call by reference value as intact?
a) static
b) const
c) absolute
d) virtual
Answer: b
Clarification: Because const will not change the value of the variables during the execution.
3. By default how the value are passed in c++?
a) call by value
b) call by reference
c) call by pointer
d) call by object
Answer: a
Clarification: None.
4. What will be the output of the following C++ code?
-
#include
-
using namespace std;
-
void copy (int& a, int& b, int& c)
-
{
-
a *= 2;
-
b *= 2;
-
c *= 2;
-
}
-
int main ()
-
{
-
int x = 1, y = 3, z = 7;
-
copy (x, y, z);
-
cout << "x =" << x << ", y =" << y << ", z =" << z;
-
return 0;
-
}
a) 2 5 10
b) 2 4 5
c) 2 6 14
d) 2 4 9
Answer: c
Clarification: Because we multiplied the values by 2 in the copy function.
Output:
$ g++ arg6.cpp $ a.out x = 2,y = 6,z = 14
5. What will be the new value of x in the following C++ code?
-
#include
-
using namespace std;
-
void fun(int &x)
-
{
-
x = 20;
-
}
-
int main()
-
{
-
int x = 10;
-
fun(x);
-
cout << "New value of x is " << x;
-
return 0;
-
}
a) 10
b) 20
c) 15
d) 36
Answer: b
Clarification: 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