C++ Questions and Answers Part-24

1. Which looping process is best used when the number of iterations is known?
a) for
b) while
c) do-while
d) all looping processes require that the iterations be known

Answer: a
Explanation: Because in for loop we are allowed to provide starting and ending conditions of loops, hence fixing the number of iterations of loops, whereas no such things are provided by other loops.

2. Where does the execution of the program starts?
a) user-defined function
b) main function
c) void function
d) else function

Answer: b
Explanation: Normally the execution of the program in c++ starts from main only.

3. What are mandatory parts in the function declaration?
a) return type, function name
b) return type, function name, parameters
c) parameters, function name
d) parameters, variables

Answer: a
Explanation: In a function, return type and function name are mandatory all else are just used as a choice.

4. which of the following is used to terminate the function declaration?
a) :
b) )
c) ;
d) ]

Answer: c
Explanation: ; semicolon is used to terminate a function declaration statement in C++.

5. How many can max number of arguments present in function in the c99 compiler?
a) 99
b) 90
c) 102
d) 127

Answer: d
Explanation: C99 allows to pass a maximum of 127 arguments in a function.

6. Which is more effective while calling the functions?
a) call by value
b) call by reference
c) call by pointer
d) call by object

Answer: b
Explanation: In the call by reference, it will just passes the reference of the memory addresses of passed values rather than copying the value to new memories which reduces the overall time and memory use.

7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void mani()
void mani()
{
cout<<"hai";
}
int main()
{
mani();
return 0;
}
a) hai
b) haihai
c) compile time error
d) runtime error

Answer: c
Explanation: We have to use the semicolon to declare the function in line 3. This is called a function declaration and a function declaration ends with a semicolon.

8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void fun(int x, int y)
{
x = 20;
y = 10;
}
int main()
{
int x = 10;
fun(x, x);
cout << x;
return 0;
}
a) 10
b) 20
c) compile time error
d) 30

Answer: a
Explanation: In this program, we called by value so the value will not be changed, So the output is 10
Output:
10

9. What is the scope of the variable declared in the user defined function?
a) whole program
b) only inside the {} block
c) the main function
d) header section

Answer: b
Explanation: The variable is valid only in the function block as in other.

10. How many minimum number of functions should be present in a C++ program for its execution?
a) 0
b) 1
c) 2
d) 3

Answer: b
Explanation: The execution of a C++ program starts from main function hence we require atleast 1 function to be present in a C++ program to execute and i.e. the main function.