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 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: 5 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: 6 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: 7 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: 8 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: 9 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: 10 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.