11
DPS – Modern Indian School Doha, Qatar Pointers Worksheet Class XII Name: _________________ 1. Find mistakes, if any, in the following definition and make necessary correction: char S[10], *ptr = S[6]; ______________________________________ ________ 2. Distinguish between: int *ptr = new int(5); and int *ptr = new int[5]; ______________________________________ ______________________________________ ______________________________________ ________________________ 3. Consider the following program void main( ) { int x[10], i, *px=x, *py = &x[5]; i = py-(px++); } What is the value of i? (a) 4 (b) 5 (c) 6 (d) 10. 4. How are the declarations int *p; and int **p; different? Page 1 of 11

Pointers Assignment

Embed Size (px)

Citation preview

Page 1: Pointers Assignment

DPS – Modern Indian SchoolDoha, Qatar

Pointers WorksheetClass XII Name: _________________

1. Find mistakes, if any, in the following definition and make necessary correction:char S[10], *ptr = S[6];

______________________________________________

2. Distinguish between: int *ptr = new int(5); and int *ptr = new int[5];

__________________________________________________________________________________________________________________________________________

3. Consider the following programvoid main( ){ int x[10], i, *px=x, *py = &x[5]; i = py-(px++);}

What is the value of i?(a) 4 (b) 5 (c) 6 (d) 10.

4. How are the declarations int *p; and int **p; different?

______________________________________________________________________________________________________________________________________________________________________________________________________________________________________

5. Rewrite the following declaration, without errors and underlining the corrections made:

Page 1 of 9

Page 2: Pointers Assignment

float **P1, *p2=&p1;

____________________________________________________________________________________________

6. Differentiate between static and dynamic allocation of memory.

Static Dynamic

1

2

3

7. Define a pointer. What is the relationship between an array and a pointer? Given below is a function to traverse a character array using for-loop. Use pointer notation in place of array notation and substitute for-loop with while-loop so that the output of the function stringlength() remains the same.

int strlength(char s[]){ int count=0; for(int x=0; s[x]; x++,count++); return (count);}

______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

8. What will be the output of following code fragment:

void main(){ int a[ ]={ 3, 5, 6 , 7}, *p=a;

Page 2 of 9

Page 3: Pointers Assignment

cout<<*p<< “\t”<<*(p+2)<< “\t”<<*p++;}

______________________________________________

9. Give the output of the following program segment. (Assume required header files are #included):char *s = “SUPER”;for (int x=strlen(s)-1; x>=0; x--){ for(int y=0; y<=x; y++) cout<<s[y]; cout<<endl;}

10. Give the output of the following code. (Assuming header files are #included):

char *NAME= “a ProFile”;

_______________for (int x=0; x<strlen (NAME); x++) if (islower(NAME[x])) NAME[x]=toupper(NAME[x]); else if (isupper(NAME[x])) if (x %2!=0) NAME[x]=tolower(NAME[x-1]); else NAME[x]--;cout<<NAME<<endl;

11. Re-write after correcting syntax error(s), if any. Underline the corrections made.void main( ){ const int i=20; const int *ptr=&i; *ptr++; int j=15; ptr=&j;}

12. Give the output of the following. (Assume all required header files are #included):void main( ){ int a=32, *X=&a; char ch=65, &cho=ch; cho += a; *X += ch; cout << a << “, ” << ch << endl;

Page 3 of 9

______________________________________________________________________________________________________________________________________________________

______________________________________________________________________________________________________________________________________________________________________________________________________________________________

____________________

Page 4: Pointers Assignment

}

13. Give the output of the following program segment (Assume header files are #included):void main( ){ int array[ ]={2,3,4,5}, *arptr=array; int value=* arptr; cout << value << " "; value=*arptr++; cout << value << " "; value=*arptr; cout << value << " "; value=* ++arptr; cout << value;}

_________________________________________

14. Find the output of the following program :-void main( ){ int x[]={10,25,30,55,110}, *p=x; while (*p<110) { if(*p%3 !=0) *p=*p+1; else *p=*p+2; p++; } for (int i=4; i>=1; i--) { cout<<x[i]<< "*"; if (i % 3==0) cout<<endl; } cout<<x[0]*3<<endl;}

_________________________________________

Page 4 of 9

Page 5: Pointers Assignment

15. WAP to input a string and copy it onto another. The strings are passed to a UDF, which copies the strings character by character. Do not use any std. library functions.

___________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

16. Write a function that takes two strings arguments and returns the string whose ASCII value is the greater at the 1st differing position. Test the function by calling it from main() with 2 parameters.

__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Page 5 of 9

Page 6: Pointers Assignment

____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

17. Write a program to test whether an input string is a palindrome or not. (Use pointer notation).

_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Page 6 of 9

Page 7: Pointers Assignment

__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

18. WAP to accept a string and a character and print the number of times the character occurs in the string.

_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Page 7 of 9

Page 8: Pointers Assignment

___________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

19. WAP to create a 3*4 integer array with some initial values. Print the address of each element in tabular form. Also print the value of each element in tabular form. (Use pointer notation)

________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Page 8 of 9

Page 9: Pointers Assignment

______________________________________________________________________________________________

Page 9 of 9