What will be the output of the following C++ code?
Content of header file h1.h
------------------------------------------------
h1.h
#include <iostream>
using namespace std;
namespace A{
int func(int a){
cout<<"using namespace A";
return 2*a;
}
}
------------------------------------------------
Content of header file h2.h
------------------------------------------------
h2.h
#include <iostream>
using namespace std;
namespace B{
float func(float a){
cout<<"using namespace B";
return 2*a;
}
}
------------------------------------------------
Content of program.cpp
------------------------------------------------
#include <iostream>
#include <string>
#include "h1.h"
#include "h2.h"
using namespace std;
using namespace A;
using namespace B;
int main(int argc, char const *argv[])
{
/* code */
int a = 10;
float b = 10.0;
cout<<func(a)<<endl;
cout<<func(b);
return 0;
}
-----------------------------------------------
Content of header file h1.h
------------------------------------------------
h1.h
#include <iostream>
using namespace std;
namespace A{
int func(int a){
cout<<"using namespace A";
return 2*a;
}
}
------------------------------------------------
Content of header file h2.h
------------------------------------------------
h2.h
#include <iostream>
using namespace std;
namespace B{
float func(float a){
cout<<"using namespace B";
return 2*a;
}
}
------------------------------------------------
Content of program.cpp
------------------------------------------------
#include <iostream>
#include <string>
#include "h1.h"
#include "h2.h"
using namespace std;
using namespace A;
using namespace B;
int main(int argc, char const *argv[])
{
/* code */
int a = 10;
float b = 10.0;
cout<<func(a)<<endl;
cout<<func(b);
return 0;
}
-----------------------------------------------A. using namespace A10
using namespace B10
B. using namespace A20
using namespace B20
C. Error due to clash of func()
D. This is not allwed in C++
Answer: Option B
What is the correct syntax for defining a function in C++?
A. returnType functionName(parameters) { body; }
B. functionName(parameters) { returnType body; }
C. functionName(returnType, parameters) { body; }
D. returnType functionName(parameters, body) { }
What is the purpose of a function prototype in C++?
A. Determines the parameters of the function
B. Specifies the return type of the function
C. Provides the implementation of the function
D. Declares the function before it is defined
Which keyword is used to define a function in C++ that does not return any value?
A. return
B. null
C. void
D. None of the above
What is the correct way to call a function named "add" that takes two parameters in C++?
A. add(a, b);
B. add(int a, int b);
C. functionName = add(a, b);
D. add.functionName(a, b);

Join The Discussion