C++ Questions and Answers Part-27

1. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void square (int *x)
{
*x = (*x + 1) * (*x);
}
int main ( )
{
int num = 10;
square(&num);
cout << num;
return 0;
}
a) 100
b) compile time error
c) 144
d) 110

Answer: d
Explanation: We have increased the x value in operand as x+1, so it will return as 110.
Output:
110

2. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int add(int a, int b);
int main()
{
int i = 5, j = 6;
cout << add(i, j) << endl;
return 0;
}
int add(int a, int b )
{
int sum = a + b;
a = 7;
return a + b;
}
a) 11
b) 12
c) 13
d) compile time error

Answer: c
Explanation: The value of a has been changed to 7, So it returns as 13.
Output:
13

3. What will happen when we use void in argument passing?
a) It will not return value to its caller
b) It will 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

Answer: a
Explanation: As void is not having any return value, it will not return the value to the caller.

4. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void Sum(int a, int b, int & c)
{
a = b + c;
b = a + c;
c = a + b;
}
int main()
{
int x = 2, y =3;
Sum(x, y, y);
cout << x << " " << y;
return 0;
}
a) 2 3
b) 6 9
c) 2 15
d) compile time error

Answer: c
Explanation: We have passed three values and it will manipulate according to the given condition and yield the result as 2 15
Output:
2 15

5. Which of the following permits function overloading on c++?
a) type
b) number of arguments
c) type & number of arguments
d) number of objects

Answer: c
Explanation: Both type and number of arguments permits function overloading in C++, like
int func(int);
float func(float, float)
Here both type and number of arguments are different.

6. In which of the following we cannot overload the function?
a) return function
b) caller
c) called function
d) main function

Answer: a
Explanation: While overloading the return function, it will rise a error, So we can’t overload the return function.

7. Function overloading is also similar to which of the following?
a) operator overloading
b) constructor overloading
c) destructor overloading
d) function overloading

Answer: b
Explanation: In constructor overloading, we will be using the same options availed in function overloading.

8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void print(int i)
{
cout << i;
}
void print(double f)
{
cout << f;
}
int main(void)
{
print(5);
print(500.263);
return 0;
}
a) 5500.263
b) 500.2635
c) 500.263
d) 500.266

Answer: a
Explanation: In this program, we are printing the values and the values will be print(5) will be printed first because of the order of the execution.
Output:
5500.263

9. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int Add(int X, int Y, int Z)
{
return X + Y;
}
double Add(double X, double Y, double Z)
{
return X + Y;
}
int main()
{
cout << Add(5, 6);
cout << Add(5.5, 6.6);
return 0;
}
a) 11 12.1
b) 12.1 11
c) 11 12
d) compile time error

Answer: d
Explanation: As one can observe that no function has declaration similar to that of called Add(int, int) and Add(double, double) functions. Therefore, error occurs.

10. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int operate (int a, int b)
{
return (a * b);
}
float operate (float a, float b)
{
return (a / b);
}
int main()
{
int x = 5, y = 2;
float n = 5.0, m = 2.0;
cout << operate(x, y) <<"\t";
cout << operate (n, m);
return 0;
}
a) 10.0 5.0
b) 5.0 2.5
c) 10.0 5
d) 10 2.5

Answer: d
Explanation: In this program, we are divide and multiply the values.
Output:
10 2.5