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: 2In C++, if a function f is defined as a friend function, then which of the following category of data members can be accessed using the function f?
Option D
A friend function is a function that is not a member of a class, but it is declared inside the class using the friend keyword.
A friend function is granted special permission to access all members of the class, including private, protected, and public members.
Although a friend function is defined outside the class, it can access the class private and protected data just like a member function.
#include <iostream>
using namespace std;
class Student
{
private:
int marks=90;
protected:
int rollNo=101;
public:
char name[20]="Suresh Kulahry";
friend void display(Student);
};
void display(Student st)
{
cout <<"Roll No : "<<st.rollNo<<endl;
cout <<"Name : "<<st.name<<endl;
cout <<"Marks : "<<st.marks<<endl;
}
int main()
{
Student std;
display(std);
return 0;
}
OUTPUT
Roll No : 101
Name : Suresh Kulahry
Marks : 90Q: 3Which 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: 4What 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: 5What 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: 6Which 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: 7Which 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: 8What 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: 9Pick 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: 10Consider 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.
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.