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: 4What is the correct interpretation of the following declaration in C++?
int (*FPTR)(char*)
Option C
In C++, a function pointer is a pointer that stores the address of a function. It allows a program to call a function indirectly through the pointer. Consider the declaration:
int (*FPTR)(char*)
Therefore, FPTR is a pointer to a function that takes a character pointer (char*) as an argument and returns an integer value.
| Declaration | Meaning |
|---|---|
| int (*FPTR)(char*); | Pointer to a function taking char* and returning int. |
| void (*FPTR)(int); | Pointer to a function taking int and returning void. |
| float (*FPTR)(float, float); | Pointer to a function taking two float arguments and returning float. |
| char* (*FPTR)(int); | Pointer to a function taking int and returning char*. |
#include <iostream>
using namespace std;
int findLength(char *str)
{
int count=0;
while (*str!='\0')
{
count++;
str++;
}
return count;
}
int main()
{
int (*FPTR)(char*);
FPTR=findLength;
char mystr[]="Suraku Academy";
cout<<"Length : "<< FPTR(mystr);
return 0;
}
OUTPUT
Length : 14Q: 5Consider 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: 6Consider the following declaration in C++:
int C, *PTR;
Which of the following statement is TRUE?
Option C
A pointer is a special type of variable that stores the memory address of another variable instead of its value. In the given declaration:
int C, *PTR;
The address-of operator (&) is used to obtain the memory address of a variable, while the dereference operator (*) is used to access the value stored at the address held by a pointer.
Therefore, to store the address of C in PTR, we write PTR=&C;
Q: 7Consider the following C++ code segment:
int a=10, b=20;
int *p=&a, *q=&b;
p=q;
With respect to the code segment mentioned above, which of the following statement is correct?
Option D
A pointer is a special variable that stores the memory address of another variable. In the given code:
int a = 10, b = 20;
int *p = &a, *q = &b;
p = q;
Initially:
After executing the statement p = q;, the address stored in q is copied into p. As a result, both pointers store the same address, which is the address of b.
The values of a and b remain unchanged. Only the pointer p changes its target.
#include <iostream>
using namespace std;
int main()
{
int a=10, b=20;
int *p=&a;
int *q=&b;
cout<<"a = "<<a<<", b = "<< b<<endl;
cout<<"p points to value = " <<*p<<endl;
cout<<"q points to value = " <<*q<<endl;
cout<<"Address stored in p = " <<p<<endl;
cout<<"Address stored in q = " <<q<<endl;
p=q;
cout<<"After p = q\n";
cout<<"a = " <<a<< ", b = " <<b<<endl;
cout<<"p points to value = " <<*p<<endl;
cout<<"q points to value = " <<*q<<endl;
cout<<"Address stored in p = " <<p<<endl;
cout<<"Address stored in q = " <<q<<endl;
return 0;
}
OUTPUT
a = 10, b = 20
p points to value = 10
q points to value = 20
Address stored in p = 0x7ffe96a25d8c
Address stored in q = 0x7ffe96a25d88
After p = q
a = 10, b = 20
p points to value = 20
q points to value = 20
Address stored in p = 0x7ffe96a25d88
Address stored in q = 0x7ffe96a25d88Q: 8Which option gives the correct interpretation of the following declaration in C++?
int (*p[5])();
Option B
In C++, a declaration should always be read starting from the variable name and then moving outward according to the precedence of operators. Consider the declaration:
int (*p[5])();
Therefore, the declaration means, p is an array of 5 pointers to functions, where each function takes no arguments and returns an integer value.
Q: 9What 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: 10What 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.
Q: 11Which of the following is not a correct declaration statement in C++?
Option C
In C++, a pointer must store the address of a variable of the same data type. Means,
Assigning the address of one data type to a pointer of another data type is not allowed without an explicit type cast.
The given statement,
int p;
double *fp = &p;
Since int* cannot be assigned to double*, this declaration is incorrect and results in a Compile-Time Error.
| Declaration | Explanation |
|---|---|
| int *ptr=0, p; | Correct. ptr is a pointer to an int and is initialized with 0 (NULL pointer). p is a normal integer variable. Both declarations are valid. |
| int p, *ptr=0; | Correct. p is an integer variable, while ptr is an integer pointer initialized to 0 (NULL). This is also a valid declaration. |
| int p; double *fp=&p; | Incorrect. |
| int *p, *q, r; | Correct. p and q are pointers to integers, whereas r is a normal integer variable. |
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.