C++ Programming Questions and Answers – Functions

«
»

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Functions”.

1. Which of the following is the default return value of functions in C++?
a) int
b) char
c) float
d) void
View Answer

Answer: a
Explanation: C++ uses int as the default return values for functions. It also restricts that the return type of the main function must be int.
advertisement

2. What happens to a function defined inside a class without any complex operations (like looping, a large number of lines, etc)?
a) It becomes a virtual function of the class
b) It becomes a default calling function of the class
c) It becomes an inline function of the class
d) The program gives an error
View Answer

Answer: c
Explanation: Any function which is defined inside a class and has no complex operations like loops, a large number of lines then it is made inline.

3. What is an inline function?
a) A function that is expanded at each call during execution
b) A function that is called during compile time
c) A function that is not checked for syntax errors
d) A function that is not checked for semantic analysis
View Answer

Answer: a
Explanation: Inline function is those which are expanded at each call during the execution of the program to reduce the cost of jumping during execution.

4. An inline function is expanded during ______________
a) compile-time
b) run-time
c) never expanded
d) end of the program
View Answer

Answer: b
Explanation: An inline function is expanded during the runtime of a program.

5. In which of the following cases inline functions may not word?
i) If the function has static variables.
ii) If the function has global and register variables.
iii) If the function contains loops
iv) If the function is recursive
a) i, iv
b) iii, iv
c) ii, iii, iv
d) i, iii, iv
View Answer

Answer: d
Explanation: A function is not inline if it has static variables, loops or the function is having any recursive calls.

6. When we define the default values for a function?
a) When a function is defined
b) When a function is declared
c) When the scope of the function is over
d) When a function is called
View Answer

Answer: b
Explanation: Default values for a function is defined when the function is declared inside a program.

7. Where should default parameters appear in a function prototype?
a) To the rightmost side of the parameter list
b) To the leftmost side of the parameter list
c) Anywhere inside the parameter list
d) Middle of the parameter list
View Answer

Answer: a
Explanation: Default parameters are defined to the rightmost side of parameter list in a function to differentiate between the normal and default parameters for example if a function is defined as fun(int x = 5, int y) then if we call fun(10) then 10 should be given to x or y because one can apply both logics like x = 10 already defined and 10 passed is for y but if compiler reads it from left to right it will think it is for x and no parameter is given for y, therefore, the compiler will give error.
advertisement

8. If an argument from the parameter list of a function is defined constant then _______________
a) It can be modified inside the function
b) It cannot be modified inside the function
c) Error occurs
d) Segmentation fault
View Answer

Answer: b
Explanation: A function is not allowed a constant member of the parameter list.

9. Which of the following feature is used in function overloading and function with default argument?
a) Encapsulation
b) Polymorphism
c) Abstraction
d) Modularity
View Answer

Answer: c
Explanation: Both of the above types allows a function overloading which is the basic concept of Polymorphism.

10. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
 
int fun(int x = 0, int y = 0, int z)
{  return (x + y + z); }
 
int main()
{
   cout << fun(10);
   return 0;
}

a) 10
b) 0
c) Error
d) Segmentation fault
View Answer

Answer: a
Explanation: Default arguments should always be declared at the rightmost side of the parameter list but the above function has a normal variable at the rightmost side which is a syntax error, therefore the function gives an error.

11. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
 
class Test
{
  protected:
    int x;
  public:
    Test (int i):x(i) { }
    void fun() const  { cout << "fun() const " << endl; }
    void fun()        {  cout << "fun() " << endl;     }
};
 
int main()
{
    Test t1 (10);
    const Test t2 (20);
    t1.fun();
    t2.fun();
    return 0;
}

a)

fun() 
fun() const
advertisement

b)

fun() const
fun() 

c)

fun() 
fun() 

d)

fun() const
fun() const
View Answer
Answer: a
Explanation: As the object declared are of two types one is normal object and other is constant object So normal objects calls normal fun() whereas constant objects calls constant fun().
 
 

12. 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
View Answer

Answer: d
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.

13. What will be the output of the following C++ code?

advertisement
#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
View Answer

Answer: a
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.

14. From which function the execution of a C++ program starts?
a) start() function
b) main() function
c) new() function
d) end() function
View Answer

Answer: b
Explanation: The execution of a C++ program starts from the main() function.

15. 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
View Answer

Answer: c
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.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn