This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1Identify which of the following function calls are allowed. The prototype of demo function is defined as: (read very carefully)
int demo(int, int b=10, c=5);
(I) demo(23);
(II) demo();
(III) demo(10,20,30);
(IV) demo(14,15);
(V) demo(‘a’, ‘b’, ‘c’);
(VI) demo(5, ‘c’);
Option C
Here the concept used is function with default argument. The key rule:
Now check each calling statement:
Q: 2Which of the following function reduce the function call overhead?
Option A
When a function is called in a program, the system needs to perform several additional operations apart from executing the actual function code. These operations include passing arguments from the calling function to the called function, transferring control (jumping) to the function’s code, and after execution, returning control back to the calling function along with any return value.
An inline function reduces this overhead by replacing the function call with the actual function code at the place where it is called.
inline int add(int a, int b)
{
return a+b;
}
Here, instead of calling add(), the compiler directly inserts a+b, which saves execution time.
Q: 3What is the scope of the variable declared in the user-defined function?
Option B
In C++, a variable declared inside a user-defined function has local scope, meaning it is accessible only within the curly braces { } block of that function. It cannot be accessed from other functions or outside the function in which it is declared. Once the function execution ends, the variable is destroyed and its memory is released.
Q: 4What is the output of the following C++ program segment?
void check(int &arg1)
{
arg1=550;
}
int main()
{
int arg=10;
check(arg);
cout<<”New value of arg is “<<arg;
return 0;
}
Option B
In C++, when a function parameter is passed using a reference using & (Call by Reference), it means the function works directly on the original variable, not on a copy. Any change made inside the function will affect the original variable.
Here, the function check(int &arg1) takes the argument by reference. In main(), the variable arg is initialized to 10 and passed to the function. Inside the function, arg1=550; modifies the same memory location as arg.
So, after the function call, the value of arg becomes 550.
Q: 5Which of the following keyword specifies the compiler to substitute the code within the function definition for every instance of a function call?
Option C
In C/C++, the inline keyword is used to suggest the compiler to replace a function call with the actual code of the function. This means that instead of jumping to the function during execution, the compiler directly inserts the function’s code wherever it is called.
This substitution, occurs at compile time and can improve performance for small, frequently called functions by reducing execution time, though it may increase the overall program size due to code duplication.
#include <iostream>
using namespace std;
inline int add(int a, int b)
{
return(a+b);
}
int main()
{
int x=5, y=3;
int result=add(x,y);
cout<<"Addition: "<<result;
return 0;
}
In this, instead of calling the add() function normally, the compiler may replace add(x,y) with x+y directly in the code.
Note:
Finally, inline functions are best suited for small, simple, frequently called functions, such as getters or small arithmetic operations.
Q: 6Which statement is correct about static member function in C++?
I. It can access to only other static members declared in the same class.
II. It cannot be called using the class name (instead of its objects).
Option A
In C++, a static member function belongs to the class rather than to the objects of the class. It is declared using the static keyword.
A static member function:
#include<iostream>
using namespace std;
class Demo
{
static int a;
public:
static void show()
{
cout<<a;
}
};
int Demo::a=10;
int main()
{
Demo::show();
return 0;
}
Q: 7What will be the output of the following C++ code?
#include<iostream>
using namespace std;
long factorial(long a)
{
if(a>1)
return(a*factorial(a+1));
else
return(1);
}
int main()
{
long num=3;
cout<<num<<”!=”<<factorial(num);
return 0;
}
Option C
Normally, factorial of n is calculated as n*factorial(n-1). Here, factorial(a+1) increases a instead of decreasing it, so the function never reaches the base case (a<=1). This causes infinite recursion, which eventually overflows the stack.
When the program runs, it will produce a runtime error called a segmentation fault, because the system runs out of memory due to too many recursive calls.
Q: 8Pick the incorrect statement about inline function in C++.
Option B
Inline functions in C++ are meant to improve performance by avoiding the overhead of a function call. Instead of jumping to a function and returning back, the compiler substitutes the function’s code directly at the place where it is called.
This helps save time, especially for small and frequently used functions. However, inline functions are not supposed to be large or complex.
Q: 9Consider the following C++ code:
#include<iostream>
int change (int &);
int main()
{
int a=1, b=2;
a=change(b);
cout<<a<<b;
return 0;
}
int change (int &x)
{
x=10;
return(11);
}Find output of the above code.
Option C
In this C++ program, the function change is defined to take an integer reference as its parameter (int &x). Passing by reference (call by reference) means any changes made to x inside the function will directly affect the original variable passed in.
Q: 10A function that is substituted by a copy of its code at the point of function call, by the compiler is
Option B
In C++, an inline function is a function whose code is expanded or copied at the place where it is called, instead of performing a normal function call. This helps in reducing function call overhead and improves performance for small functions.
Q: 11Which function is not a member of class?
Option B
A friend function is not a member of the class, even though it is allowed to access the private, protected, or public members of that class. Friend functions are declared inside the class using the friend keyword.
Q: 12The default arguments are used when?
Option D
In C++, default arguments are values provided to function parameters at the time of function declaration. These default values are automatically used when the function call does not supply enough arguments.
#include <iostream>
using namespace std;
void addition(int x, int y=0, int z=0)
{
int p=x+y+z;
cout<<"Addition : "<<p<<endl;
}
int main()
{
addition(10);
addition(10,20);
addition(10,20,30);
return 0;
}
OUTPUT
Addition : 10
Addition : 30
Addition : 60
Q: 13Which of the following is specified by function prototype in C++?
I. Return Type
II. Function Name
III. Number and Types of Argument
Option D
In C++, a function prototype is a declaration of a function that provides information to the compiler before the function is actually defined. It tells the compiler how the function will be used.
A function prototype specifies:
Syntax:
E.g.:
Q: 14A function that can access private members of a class, even though it is not a member of the class itself, is
Option B
In C++, a friend function is a special function that is not a member of a class but is still allowed to access its private and protected members. This is done using the friend keyword inside the class definition.
For example, a function declared as friend inside a class can directly access private variables of that class, even though it is defined outside the class.
E.g.:
#include <iostream>
using namespace std;
class Demo
{
private:
int a;
public:
Demo()
{
a=22;
}
friend void showData(Demo);
};
void showData(Demo d1)
{
cout<<"Value : "<<d1.a; // Accessing private member.
}
int main()
{
Demo obj;
showData(obj);
return 0;
}
OUTPUT
Value : 22
Q: 15Inline function is not possible when?
Option A
An inline function is a function whose code is substituted at the place of function call by the compiler to reduce function call overhead.
However, inline expansion is not suitable in certain cases, especially when it increases code size unnecessarily.
If a function contains a loop, inlining it may cause the loop body to be copied multiple times, resulting in larger code. Therefore, such functions are generally not considered inline.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.