1. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int fun(int=0, int = 0);
int main()
{
cout << fun(5);
return 0;
}
int fun(int x, int y) { return (x+y); }
a) -5
b) 0
c) 10
d) 5
Explanation: C++ allows to define such prototype of the function in which you are not required to give variable names only the default values. While in function definition you can provide the variable names corresponding to each parameter.
2. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void square (int *x, int *y)
{
*x = (*x) * --(*y);
}
int main ( )
{
int number = 30;
square(&number, &number);
cout << number;
return 0;
}
a) 870
b) 30
c) error
d) Segmentation fault
Explanation: As we are passing value by reference therefore the change in the value is reflected back to the passed variable number hence value of number is changed to 870.
3. From which function the execution of a C++ program starts?
a) start() function
b) main() function
c) new() function
d) end() function
Explanation: The execution of a C++ program starts from the main() function.
4. Which of the following is important in a function?
a) Return type
b) Function name
c) Both return type and function name
d) The return type, function name and parameter list
Explanation: The important things required in a function is its return type and its name other than that parameter list are optional which a function may or may not have.
5. How many ways of passing a parameter are there in c++?
a) 1
b) 2
c) 3
d) 4
Explanation: There are three ways of passing a parameter. They are pass by value,pass by reference and pass by pointer.
6. Which is used to keep the call by reference value as intact?
a) static
b) const
c) absolute
d) virtual
Explanation: Because const will not change the value of the variables during the execution.
7. 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
Explanation: call by value
8. What will be the output of the following C++ code?
#include <iostream>
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
Explanation: Because we multiplied the values by 2 in the copy function.
Output:
x = 2,y = 6,z = 14
9. What will be the new value of x in the following C++ code?
#include <iostream>
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
Explanation: 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:
20
10. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
long factorial (long a)
{
if (a > 1)
return (a * factorial (a + 1));
else
return (1);
}
int main ()
{
long num = 3;
cout << num << "! = " << factorial ( num );
return 0;
}
a) 6
b) 24
c) segmentation fault
d) compile time error
Explanation: As we have given in the function as a+1, it will exceed the size and so it arises the segmentation fault.
Output:
segmentation fault