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: 1The 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
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: 2What 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;
}
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: 3Find output of the following program?
void main()
{
int a=14;
int &b=a;
b++;
cout<<a;
}
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: 4Consider the following C++ snippet, which allocates memory?
int *ptr;
ptr=new int[4];
Which of the following statements may make the program crash when executed subsequently?
Option C
The statement ptr = new int[4]; dynamically allocates memory for 4 integers. The valid indices for this array are 0, 1, 2, and 3.
| STATEMENT | EXPLANATION |
|---|---|
| ptr++ | Moves the pointer to the next integer location. It does not immediately cause a crash. |
| ptr += 3 | Moves the pointer forward by three positions within the allocated block. |
| ptr[4] = 0x16 | Attempts to access the 5th element, which is outside the allocated array bounds. This may cause a Runtime Crash or Undefined Behavior. |
| ptr = null | Sets the pointer to null. It does not crash unless dereferenced later. |
Q: 5What is output of the given code?
void main()
{
int a=3, b, *p, **q;
p=&a;
q=&p;
b=2*(**q+*p);
cout<<b;
}
Option B
The given program uses a pointer p and a double pointer q. The variable a is initialized with value 3.
The pointer p stores the address of a, and the double pointer q stores the address of p. Therefore, both *p and **q give the value 3.
The expression b=2*(**q+*p) becomes 2*(3+3), which is equal to 12.
Q: 6What will happen if the following C++ statement is compiled and executed?
int *ptr=NULL;
delete ptr;
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.
You have reached the end of this topic. Continue learning with the next topic below.
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.