C++ Questions and Answers Part-28

1. Overloaded functions are ________________
a) Very long functions that can hardly run
b) One function containing another one or more functions inside it
c) Two or more functions with the same name but different number of parameters or type
d) Very long functions

Answer: c
Explanation: This is the definition of function overloading i.e. function having same name but different number of parameters and types.

2. What will happen while using pass by reference?
a) The values of those variables are passed to the function so that it can manipulate them
b) The location of variable in memory is passed to the function so that it can use the same memory area for its processing
c) The function declaration should contain ampersand (& in its type declaration)
d) The function declaration should contain $

Answer: b
Explanation: In pass by reference, we can use the function to access the variable and it can modify it. Therefore we are using pass by reference.

3. What should be passed in parameters when function does not require any parameters?
a) void
b) blank space
c) both void & blank space
d) tab space

Answer: b
Explanation: When we does not want to pass any argument to a function then we leave the parameters blank i.e. func() – function without any parameter.

4. What are the advantages of passing arguments by reference?
a) Changes to parameter values within the function also affect the original arguments
b) There is need to copy parameter values (i.e. less memory used)
c) There is no need to call constructors for parameters (i.e. faster)
d) All of the mentioned

Answer: d
Explanation: All the above mentioned are advantages and properties of call by reference.

5. If the user did not supply the value, what value will it take?
a) default value
b) rise an error
c) both default value & rise an error
d) error

Answer: a
Explanation: If the user did not supply the value means, the compiler will take the given value in the argument list.

6. Where can the default parameter be placed by the user?
a) leftmost
b) rightmost
c) both leftmost & rightmost
d) topmost

Answer: b
Explanation: To avoid the ambiguity between the non-default parameters and default parameters.

7. Which value will it take when both user and default values are given?
a) user value
b) default value
c) custom value
d) defined value

Answer: a
Explanation: The default value will be used when the user value is not given, So in this case, the user value will be taken.

8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void func(int a, bool flag = true)
{
if (flag == true )
{
cout << "Flag is true. a = " << a;
}
else
{
cout << "Flag is false. a = " << a;
}
}
int main()
{
func(200, false);
return 0;
}
a) Flag is true. a = 200
b) Flag is false. a = 100
c) Flag is false. a = 200
d) Flag is true. a = 100

Answer: c
Explanation: In this program, we are passing the value, as it evaluates to false, it produces the output as following.
Output:
Flag is false. a = 200

9. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
string askNumber(string prompt = "Please enter a number: ");
int main()
{
string number = askNumber();
cout << "Here is your number: " << number;
return 0;
}
string askNumber(string prompt)
{
string number;
cout << prompt;
cin >> number;
return number;
}
a) 5
b) 6
c) the number you entered
d) compile time error

Answer: c
Explanation: In this program, we are getting a number and printing it.
Output:
Please enter a number:
5
Here is your number:5

10. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void Values(int n1, int n2 = 10)
{
using namespace std;
cout << "1st value: " << n1;
cout << "2nd value: " << n2;
}
int main()
{
Values(1);
Values(3, 4);
return 0;
}
a) 1st value: 1
10
3
4
b) 1st value: 1
10
3
10
c) compile time error
d) runtime error

Answer: a
Explanation: In this program, We are passing the values as by default values rules it is working.
Output:
1st value: 1
2nd value: 10
1st value: 3
2nd value: 4