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

6/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?
#include <iostream>
using namespace std;
void Values(int n1, int n2 = 10)
{
    using namespace std;
    cout << "1st value: " << n1;
    cout << "2nd value: " << n2;
}
int main()
{
    Values(1);
    Values(3, 4);
    return 0;
}

Select an option to see the answer and solution.

What is the maximum number of arguments or parameters that can be present in one function call?

Select an option to see the answer and solution.

How to print the value of the i variable inside namespace B?
namespace A{
	int var = 10;
	namespace B{
		int i = 15;
	}
}

Select an option to see the answer and solution.

What is the use of Namespace?

Select an option to see the answer and solution.

How can you access the arguments that are manipulated in the function?

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 var = 5;
}
namespace second
{
    double var = 3.1416;
}
int main ()
{
    int a;
    a = first::var + second::var;
    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>
using namespace std;
int main()
{
    int age = 0;
    try 
    {
        if (age < 0) 
        {
            throw "Positive Number Required";
        }
        cout << age;
    }
    catch(const char *Message)
    {
        cout << "Error: " << Message;
    }
    return 0;
}

Select an option to see the answer and solution.

What is the correct syntax of defining a namespace?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
 {
    char* buff;
    try 
    {
         buff = new char[1024];
         if (buff == 0)
           throw "Memory allocation failure!";
        else
           cout << sizeof(buff) << "Byte successfully allocated!"<<endl;
     }
    catch(char *strg)
    {
        cout<<"Exception raised: "<<strg<<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;
int gcd (int a, int b)
{
    int temp;
    while (b != 0) 
    {
        temp = a % b;
        a = b;
        b = temp;
    }
    return(a);
}
int main ()
{
    int x = 15, y = 25;
    cout << gcd(x, y);
    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 & WeeklyHours()
{
    double h = 46.50;
    double &hours = h;
    return hours;
}
int main()
{
    double hours = WeeklyHours();
    cout << "Weekly Hours: " << hours;
    return 0;
}

Select an option to see the answer and solution.

How many minimum number of functions should be present in a C++ program for its execution?

Select an option to see the answer and solution.

What is the ability to group some lines of code that can be included?
in the program?

Select an option to see the answer and solution.

Function overloading is also similar to which of the following?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int add(int first, int second)
{
    return first + second + 15;
}
int operation(int first, int second, int (*functocall)(int, int))
{
    return (*functocall)(first, second);
}
int main()
{
    int  a;
    int  (*plus)(int, int) = add;
    a = operation(15, 10, plus);
    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 <stdarg.h>
using namespace std;
void fun(std::string msg, ...);
int main()
{
    fun("cPlusPlus", 1, 4, 7, 11, 0);
    return 0;
}
void fun(std::string msg, ...)
{
    va_list ptr;
    int num;
    va_start(ptr, msg);
    num = va_arg(ptr, int);
    num = va_arg(ptr, int);
    cout << num;
}

Select an option to see the answer and solution.

What changes you can do in the header files to avoid the redefinition that compiler will give when both the header files are included in the same program keeping the declaration of both the functions same?
Content of h1.h
------------------------------------------------
h1.h
#include <iostream>
using namespace std;
int func(int a){
	cout<<"Multiplied by 2";
	return 2*a;
}
------------------------------------------------
 
Content of h2.h
------------------------------------------------
h2.h
#include <iostream>
using namespace std;
int func(int a){
	cout<<"divided by 2";
	return a/2;
}
------------------------------------------------

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int n(char, int);
int (*p) (char, int) = n;
int main()
{
    (*p)('d', 9);
    p(10, 9);
    return 0;
}
int n(char c, int i)
{
    cout << c <<  i;
    return 0;
}

Select an option to see the answer and solution.

Where does the execution of the program starts?

Select an option to see the answer and solution.

When we define the default values for a function?

Select an option to see the answer and solution.