Q: 1 The correct statement for a function that takes pointer to a float, a pointer to a pointer to a char and returns a pointer to a pointer to an integer is
int** fun(float**, char **)
int* fun(float*, char*)
int** fun(float*, char**)
More than one of the above
[ Option C ]
In C++, function declarations specify the return type, function name, and parameter types. Pointers use * (float* for pointer to float, char** for pointer to pointer to char, int** for pointer to pointer to int).
Combining these, the correct function declaration is int** fun(float*, char**);.
Q: 2 What will be the output of the following C++ code?
#include<iostream.h>
using namespace std;
void square(int *x, int *y)
{
*x=(*x)*--(*y);
}
int main()
{
int number=30;
square(&number,&number);
cout<<number;
return 0;
}
30
870
Segmentation fault
None of the above
[ Option B ]
In this program, the variable number is passed to both parameters x and y, meaning both pointers refer to the same memory location. Inside the function, the statement *x = (*x) * --(*y); first decreases the value of number using the pre-decrement operator --(*y), so number becomes 29.
Then, the expression multiplies the original value by 29. Since the original value was 30, the calculation becomes 30 * 29, resulting in 870.
Q: 3 Find output of the following program?
void main()
{
int a=14;
int &b=a;
b++;
cout<<a;
}
14
15
Prints memory location
Compile time error occurred, illegal use of ‘address of’ operator.
[ Option B ]
Here the variable a is initialized with the value 14. The statement int &b = a; creates b as a reference variable, which means b is an alias of a and both refer to the same memory location.
When the statement b++; is executed, it increments the value stored at that memory location. Since b and a refer to the same variable, the value of a also becomes 15. Finally, cout<<a; prints the updated value of a, which is 15.
Q: 4 What will happen if the following C++ statement is compiled and executed?
int *ptr=NULL;
delete ptr;
The program is not semantically correct
The program is compiled and executed successfully
The program gives a compile-time error
More than one of the above
[ Option B ]
In this program, the pointer ptr is initialized to NULL, which means it points to nothing. When we use delete ptr;. In C++, deleting a NULL pointer is safe. If the pointer is NULL, delete does nothing and does not cause any error. Therefore, the program is semantically correct, compiles without any issues, and executes successfully without crashing.
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.