Home »
Aptitude Questions and Answers »
C++ Aptitude Questions and Answers
C++ programming functions, blocks Aptitude Questions and Answers
C++ Functions and Block based aptitude questions and answers - This section contains Aptitude Questions and Answers based on C++ functions and blocks.
List of Functions and Blocks related C++ programming Aptitude Questions and Answers
1) What will be the output of following program?
#include <iostream>
using namespace std;
void fun1(int x)
{
cout<<x<<endl;
}
int main()
{
//function calling
fun1(10,20);
return 0;
}
- Run Time Error
- Compile Time Error
- 10
- 10, 20
Correct Answer - 2
Compile Time Error
Since function declaration has only one integer argument while we are passing to arguments, hence program will through compiler error.
2) What will be the output of following program?
#include <iostream>
using namespace std;
int main()
{
//function declaration
void fun(void);
//function calling
fun();
cout<<"::OK"<<endl;
return 0;
}
//function definition
void fun(void)
{
cout<<"Hello";
}
- Compile Time Error
- Run Time Error
- ::OK
- Hello::OK
Correct Answer - 4
Hello::OK
We can declare any function within the main().
3) What will be the output of following program?
#include <iostream>
using namespace std;
//declaration and definition
void print_value(int x,int y=10)
{
cout<<"x:"<<x<<",y:"<<y<<endl;
}
int main()
{
//function calling
print_value(100);
return 0;
}
- x:100,y:10
- x:100,y:100
- x:100,y:Garbage
- Error
Correct Answer - 1
x:100,y:10
In the function declaration, argument y is a default parameter and while calling we are not passing second parameter hence 10 will be the value of second parameter y.
4) What will be the output of following program?
#include <iostream>
using namespace std;
//declaration and definition
void fun(const int x=10)
{
cout<<"x:"<<x<<endl;
}
int main()
{
const int a=100;
cout<<"call 1:";
fun();
cout<<"call 2:";
fun(a);
return 0;
}
- Call 1:x:10
Call 2:x:100
- Call 1:x:10
Call 2:x:10
- Call 1:x:100
Call 2:x:100
- Error
Correct Answer - 1
Call 1:x:10
Call 2:x:100
In the first calling there is no parameter, hence default value 10 will be printed and in the second calling 100 is passing as a parameter so 100 will be printed after second calling.
5) Which is the correct form to call function fun1()?
#include <iostream>
using namespace std;
namespace myfunctions
{
void fun1(void)
{
cout<<"Fun1"<<endl;
}
}
int main()
{
.....
return 0;
}
- fun1();
- myfunctions.fun1();
- myfunctions::fun1();
- myfunctions->fun1();
Correct Answer - 3
myfunctions::fun1();
fun1() is declared and define within the namespace and we can access a function of a namespace using Scope Resolution Operator (::).
6) What will be the output of following program?
#include <iostream>
using namespace std;
namespace myfunctions
{
void fun1(void)
{
cout<<"Fun1"<<endl;
}
void fun1(int a)
{
cout<<a<<endl;
}
}
int main()
{
myfunctions::fun1(10.2f);
return 0;
}
- Error
- 10
- 10.200000
- 10.2
Correct Answer - 2
10
In the namespace there are two functions with same name (it is called function overloading), we are calling fun1 with value 10.2f and this value will convert into integer (implicit conversion) hence second fun1 will be called.