Q: 1 Identify 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’);
(I),(II),(III),(IV),(V),(VI)
(I),(III),(IV)
(I),(III),(IV),(V),(VI)
(III),(V)
[ Option C ]
Here the concept used is function with default argument. The key rule:
Now check each calling statement:
Q: 2 Which of the following function reduce the function call overhead?
Inline function.
Function with default argument.
Friend function.
Virtual function.
[ 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: 3 What is the scope of the variable declared in the user-defined function?
Whole program
Only inside the { } block
Inside the main function
None of the above
[ 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: 4 What 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;
}
New value of arg is 10
New value of arg is 550
New value of arg is 15
New value of arg is 55
[ 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: 5 Which of the following keyword specifies the compiler to substitute the code within the function definition for every instance of a function call?
virtual
static
inline
public
[ 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: 6 What 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;
}
6
24
Segmentation fault
None of the above
[ 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: 7 Pick the incorrect statement about inline function in C++.
They save overhead of a return call from a function.
They are generally very large and complicated function.
These functions are inserted/substituted at the point of call.
More than one of the above.
[ 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: 8 Consider 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.
Run time error
112
1110
Compile time error
[ 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: 9 A function that is substituted by a copy of its code at the point of function call, by the compiler is
Friend Function
Inline Function
Class
Virtual Function
[ 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: 10 Which function is not a member of class?
Virtual function
Friend function
Inline function
Static function
[ 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: 11 The default arguments are used when?
Function is void.
When arguments are passed by reference.
When function is called with extra argument.
When function is called with less argument.
[ 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: 12 A function that can access private members of a class, even though it is not a member of the class itself, is
A private function
A friend function
A inline function
Not possible
[ 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: 13 Inline function is not possible when?
A function containing a loop
A function containing double type variable
A function containing return statement
All of the above
[ 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.
Q: 14 Determine output of the following C++ code?
#include<iostream>
using namespace std;
int myfunc(int a);
int myfunc(int a, int b);
int main()
{
int (*fp) (int a, int b);
int (*fp1) (int a);
fp=myfunc;
fp1=myfunc;
cout<<fp(5,6);
return 0;
}
int myfunc(int a)
{
return a;
}
int myfunc(int a, int b)
{
return a*b;
}
6
30
11
5
[ Option B ]
In the given C++ code, two overloaded versions of the function myfunc are defined—one taking a single int parameter and another taking two int parameters. Correspondingly, two function pointers are declared:
When assigning an overloaded function to a function pointer, the type of the pointer determines which overload is selected. Since fp is of type int (*)(int, int), it correctly binds to myfunc(int, int), and similarly, fp1 binds to myfunc(int).
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.