C++ Programming :: Functions

Home > C++ Programming > Functions > General Questions

1 / 13

 What is the output of the C++ program?

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

Answer: Option C

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.

Workspace


Tags:  Commvault

2 / 13

 What is the output of the C++ program?

#include
using namespace std;

#define SQ(a) a*a
inline int square(int a)
{
return a*a;
}

int main () {
int i, j, k, l;
i = SQ(2 + 3);
j = square(2 + 3);
k = SQ(6 - 1);
l = square(6 - 1);

cout << i << " "<< j << " "<< k << " " << l;
return 0;
}

Answer: Option A

Explanation:

We defined a macro SQ as SQ(a) a*a. So whenever we use SQ() in our code compiler replaces that with macro definition which is a*a.

So,

SQ(2 + 3) will be replaced with 2 + 3 * 2 + 3 and '*' had more priority in operator precedence. i becomes 2 + 6 + 3 = 11.

square(2 + 3) is a regular function call and j becomes 25.

SQ(6 - 1) will be replaced with 6 - 1 * 6 - 1 and '*' had more priority in operator precedence. k becomes 6 - 6 - 1 = -1.

square(6 - 1) is a regular function call and l becomes 25.

Workspace


Tags:  Commvault

3 / 13

 What is the output of the C++ program?

#include
using namespace std;
class base {
int b;
public:
base (int x) {
b = x;
cout << "Base";
};
};
class derived : public base
{
double d;
public:
derived(double x, int y);
d = x;
cout << " Derived";
};

int main () {
derived d(23.5, 10);
}

Answer: Option D

Explanation:

Correct program:

#include <iostream>

using namespace std;


class base {

    int b;

public:

    base (int x) {

        b = x;

        cout << "Base";

    };

};

class derived : public base

{

    double d;

public:

    derived(double x, int y);

    d = x;

    cout << " Derived";

};


int main () {

    derived d(23.5, 10);

}

Answer: Compiler throws "error: ‘d’ does not name a type" and "error: ‘cout’ does not name a type" errors.

This is because statements in c++ should be inside a function but  d = x; and cout << " Derived"; are defined directly inside a class.

Workspace


Tags:  Commvault

Answer: Option C

Explanation:

Here is no explanation for this answer

Workspace


Tags:  No Tags on this question yet!

Answer: Option D

Explanation:

A function can throw as many exceptions as needed of different types, but can only handle some of those exceptions. 

Exceptions which are caught but not handled by the function will be handed over to the caller and it is up to the caller function now to handle these exceptions in its own way.

Workspace


Tags:  No Tags on this question yet!

6 / 13

 When an object-oriented program detects an error within a function, the function

Answer: Option A

Explanation:

Basic exception handling mechanism in OOP language is like below.

try: is a block of statements that may throw an exception.

catch: is a block of statements that gets called when a exception is thrown.

throw: can be used to throw an exception.

So when an object-oriented program detects an error within a function, the function throws an exception and catch block if implemented catches the exception, if not the exception will be propagated to the caller function.

Workspace


Tags:  No Tags on this question yet!

Answer: Option D

Explanation:

A friend function is a regular function with its definition placed outside of any class. But a friend function when defined will have access to all private and public members of the class.

To define a friend function, we will declare the function name preceded with keyword inside the class and function definition will be placed outside of the class.

A friend function can be invoked directly by using the function name.

Workspace


Tags:  No Tags on this question yet!

Answer: Option A

Explanation:

A function will become const when we use 'const' keyword in function declaration. when we declare a function with const keyword, compiler does not allow that function to modify the state of the object on which the function being called.

Workspace


Tags:  No Tags on this question yet!

9 / 13

 Friend function should be declared in the _________part of the class.

Answer: Option C

Explanation:

A friend function is a regular function with its definition placed outside of any class. But a friend function when defined will have access to all private and public members of the class.

To define a friend function, we will declare the function name preceded with keyword inside the class and function definition will be placed outside of the class.

A friend function can be invoked directly by using the function name.

It can be declared in Private and Public part of the class.

Workspace


Tags:  No Tags on this question yet!

Answer: Option A

Explanation:

Static Class members will have only one copy of its will be used by all members of the class no matter how many objects are created.

However static member functions are independent of the objects of class. we can access a static function directly using the scope resolution operator '::'.

A static member function will only have access to static members of the class.

Workspace


Tags:  No Tags on this question yet!