Commvault Written Test Qs.(C++ Programming) :: Functions - Discussion
8 / 40
#include <iostream>
using namespace std;
void fun(int a, int b, int &c,int &d)
{
c += a + b;
d += a - b;
}
int main () {
int vl = 5, v2 = 10, v3 = 2, v4 = 3;
fun(vl, v2, v3, v4);
cout << v3 << " "<< v4 ;
return 0;
}
#include <iostream>
using namespace std;
void fun(int a, int b, int &c,int &d)
{
c += a + b;
d += a - b;
}
int main () {
int vl = 5, v2 = 10, v3 = 2, v4 = 3;
fun(vl, v2, v3, v4);
cout << v3 << " "<< v4 ;
return 0;
}
A15 -5
B2 3
C17 -2
DNone of the above
Show Explanation
The question has some problems. The main function must return int instead of void. otherwise the compiler throws "‘::main’ must return ‘int’ " error. And '#include <iostream>' should be included to use IO functions.
The answer is C.
Explanation:
By associativity of operators, the expressions c += a + b; and d += a - b; will be evaluated from right to left and remember the arguments c and d pass by reference.
a = 5, b= 10, c= 2, d= 3;
So c += 15 and c becomes 17, d += -5 and d becomes -2.
Asked In ::
The question has some problems. The main function must return int instead of void. otherwise the compiler throws "‘::main’ must return ‘int’ " error. And '#include <iostream>' should be included to use IO functions.
The answer is C.
Explanation:
By associativity of operators, the expressions c += a + b; and d += a - b; will be evaluated from right to left and remember the arguments c and d pass by reference.
a = 5, b= 10, c= 2, d= 3;
So c += 15 and c becomes 17, d += -5 and d becomes -2.
Read Full Answer
Report Error
Please Login First Click Here