Get Off-Campus Placement Jobs Info !!!
Programs asked in Mettl Coding Round
Click To Practce List of Programs asked in Nagarro !!!

Commvault Written Test Qs.(C++ Programming) :: Functions - Discussion

8 / 40

What is the output of the C++ program?

#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

Answer: Option (Login/Signup)

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 :: Commvault Societe Generale

Post Your Answer Here:     

Reply    
Rate This: +0 -0
    Report


Report Error

Please Login First Click Here