0% found this document useful (0 votes)
5 views6 pages

Pointer Practice Short Questions

The document contains multiple C++ program segments that demonstrate various programming concepts including pointer manipulation, array handling, and constant variables. Each segment is followed by its expected output or error message. The programs illustrate different behaviors of pointers, array indexing, and the use of constants in C++.

Uploaded by

i210865
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Pointer Practice Short Questions

The document contains multiple C++ program segments that demonstrate various programming concepts including pointer manipulation, array handling, and constant variables. Each segment is followed by its expected output or error message. The programs illustrate different behaviors of pointers, array indexing, and the use of constants in C++.

Uploaded by

i210865
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Program / Program segment Output/error

1
int num1, num2=10;
float f1=2.5;
float * fptr;
int * iptr;
iptr = &num2;
*iptr = 50;
fptr = & f1;
num1 = num2 + *iptr * *fptr;
cout << num1 << endl;

2
void doSomething(int a[], int size)
{
int * p; int i=size-1;
for (int* p=a+(size-1);p >= a; p--)
{
*p = *(p-i);
i--;
}
}

int main()
{
int arr[5] = {8,2,7,4,9};
doSomething(arr,5);
for(int i=0;i<5;i++)
cout<<arr[i]<<endl;
return 0;
}

3
int* doMagic(int *p, int*q)
{
int *t = new int();
t = p;
p = q;
q = t;
return t;
}
int main()
{
int p = 5, q = 10;
int *t = NULL;
t=doMagic(&p,&q);
cout<<"p="<<p<<endl;
cout<<"q="<<q<<endl;
cout<<"t="<<*t<<endl;
return 0;
}

4
int arr[5] = {1,2,3,4,5};
int* arrptr = arr;
if (arr < (arrptr+2))
cout << "True";
else
cout << "False";
if (&arr[4] < &arr[1])
cout << "True";
else
cout << "False";

5
const int i = 20;
const int* const ptr = &i;
(*ptr)++;
int j = 15;
ptr = &j;
cout << i;

6
char a[] = { 'A', 'B', 'C', 'D' };
char* ppp = &a[1];

*ppp++;
cout << *ppp++;
cout << ++*ppp;

7
int i = 5, *j;
j = &i;
cout << i * *j * i + *j;
(B)
Program / Program segment Output/error
1
int num2=20, num1=30;
float f1=3.5;
float * fptr;
int * iptr;
iptr = &num2;
*iptr += 70;
fptr = & f1;
num1 = num2 + *iptr * *fptr;
cout << "num1 = " << num1 << endl;

2
void doSomething(int a[], int size)
{
int * p; int i=0;
for (int* p=a;p < a+size; p++)
{
*p = *(p-i);
i++;
}
}
int main()
{
int arr[5] = {1,2,3,4,5};
doSomething(arr,5);
for(int i=0;i<5;i++)
cout<<arr[i]<<endl;
return 0;
}

3
int* doMagic( int *p, int *q)
{
int *t = new int();
*t = *p;
*p = *q;
*q = *t;
return t;
}
int main()
{
int p = 5, q = 10;
int *t = NULL;
t=doMagic(&p,&q);
cout<<"p="<<p<<endl;
cout<<"q="<<q<<endl;
cout<<"t="<<*t<<endl;
return 0;
}

4
int arr[5] = {1,2,3,4,5};
int* arrptr = arr;
if (arrptr != &arr[2])
cout << "True";
else
cout << "False";
if (arr != &arrptr[0])
cout << "True";
else
cout << "False";

5
const int k = 50;
const int* const ptr = &k;
(*ptr)++;
int m = 5;
ptr = &m;
cout << k;

6
char a[] = { 'B', 'C', 'D', 'A' };
char* ppp = &a[2];
*++ppp;
cout << ++*ppp;
cout << *--ppp;
7
int i = 6, *j;
j = &i;
cout << i * *j * i + *j;
Program segment Output/error
1
int num1=60, num2=80;
float f1=2.5;
float * fptr;
int * iptr;
iptr = &num1;
*iptr -= 50;
fptr = & f1;
num1 = num2 + *iptr * *fptr;
cout << num1 << endl;

2
void doSomething(int a[], int size)
{
int * p=a;
for (int i =0;i < size; i++)
{
*(a+i) = *(p-i);
p++;
}
}

int main()
{
int arr[5] = {5,4,3,2,1};
doSomething(arr,5);
for(int i=0;i<5;i++)
cout<<arr[i]<<endl;
return 0;
}

3
int* doMagic( int *p, int *q)
{
int *t = new int();
*t = *p;
*p = *q;
q = t;
return t;
}

int main()
{
int p = 5, q = 10;
int *t = NULL;
t=doMagic(&p,&q);
cout<<"p="<<p<<endl;
cout<<"q="<<q<<endl;
cout<<"t="<<*t<<endl;
return 0;
}

4
int arr[5] = {1,2,3,4,5};
int* arrptr = arr;
if (arr[0] >= *(arrptr+2))
cout << "True";
else
cout << "False";
if (&arr[1] < &arrptr[4])
cout << "True";
else
cout << "False";

5
const int x = 150;
const int* const ptr = &x;
*ptr++;
int y = 15;
ptr = &y;
cout << *ptr;

6
char a[] = { 'A', 'B', 'C', 'D' };
char* ppp = &a[0];
*ppp++;
cout << *++ppp;
cout << --*ppp;

7
int i = 4, *j;
j = &i;
cout << i * *j * i + *j;

You might also like