Vidyalelo
C++ Programming · all questions

Functions and Procedures in C++
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

176

Questions

9/9

Page

Pick an option on a question to see the right answer and solution.

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;
}
-----------------------------------------------

Select an option to see the answer and solution.

Which of the following is important in a function?

Select an option to see the answer and solution.

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<<A::func(a)<<endl;
	cout<<A::func(b);
	return 0;
}
------------------------------------------------

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
namespace first
{
    int x = 5;
    int y = 10;
}
namespace second
{
    double x = 3.1416;
    double y = 2.7183;
}
int main ()
{
    using first::x;
    using second::y;
    bool a, b;
    a = x > y;
    b = first::y < second::x;
    cout << a << b;
    return 0;
}

Select an option to see the answer and solution.

Which of the header file should be added in the following C++ code to properly run the program?
#include <iostream>
using namespace std;
int print_all (int num, ...)
{
    int sum = 0;
    va_list args;
    va_start (args,num);
    for (int i = 0; i < num; i++) 
    {
        int num = va_arg (args,int);
        cout<<num<<" ";
    }
    va_end (args);
    return sum;
}
int main (void)
{
    print_all(8, 1, 2, -1, 4, 12, -2, 9, 7);
    return 0;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
namespace A{
 
	int var = 10;
}
namespace B{
	int var = 5;
}
int main()
{
	int var = 20;
	using namespace B;
	cout<<var;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
#define MIN(a,b) (((a)<(b)) ? a : b)
int main ()
{
    float i, j;
    i = 100.1;
    j = 100.01;
    cout <<"The minimum is " << MIN(i, j) << endl;
    return 0;
}

Select an option to see the answer and solution.

What is the mandatory preprocessor directive for c++?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void func(int x)
{
    cout << x ;
}
int main()
{
    void (*n)(int);
    n = &func;
    (*n)( 2 );
    n( 2 );
    return 0;
}

Select an option to see the answer and solution.

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;
}

Select an option to see the answer and solution.

What does the client module import?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
namespace Box1
{
    int a = 4;
}
namespace Box2
{
    int a = 13;
}
int main ()
{
    int a = 16;
    Box1::a;
    Box2::a;
    cout << a;
    return 0;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
namespace A{
 
	int var = 10;
}
namespace B{
	int var = 5;
}
int main()
{
	using namespace B;
	cout<<var;
}

Select an option to see the answer and solution.

What will be the output of following C++ code?
#include <iostream>
#include <string>
using namespace std;
namespace My_old_school_and_college_friends_number
{
	long int f1 = 9999999999;
	long int f2 = 1111111111;
}
namespace contacts = My_old_school_and_college_friends_number;
int main(){
 
	cout<<contacts::f1;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <exception>
using namespace std;
int main()
{
    try 
    {
        int * array1 = new int[100000000];
        int * array2 = new int[100000000];
        int * array3 = new int[100000000];
        int * array4 = new int[100000000];
        cout << "Allocated successfully";
    }
    catch(bad_alloc&) 
    {
        cout << "Error allocating the requested memory." << endl;
    }
    return 0;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
double division(int a, int b)
{
    if (b == 0) 
    {
        throw "Division by zero condition!";
    }
    return (a / b);
}
int main ()
{
    int x = 50;
    int y = 2;
    double z = 0;
    try 
    {
        z = division(x, y);
        cout << z;
    }
    catch(const char *msg) 
    {
        cerr << msg;
    }
    return 0;
}

Select an option to see the answer and solution.